Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make maven build of child module with parent module?

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?

like image 830
scott miles Avatar asked Dec 29 '14 13:12

scott miles


People also ask

What is parent Relativepath in Maven?

By default, Maven looks for the parent POM first at project's root, then the local repository, and lastly in the remote repository. If parent POM file is not located in any other place, then you can use code tag. This relative path shall be relative to project root.

Why might you not include groupId and version elements in child pom files?

Why might you not want to include groupId and version elements in child POM files? If you include these elements, an error will be thrown when you try to build the project. These elements are inherited from the parent POM file, and do not need to be repeated.


2 Answers

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.

like image 198
Guillaume Polet Avatar answered Sep 21 '22 18:09

Guillaume Polet


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.

like image 21
Ajinkya Avatar answered Sep 24 '22 18:09

Ajinkya