tags:

views:

42

answers:

2

I want to execute linux shell commands with maven. Here is what I tried:

<plugin>  
  <groupId>org.codehaus.mojo</groupId> 
  <artifactId>exec-maven-plugin</artifactId> 
  <version>1.1.1</version> 
  <executions>
    <execution>
      <goals>
        <goal>exec</goal> 
      </goals>
    </execution>
  </executions>
  <configuration>
    <executable>hostname</executable> 
  </configuration>
</plugin>

But the above is not working. Can you please help me out.

+4  A: 

Here's what's been working for me:

                    <plugin>
                            <artifactId>exec-maven-plugin</artifactId>
                            <groupId>org.codehaus.mojo</groupId>
                            <executions>
                                <execution><!-- Run our version calculation script -->
                                    <id>Version Calculation</id>
                                    <phase>generate-sources</phase>
                                    <goals>
                                            <goal>exec</goal>
                                    </goals>
                                    <configuration>
                                            <executable>${basedir}/scripts/calculate-version.sh</executable>
                                    </configuration>
                                </execution>

Curtis
A: 

The problem here is that I don't know what is expected. With your current setup, invoking the plugin on the command line would just work:

$ mvn exec:exec
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building Q3491937
[INFO]    task-segment: [exec:exec]
[INFO] ------------------------------------------------------------------------
[INFO] [exec:exec {execution: default-cli}]
[INFO] laptop
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
...

The global configuration is used, the hostname command is executed (laptop is my hostname). In other words, the plugin works as expected.

Now, if you want a plugin to get executed as part of the build, you have to bind a goal on a specific phase. For example, to bind it on compile:

  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.1.1</version>
    <executions>
      <execution>
        <id>some-execution</id>
        <phase>compile</phase>
        <goals>
          <goal>exec</goal>
        </goals>
      </execution>
    </executions>
    <configuration>
      <executable>hostname</executable>
    </configuration>
  </plugin>

And then:

$ mvn compile
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building Q3491937
[INFO]    task-segment: [compile]
[INFO] ------------------------------------------------------------------------
[INFO] [resources:resources {execution: default-resources}]
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /home/pascal/Projects/Q3491937/src/main/resources
[INFO] [compiler:compile {execution: default-compile}]
[INFO] Nothing to compile - all classes are up to date
[INFO] [exec:exec {execution: some-execution}]
[INFO] laptop
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
...

Note that you can specify a configuration inside an execution.

Pascal Thivent
[ERROR] BUILD ERROR[INFO] ------------------------------------------------------------------------[INFO] One or more required plugin parameters are invalid/missing for 'exec:exec'[0] Inside the definition for plugin 'exec-maven-plugin' specify the following:<configuration> ... <executable>VALUE</executable></configuration>-OR-on the command line, specify: '-Dexec.executable=VALUE'I get this error now.
@gnash-85: I still have no idea of what you're doing. I used the exact snippet **you** provided and had no problem as shown above. Please update your question to show how you invoke maven (and what your current configuration is if you changed something).
Pascal Thivent
@Pascal: I have put the same code in a child module. And I am trying to execute the mvn exec:exec from the parent pom.xml. And i get this error. But when i individually execute it it is working.
@gnash-85: That's normal. When you invoke `mvn exec:exec` on the parent, mvn will run it on all projects of the multi-module build, including the parent. But the parent pom doesn't have any configuration for the plugin that expects the `executable` to be defined, hence the error message.
Pascal Thivent