13

I have multiple modules in my project and they are dependent on each other either directly or transitively. When I maven build «Project A» some where «Project D» gets build automatically.

Project A > Project B  > Project C > Project D

where > means Project B depends on Project A

«Project D» pom snipeet is:

<project xmlns="...">
    <modelVersion>4.0.0</modelVersion>  
    <groupId>com.myProduct</groupId>
    <artifactId>build-MyProjectD</artifactId>
    <name>MyProjectD</name>
    ........
</project>

As building «Project A» automatically build «Project B», as per my understanding to make this happen somewhere build-MyProjectD should be added as dependency in one of these projects Project A > Project B > Project C but I did not find any reference of string build-MyProjectD under poms of these projects.

Any idea how is there any other way to make build of child module (in this case «Project D») without having child artifactId presence in upstream project?

scott miles
  • 1,511
  • 2
  • 21
  • 36
  • you can use reactor which will contain all projects as modules. This [article](http://blog.sonatype.com/2009/10/maven-tips-and-tricks-advanced-reactor-options/) may help. – Cuzz Dec 29 '14 at 13:09

2 Answers2

19

You need to create an aggregator project. See the link for more information on the aggregation concept.

Basically, you create a parent project containing several "modules". When building the parent, the modules automatically gets built as well.

If you declare dependencies between the modules, Maven will automatically build the different modules in a correct order so that if «Project A» depends on «Project B», «Project B» is built first and then «Project A» is built so that its artifact is available for the building of the second artifact.

See also this question from the Maven's FAQ.

Guillaume Polet
  • 47,259
  • 4
  • 83
  • 117
  • If we just two modules and one module is dependent on another then we can have have depebdent module as dependency. Right? Yes if we have multiple modules, its better to go for aggregation. Is n't it ? – scott miles Dec 29 '14 at 14:26
  • @scottmiles Aggregation allows you to group a set of logical projects together and have them build in one go instead of having to build them one by one. You can have dependencies between those projects, or not. – Guillaume Polet Dec 29 '14 at 15:08
  • @ Guillaume Polet Agreed Aggragation is one way. Is another way can be adding child project as dependency in parent module, won't it build all child project automatically when we are building Parent module ? – scott miles Dec 30 '14 at 05:38
  • @scottmiles Not sure I understand your question. By definition, a child module belongs to a parent project (called an aggregator project). Maybe this [link](http://maven.apache.org/guides/getting-started/index.html#How_do_I_build_more_than_one_project_at_once) can also provide you some help as well. – Guillaume Polet Dec 30 '14 at 13:50
  • I have an aggregator that builds parent and child, but right when build starts it is looking for parent, why? – Kalpesh Soni Sep 06 '19 at 22:05
12

For a parent project Maven will build all child modules while building parent project. Add the modules to your parent pom. Assuming A is your parent project

  <modules>
    <module>projectB</module>
    <module>projectC</module>
    <module>projectD</module>
  </modules>

and in modules (B,C and D), add project A as parent (This is optional, Thanks @Guillaume Polet)

 <parent>
   <groupId>foo.bar</groupId>
   <artifactId>ProjectA</artifactId>
   <version>1.0-SNAPSHOT</version>
 </parent>

So if you build projectA, it will build ProjectB, ProjectC and ProjectD. Also maven is smart enough to figure out correct build order for B,C and D.

Community
  • 1
  • 1
Ajinkya
  • 22,324
  • 33
  • 110
  • 161