Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trigger a maven install command from another maven install command

Tags:

maven-2

Is there a way to trigger a maven install command from another maven install command?

In other words, I would like to be able to execute a maven install command on a maven project (in eclipse) and I want that this will automatically cause an install command on another maven project. Is that possible?

like image 504
Avi Y Avatar asked Oct 01 '09 08:10

Avi Y


2 Answers

The Maven way to "trigger" another build is to define a multi-module build. A parent pom project can specify modules, that will all be built using the standard lifecycle. So running mvn install on the parent would mean that each module is built in turn.

The parent is defined with pom packagin, and would have a modules declaration like this:

<modules>
  <module>module-a</module>
  <module>module-b</module>
</modules>

Alternatively it is possible to attach additional artifacts to a build so they are deployed alongside the primary artifacts (assuming they've already been packaged, you can use the build-helper-maven-plugin to attach an arbitrary file to your pom, so it will be deployed with the specified classifier. The following configuration will attach the specified file as my-artifact-1.0-extra.jar

  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <version>1.3</version>
    <executions>
      <execution>
        <id>attach-artifacts</id>
        <phase>package</phase>
        <goals>
          <goal>attach-artifact</goal>
        </goals>
        <configuration>
          <artifacts>
            <artifact>
              <file>/path/to/extra/file.jar</file>
              <type>jar</type><!--or specify your required extension-->
              <classifier>extra</classifier>
            </artifact>
          </artifacts>
        </configuration>
      </execution>
    </executions>
  </plugin>
like image 198
Rich Seller Avatar answered Oct 26 '22 19:10

Rich Seller


As pointed out, the maven way to launch a goal (lets say mvn install) on a set of modules is to organize them as a multi-module project and to launch the goal on the parent pom. Behind the scene, Maven will use a "Maven reactor" for this work. The reactor will calculate the build order by doing a topological sort of the nodes of the directed graph constructed by the dependency relation between modules. This graph is constructed by looking at <modules> and <dependencies> tags in poms.

But launching maven from a parent is not the only option and maven offers more possibilities to play with the reactor (e.g. making a project and its dependencies or those that depend on it):

  • With maven 2.0.x you have to use the reactor plugin : http://maven.apache.org/plugins/maven-reactor-plugin/ (see Reactor: My New Favourite Maven Plugin too)

  • With maven 2.1+ you can use native command line options : http://www.sonatype.com/people/2009/03/maven-210-released/ (see the new build mode options -amd, -rf, -am, -pl)

Check it out, it might help you to achieve your goal.

like image 42
Pascal Thivent Avatar answered Oct 26 '22 19:10

Pascal Thivent