6

In our project, Maven build generates artifacts for different modules i.e. jar, console, car etc in corresponding folder structure.

Everytime we check in the code, the build genarates full new artifacts even if there is only change in "console" module.

Is there any Maven plugin or a way to generate only the artifacts which were changed since last successful build?

For instance, if I have changed the code for "console" module, then the artifact generated should only have console file in its corresponding folder.

Ondra Žižka
  • 43,948
  • 41
  • 217
  • 277
Pankaj Dwivedi
  • 159
  • 2
  • 13

4 Answers4

5

If you are on command line you can use

mvn -pl moduleToBuild

which can be combined with:

mvn -pl moduleToBuild -am

which will also build the dependencies of moduleToBuild.

If you are in a CI solution like jenkins there is a check box to activate this behaviour. This can be found under the Maven configuration part Incremental build - only build changed modules.

You have to start the maven call on the root of your multi-module build.

khmarbaise
  • 92,914
  • 28
  • 189
  • 235
1

You may want to look at using maven reactor plugin's reactor:make-scm-changes goal. This link has example on how to use this.

Raghuram
  • 51,854
  • 11
  • 110
  • 122
  • Not necessary in Maven 3. Better use mvn -pl may be in combination with -am. – khmarbaise Dec 04 '12 at 12:50
  • I have tried, but the usage is not clear to me. "The parameters 'artifactList', 'folderList' for goal ... are missing or invalid." To identify the artifacts to build is what I want from this plugin. – Ondra Žižka Sep 21 '17 at 08:02
1

I was looking for something that would check what files I have changed compared to the "upstream" version, and build all Maven modules which contain the files, and all depending on them.

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

  1. list the changed files, using (git diff --name-only master...),
  2. find the nearest pom.xml for all,
  3. deduplicate (... | sort | uniq),
  4. provide it to Maven as a list using --project-list, with --also-make.

The rest is joining it together using pipes and functions.
Of course this assumes that all sources are within the folder with pom.xml, which typically is true.

Ondra Žižka
  • 43,948
  • 41
  • 217
  • 277
1

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

Note, it ignores pom packaging modules (as those are typically roots of subtrees and would usually cause additional, unnecessary modules to be built. It also looks for pom.xml files 3 levels deep (for speed), assuming they're all part of the same reactor, but this can be adjusted 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 example @{u}...HEAD references changes in current branch compared to upstream, but this can be swapped for another diff (example <branchname> master) if this is more suitable.

f.carlsen
  • 445
  • 4
  • 8