Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven - making child projects that can be independent of their parent

Tags:

maven-2

I'm a bit of a Maven newb, and I'm trying to setup a maven project that builds several child projects, but still allows someone to just grab one of the child projects and build it independently without the parent.

parentfolder
  ->pom.xml
  ->project1
    ->pom.xml
    ->src
  ->project2
    ->pom.xml
    ->src
  ->project3
    ->pom.xml
    ->src

Basically I want someone to be able to checkout parentfolder and do mvn compile to build all the projects, and also for someone to be able to checkout just project1 and do mvn compile to build just it.

I've tried declaring the sub-projects as modules in the top-level pom.xml,

<modules>
  <module>project1</module>
  <module>project2</module>
  <module>project3</module>
</modules>

But that seems to require that the parent pom.xml info is declared in the children. This makes the child projects dependent on the parent pom.xml being present, which is what I wanted to avoid.

like image 805
Dana Avatar asked Nov 12 '09 21:11

Dana


People also ask

Does child POM inherit dependency?

Now child POM need to refer the parent POM using parent tag and specifying groupId/artifactId/version attributes. This pom file will inherit all properties and dependencies from parent POM and additionally can include extra sub-project specific dependencies as well.

What is parent Relativepath in Maven?

The relative path, if not given explicitly, defaults to .. , i.e. the pom in the parent directory of the current project. So Maven checks whether a) there is a pom file in that directory and b) that pom file contains the same coordinates as stated in the parent definition of the current project.

What is the use of parent in Maven?

A parent pom. xml file (or super POM) in Maven is used to structure the project in order to avoid redundancies and duplicate configurations by using an inheritance between different pom. xml files. If any dependency or properties are configured in both - parent and child - pom.

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 have to take care of the differences between the parent-child relation and the aggregation concept in Maven2. They are not the same principle, even if they are really often used at the same time.

PARENT

The first concept is that a project declares in his pom.xml a parent:

<project>
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>foo</groupId>
        <artifactId>bar</artifactId>
        <version>42</version>
    </parent>
    ...

In this case, in order to build this component, the parent project must be found in the local repository. This is your case here.

The interest of the parent concept in Maven 2 is to inherit properties, dependencies, configuration. This is the place where you will put all the common information of the children projects.

This is the exact same concept of the extends in Java language.

AGGREGATION

In this case, you have a project that aggregates several sub modules by specifying their names in module nodes:

<modules>
    <module>commons</module>
    <module>client</module>
    <module>server</module>
    ...
</modules>

This means that every command that you will run on this root project will be executed on each module (the order is defined by the Maven 2 Reactor). For example, if you run mvn clean install on the root project, Maven 2 will run this command on the root project, then on project commons, then on client and finally on server.

In this concept, you are able to compile one project without compiling any other one project (except if there are inter-dependencies of course).

Here is a schema that shows the two different concepts:

alt text

You have a more detailed explanations on these two concepts in the Maven : The Definitive Guide, here.

like image 108
Romain Linsolas Avatar answered Oct 18 '22 18:10

Romain Linsolas


The sub-projects are only dependent on the parent if they use the <parent> tag (maven inheritance). You are not required to use the <parent> tag in submodules. However, if you want your modules to inherit common elements from the parent pom, then you have to use maven inheritance.

Strictly speaking, if you are using inheritance and want to build only a sub-project, then the parent pom.xml doesn't have to be present in the parent directory; as long as maven can find the parent pom in a local or remote repository, then it will build. In practice, if there are changes going on in your parent pom, you'll have to work out how to get your team members to keep up to date with the parent pom.

like image 2
Ken Liu Avatar answered Oct 18 '22 19:10

Ken Liu