Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to build dependent project when building a module in maven

Tags:

How can I build the dependent projects when a child project is getting build by maven. As an example, I have 2 projects call A,B. Project B is depending on project A. I want to build the project A when I am building project B with maven. How should I do it?

like image 872
Anushka Ekanayake Avatar asked Aug 25 '14 07:08

Anushka Ekanayake


People also ask

How do you add dependencies in the Maven project?

Right-click the utility project, and select Maven>Add Dependency. Type a dependency name in the Enter groupID… field (e.g., commons-logging) to search for a dependency. Select the dependency, and click OK.

How does Maven work with dependencies?

Dependencies are external JAR files (Java libraries) that your project uses. If the dependencies are not found in the local Maven repository, Maven downloads them from a central Maven repository and puts them in your local repository.


1 Answers

Take a look at these options that can be passed to mvn:

Options:
 -am,--also-make                        If project list is specified, also
                                        build projects required by the
                                        list
 -amd,--also-make-dependents            If project list is specified, also
                                        build projects that depend on
                                        projects on the list

I believe in your case you have to use -amd

Edit: In case you need to do it through a pom. You just need to create another module say C, that just lists the sub modules A and B. And when you build C, the maven reactor will automatically build both.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.test</groupId>
  <artifactId>ParentModuleC</artifactId>
  <packaging>pm</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>ParentModuleC</name>
  <dependencies>
  </dependencies>
  <build>
  </build>
  <modules>
    <module>ModuleA</module>
    <module>ModuleB</module>
  </modules>
</project>

In the ModuleA and B you need to add this:

<parent>
    <groupId>com.test</groupId>
    <artifactId>ParentModuleC</artifactId>
    <version>1.0-SNAPSHOT</version>
</parent>

And your directory structure will look like this:

ParentModuleC
    |-pom.xml
    |----------->ModuleA
    |               |->pom.xml
    |----------->ModuleB
    |               |->pom.xml

Have a look at this for a simple example: http://books.sonatype.com/mvnex-book/reference/multimodule.html

like image 92
Yogesh_D Avatar answered Sep 17 '22 12:09

Yogesh_D