Search

Dark theme | Light theme

November 18, 2022

Mastering Maven: Exclude Modules From Build

When we are working with a multi-module project in Maven we might want to exclude a module when we invoke a build command. We might only be interested in partially building some modules. We can use the command line option -pl or --projects to specify a list of modules that need to be in our build. But we can also use ! followed by the module name to exclude modules from our build.

In the following example we have multi-module project with 4 modules: root with name parent-project, core, service and webapp:

$ pwd
parent-project
$ tree -L 1
.
├── core
├── pom.xml
├── service
└── webapp
$

First we run verify for the complete project and we see in the output that the reactor build order contains all modules:

$ mvn verify
...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO]
[INFO] parent-project                                                     [pom]
[INFO] core                                                               [jar]
[INFO] service                                                            [jar]
[INFO] webapp                                                             [war]
[INFO]
...
$

Suppose we want to exclude our webapp module than we use $ mvn -pl '!webapp' verify. In the output we see the reactor build order doesn't include our webapp module:

$ mvn verify -pl '!webapp'
...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO]
[INFO] parent-project                                                     [pom]
[INFO] core                                                               [jar]
[INFO] service                                                            [jar]
[INFO]
...
$

As our list of modules is not so big in this sample project we could also have used the option -pl to only specify the modules we want to build separated by a comma. We must use . to indicate our root module:

$ mvn verify -pl '.,core,service'
...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO]
[INFO] parent-project                                                     [pom]
[INFO] core                                                               [jar]
[INFO] service                                                            [jar]
[INFO]
...
$

Written with Maven 3.8.6.