Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven: Aggregation vs. Dependency

I'm very new to Maven and am just now trying to set up my first project tree. I'm struggling to understand the difference between two alternatives:

I have jar and war projects (two each) that i want to bundle. Traditionally I'd just create an ear project that has all four of them as dependencies.

Now I read about the aggregation of poms and am not sure what to do any more (see http://maven.apache.org/pom.html#Aggregation). Should I create an aggregateted POM with the four projects?

I guess basically my question is: What's the big difference between a module and a dependency, if the dependency is one of my "own" projects.

like image 753
apropoz Avatar asked Jul 27 '10 12:07

apropoz


People also ask

What is 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 is an aggregator POM?

To simplify building multiple projects, you can optionally create an aggregate Maven project. This consists of a single POM file (the aggregate POM), usually in the parent directory of the individual projects. The POM file specifies which sub-projects (or modules) to build and builds them in the specified order.

What is Maven dependency?

Maven is chiefly used for Java-based projects, helping to download dependencies, which refers to the libraries or JAR files. The tool helps get the right JAR files for each project as there may be different versions of separate packages.


1 Answers

A module is just a way of organizing things.

In a multi-module build, you can build an entire tree of artifacts in one step (remember the Joel Test). However, each of these will be an individual artifact, which can individually be referenced as a dependency.

Here is a sample layout, packaging in parentheses.

root (pom)
    - project1 (jar)
    - project2 (war) -> references project1 as dependency
    - project3 (jar)
    - project4 (war) -> references project3 as dependency
    - project5 (ear) -> references project2 and project4 as dependency

call mvn install in the root directory to build the entire tree.

The assumption here is that project1 is only used by project2 and project3 is only used by project4. Otherwise here is a more complex scenario.

root (pom)
    - project1 (jar)
    - project2 (jar)
    - project3 (war) -> references project1 and project2 as dependency of scope provided
    - project4 (war) -> references project1 and project2 as dependency of scope provided
    - project5 (ear) -> references project1 through project4 as dependency

So, modules take away the work of building several projects independently, but you still need to manage your dependencies yourself.

like image 119
Sean Patrick Floyd Avatar answered Oct 16 '22 05:10

Sean Patrick Floyd