Create Maven to create only modified artifact

In our project, Maven build creates artifacts for different modules, for example, jar, console, car, etc. in the corresponding folder structure.

Each time we check the code, the assembly generates all new artifacts, even if there are only changes in the "console" module.

Is there any Maven plugin or way to generate only artifacts that have changed since the last successful build?

For example, if I changed the code for the "console" module, then the created artifact should only have a console file in the corresponding folder.

+6
source share
4 answers

If you are on the command line, you can use

mvn -pl moduleToBuild 

which can be combined with:

 mvn -pl moduleToBuild -am 

which will also create moduleToBuild dependencies.

If you are in a CI solution, such as jenkins, there is a checkbox to activate this behavior. This can be found in the Maven configuration section. Incremental build - only compile modified modules .

You need to run the maven call at the root of your multi-module assembly.

+5
source

You can look at the plug-in for the maven reactor reactor: make -scm-changes . This link has an example of how to use this.

+1
source

I was looking for something to check which files I changed from the upstream version and compile all the Maven modules that contain the files, and it all depends on them.

Since reactor:make-scm-changes doesn't seem to do this, one way to do this (Linux Bash) is

The rest connects it together using pipes and functions.
Of course, this assumes that all sources are in a folder with pom.xml , which is usually true.

+1
source

Here is an example of the approach mentioned by Ondra Žižka using mvn clean install and bash.

Note that it ignores pom packaging modules (since they are usually the roots of subtrees and usually cause additional, unnecessary modules to be created). He also searches for pom.xml files at 3 levels (for speed), assuming they are all parts of the same reactor, but this can be adapted to your project.

 find . -maxdepth 3 -name pom.xml | xargs -I{} grep -iL -F "<packaging>pom</packaging>" {} | xargs dirname | grep -v target | sed -e 's/^.[/]*//g' | grep . > /tmp/mvn-modules.txt && git diff --name-only @{u}...HEAD | grep -o -F -f /tmp/mvn-modules.txt | xargs | tr ' ' ',' | xargs -I{} mvn clean install -U -pl {} -amd 

In the @{u}...HEAD example, the links are changed in the current branch compared to the upstream, but this can be replaced with another diff (the <branchname> master example), if this is more suitable.

0
source

All Articles