Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache Maven: What is the difference between Inheritance, Aggregation, and Dependencies?

Tags:

java

maven-2

I'm new to Maven, and I'm trying to understand why my company's modules are organized into 'module groups', but also each sub-module declares its parent explicitly. I don't quite understand what the POM Reference is trying to say about the difference between inheritance and aggregation.

For example, a parent module:

<groupId>example.group</groupId>
<artifactId>util</artifactId>
<packaging>pom</packaging>
<name>Util Parent</name>

<modules>
    <module>util_client</module>
    <module>util_core</module>
    <module>util_server</module>
</modules>

And one of its children:

<parent>
    <artifactId>util</artifactId>
    <groupId>example.group</groupId>
    <version>trunk-SNAPSHOT</version>
</parent>

<groupId>example.group.util</groupId>
<artifactId>util_core</artifactId>
<packaging>jar</packaging>
<name>Util Core</name>

Why declare it both ways? Is it redundant? To make things even more confusing, some of the util submodules depend upon eachother:

<groupId>example.group.util</groupId>
<artifactId>util_client</artifactId>
<packaging>jar</packaging>
<name>Util Client</name>

<dependencies>
    <dependency>
        <groupId>example.group.util</groupId>
        <artifactId>util_core</artifactId>
    </dependency>
</dependencies>

Sorry if this is a doozy of a question, but wow this is confusing! Thanks for your help.

like image 552
Neil Traft Avatar asked Sep 21 '11 15:09

Neil Traft


People also ask

What is project aggregation in Maven?

In Maven, project aggregation is similar to project inheritance, except that the change is made in the parent pom instead of the child pom. Maven uses the term module to define a child or subproject, which is part of a larger project. An aggregate project can build all the modules together.

What are Maven dependencies?

What is Maven Dependency? In Maven, a dependency is just another archive—JAR, ZIP, and so on—which our current project needs in order to compile, build, test, and/or run. These project dependencies are collectively specified in the pom. xml file, inside of a <dependencies> tag.

What is groupID and artifactId in Maven project example?

groupId – a unique base name of the company or group that created the project. artifactId – a unique name of the project. version – a version of the project. packaging – a packaging method (e.g. WAR/JAR/ZIP)


1 Answers

When you define sub-modules, you can build and release them all at once from the top level.

When you use inheritance in the second example, you can use definitions from the parent POM defined once, (Like which versions of software to use)

In the last example, when one module needs resources from another module, you can add it as a dependency and it will download and include it in the build path automatically.

like image 185
Peter Lawrey Avatar answered Sep 22 '22 10:09

Peter Lawrey