tags:

views:

275

answers:

2

Hi,

I want to run a simple exec command post maven install phase. What is the simplest way possible to achieve this? (without adding new plugins)

+1  A: 

The maven default lifecycle ends with the install and deploy goals - there is no pre- and post- version of those goals.

Potential options would be to:

  • Bind the exec:exec mojo to with the deploy phase. I would warn you against that idea as the deploy phase is really meant to deploy artifacts to a remote maven repository.

  • Run the exec:exec mojo after the install goal in your mvn invocation... e.g.:

    mvn clean install exec:exec -Dexec.executable="yourcommand" -Dexec.args="arguments"

  • If you have a multi-module project, create another module at the end of your <modules> and bind exec:exec to any of the usual goals within that module. This will force this particular module to run last assuming other criteria the reactor looks at isn't met. A common strategy I employ with maven projects I work on is "when in doubt, create another module".

whaley
+3  A: 

If you want to run this command as part of the normal build lifecycle, there is no other way than binding the exec goal on the install phase:

<build>
  <plugins>
    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>exec-maven-plugin</artifactId>
      <version>1.1.1</version>
      <executions>
        <execution>
          <id>my-exec</id>
          <phase>install</phase>
          <goals>
            <goal>exec</goal>
          </goals>
          <inherited>false</inherited>
        </execution>
      </executions>
      <configuration>
        <executable>COMMAND</executable>
      </configuration>
    </plugin>
  </plugins>
</build>

I did a simple test using the configuration above (using ls as "COMMAND") with a freshly created maven project and running mvn install produces the following output:

$ mvn install
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building maven-exec-testcase
[INFO]    task-segment: [install]
[INFO] ------------------------------------------------------------------------
...
[INFO] [install:install {execution: default-install}]
[INFO] Installing /home/pascal/Projects/maven-exec-testcase/target/maven-exec-testcase-1.0-SNAPSHOT.jar to /home/pascal/.m2/repository/com/mycompany/app/maven-exec-testcase/1.0-SNAPSHOT/maven-exec-testcase-1.0-SNAPSHOT.jar
[INFO] [exec:exec {execution: my-exec}]
[INFO] pom.xml
[INFO] src
[INFO] target
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 12 seconds
[INFO] Finished at: Tue Jan 05 19:26:04 CET 2010
[INFO] Final Memory: 11M/75M
[INFO] ------------------------------------------------------------------------

As we can see, the command is executed at the end of the install phase (after the copy of the artifact to the local repository).

And if you really don't want to add the snippet to your pom, then you'll have to explicitly call exec:exec after install on the command line as suggested by whaley.

Pascal Thivent