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.

How to

The trick here is to use Maven advanced reactor options, particularly the following options:

-pl, –projects
Build specified reactor projects instead of all projects
-am, –also-make
If project list is specified, also build projects required by the list

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:

$SHELL > mvn clean install -pl A3
Advertisements

2 comments

  1. How does this option differ from cd’ing into the submodule project directory and executing maven build inside the subdirectory?

    1. Hello José,

      First of all, thank you for your comment 🙂

      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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s