Menu

Build maven specific module only with dependencies

Goal

Building a module with its dependencies in a multi module Maven project

Description

This recipe shows you how to build a specific Maven module in a multi module Maven project. It may be useful to reduce the build time during development, and it’s particular useful when the project you are working has lots of modules and it takes long time to finish the build.

So, using the previous options, and assuming we have a multi module maven project A with submodules from A1 to A10, where we only want to build A3 with its own dependencies, we could simply go to the root of A and execute the following command:

$SHELL > mvn clean install -pl A3 -am

Explanations

This recipes makes it possible to decrease the build time, thus leading to decreased development time too, which may be a good way to make the development faster and cheaper.

Similarly to the previously shown maven command, and assuming that, since our previous maven build, we had not changed anything in the “dependent” modules of A3 and we had only changed something in A3, we could also simply build module A3 without its dependents:

For this particular example, I would say the only difference is the command(s) you write, not the result you get.

Notice, however, that these options (-pl and -am) make it possible to build a list of child modules. So, by simply replacing the “A3” command in the recipe with “A3, A4” you may execute child modules A3 and A4, if you want, for instance, with something similar to:

$SHELL > mvn clean install -pl A3, A4 -am

The equivalent option would be cd’ing into each submodule and build each. However, the dependent modules, if any would be “shared” between both A3 and A4 would have to be built twice (one for each submodule build), which is not equivalently efficient.

The maven advanced reactor options do not give you anything that you can’t get manually. They only provide you with more flexible and, in some cases, more efficient options for your own builds.