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?
            Asked
            
        
        
            Active
            
        
            Viewed 2.1k times
        
    1 Answers
25
            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
        Nicholas K
        
- 15,148
 - 7
 - 31
 - 57
 
        Yogesh_D
        
- 17,656
 - 10
 - 41
 - 55
 
- 
                    can I do it with pom? – Anushka Ekanayake Aug 25 '14 at 07:21
 - 
                    Edited the answer for how to do it using a multimodule project – Yogesh_D Aug 25 '14 at 07:31
 - 
                    2The answer has just been pointed as the answer to a duplicated question (http://stackoverflow.com/questions/41849997/make-maven-use-the-current-code-and-not-the-installed-version-when-compiling-chi). Sorry but this answer is not very clear... `amd` is referenced in the answer but not used in the proposed solution that relies on the multimodule concept. You should either remove the reference to `amd` or explain why it is not applicable in this case because otherwise it may be misleading. – davidxxx Jan 25 '17 at 11:22