The example used in this article is based around a JEE project that produces an EAR file made up of two other modules. This example has two main modules:
simple-jar-child - A java library
simple-war-child - A web application
Which are built into an EAR file, defined as a third module called standard-ear.
To group configuration together and to provide a central place to build from we create a parent project called multi-module-mvn.
The example project refered to in this article is available on bitbucket.
The Parent pom
The parent pom is used to group the child modules together and the centralise common configuration.
The key elements in the parent pom are:
packaging - defined as “pom”
modules - contains a list of all the child modules
The simple-jar-child and simple-war-child Child Modules
The pom for these projects are very similar to a standalone module:
However, the groupId and version of this module is not defined, instead these values are inherited from its parent which is defined using the parent element. There are also no dependencies defined in this pom despite simple-jar-child requiring JUnit in order to run the tests, this is because these projects inherit this dependency from the parent.
The standard-ear Child Module
The standard-ear module is used to pull the other two child projects into an EAR file that can be deployed to a JEE container. This project is slightly different from the other children, firstly it contains no source files as the EAR file contents are pulled from the other modules, secondly the pom is a little bit more complex:
The output of this module is an EAR file, the contents of this file has to be defined in the module’s pom. The pom has two references to the other children that we want to include, firstly they are defined as dependencies so that maven knows that this module cannot be build until it has the artifacts from the other modules. Secondly, the list of artifacts to be included in the EAR file are defined in the maven EAR plugin configuration.
Building the multi-module Project
Now if we navigate to the multi-module-mvn directory and execute mvn install; maven automatically works out the right order based on the defined dependencies, building each module in turn. The EAR file is created in “standard-ear/targets”.
Building Individual Modules
As you project grows you won’t want to build all modules every time you make a change, so we can build just the modules that we want, there are two ways of doing this:
Specify the module at the command line:
mvn --projects standard-ear install
if you also build the project’s dependencies then add the “also-make” parameter:
mvn --projects standard-ear --also-make install
Alternatively, you can navigate to the child module and run maven, this will automatically pick up the parent pom.