Explain Codes LogoExplain Codes Logo

Maven Modules + Building a Single Specific Module

java
maven
build
dependency-management
Nikita BarsukovbyNikita Barsukov·Oct 15, 2024
TLDR

To build a specific module and its dependencies in Maven, run:

mvn clean install -pl :module-name -am

Here, mvn clean install issues Maven to clean and install. -pl indicates the project(s) you want to build, and -am makes Maven resolve and build any dependent modules which are part of the listed projects. Replace module-name with your module's artifactId.

But wait, there's more! Does your artifactId differ from the module's directory name? Fret not, just use a colon:

mvn clean install -pl :artifactId

Did you modify something in module A that B depends on? Skip the full rebuild and speed up your process:

mvn clean install -pl :A -am

By doing this, you're ready to build or test B without bothering about unchanged modules. Efficient, isn't it?

Catchy Maven options for efficient builds

Decoding Maven build options

Who knew that -pl and -am were code words! In Maven-verse, -pl lists the module to be built, and -am guarantees a 'depencency check', building required dependencies of your module.

For a twist, use -amd, short for --also-make-dependents, to build a module and the modules counting on it.

Opting for 'install'

Remember, mvn install isn't a magic pill. Unless dependencies have transformed, stick to mvn package to avoid unnecessary overwrites. Use mvn install only if the active development module undergoes frequent updates to keep dependencies fresh off the oven!

For a collection of modules to be built together, keep them under an aggregator project. Execute mvn install on the aggregator to get all the subset projects built together.

Maven and your IDE love story

Ensure your Maven works together with your IDE, such as Eclipse using m2eclipse. Leverage IDE's Maven features for efficient project management.

Quick tips for targeted builds

Choose your own adventure with profiles

Customize your Maven builds for various environments or requirements using profiles. Profiles are like changing outfits, suitable for every occasion.

Make the most out of snapshot versions

Install snapshot versions when a module has new changes. It's like getting the daily newspaper - you want only today's news, not yesterday's!

Use aggregator projects to tackle specific cases

Tackle different build cases by setting up aggregator projects for various subsets of your project. It's like having team leads for different project goals.

Handling large projects made easier

For large dependency graphs, use the mvn dependency:tree command to visualize and understand project dependencies. Remember: A dependency graph today can save a headache tomorrow!

Increment your builds

For large projects, take advantage of incremental builds. This builds only the modules that have been changed since the last build, thereby saving time. Let your CI/CD do the heavy lifting while you sit back and relax!