Antony Hutchison
Antony Hutchison

Reputation: 3001

Do I need to have a Parent declaration in my POM.xml?

I've declared my project as a pom.xml to define the parent, and it triggers a reactor build of all the included modules. Everything builds fine and works as expected. It builds in the right order, all the tests run correctly, and I get my expected output.

One of the projects is a shared library. I don't want to add a <parent> declaration here, so I didn't. It all still works.

My Question: do I need to bother adding a parent project declaration in any of my sub-projects? What are the pros and cons of having a two-way relationship between the projects? If I don't add the declarations, am I going to make it harder later when something stops working?

Rephrased into a single question: why bother with <parent> configuration in module pom.xml files?

Upvotes: 7

Views: 8354

Answers (2)

Lan
Lan

Reputation: 6660

Your question is related to the difference between Project Inheritance vs. Project Aggregation. The <parent> reference defines the inheritance relationship. The <modules> section in the parent pom.xml defines the aggregation. They are different.

If you do not have the <parent> configuration in the module pom.xml, it will not inherit the parent pom.xml configuration. So let's say you define the version of a dependency in the parent pom in the <dependencyManagement> section, the module pom.xml without parent reference will not inherit that. Or if all your child modules need to use a common library, you can define the dependency in the parent pom.xml. However, the module pom.xml without the parent reference will not inherit that dependency either.

For details, please check out Project Inheritance vs Project Aggregation

Upvotes: 14

user626607
user626607

Reputation:

It depends on how you configure your build. If you have a correctly configured parent pom, basic information like version and groupid can be shared between your projects.

You can define the project wide configuration information that should be shared in all the modules in the project. For example:

<modelVersion>4.0.0</modelVersion>
<groupId>groupd.id</groupId>
<artifactId>artifact.id</artifactId>
<packaging>pom</packaging>
<version>version</version>

With that in place, you do not have duplicate that information in the module projects. The same information can be referenced like so:

<parent>
    <groupId>group.id</groupId>
    <artifactId>artifact.id</artifactId>
    <version>version</version>
</parent>

Remember that the parent pom can be used to shared more information than specified above. You can do dependency management, plugin management and even define re-usable profiles.

Upvotes: 1

Related Questions