Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running a task post maven install

Tags:

maven-2

maven

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)

like image 951
Priyank Avatar asked Jan 05 '10 09:01

Priyank


People also ask

How do I run a Maven task?

From the main menu, select Run | Edit Configurations to open the run/debug configuration for your project. In the list that opens, select Run Maven Goal. In the Select Maven Goal dialog, specify a project and a goal that you want to execute before launching the project.

Does Maven deploy Run install?

Maven Install Command: mvn install The Install Plugin uses the information in the POM (groupId, artifactId, version) to determine the proper location of the artifact within the local repository. The mvn deploy runs the deploy plugin which deploys an artifact to the remote repository.

What is mvn clean install?

mvn clean install is the command to do just that. You are calling the mvn executable, which means you need Maven installed on your machine. (see How do you install Maven?) You are using the clean command, which will delete all previously compiled Java .


2 Answers

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.

like image 134
Pascal Thivent Avatar answered Oct 16 '22 00:10

Pascal Thivent


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".

like image 38
3 revs, 2 users 97% Avatar answered Oct 16 '22 00:10

3 revs, 2 users 97%