How does one use Maven to support incremental builds? Is there a guide somewhere? (top Google results are disappointing)
-
1Give it a try with Bing - "Incremental Maven Build" - it gave me this which is quite good - [blog on incremental build plugin](http://harshana05.blogspot.com/2011/05/apache-maven-incremental-build-support.html) – raksja Jul 12 '12 at 21:21
-
1But it doesn't provide functionality of "Incremental Testing", all modules' tests will be run eventhough build happens incrementally. – raksja Jul 12 '12 at 21:23
6 Answers
I can't quite figure out the dynamic that drives the Maven community but it isn't one that's friendly to having fine-grained control over your build-process.
Anyhow, after digging around I found an answer that worked for me here: http://www.codesenior.com/en/tutorial/Java-Maven-Compile-Only-Changed-Files
Note that setting the value to false
confused me at first, but an explanation is given here: https://stackoverflow.com/a/19653164/409638
Reproduced here for convenience:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<useIncrementalCompilation>false</useIncrementalCompilation>
</configuration>
</plugin>
It's the useIncrementalCompilation
set to false
that is key here.
I can confirm that when I run my build I have gone from:
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 114 source files to /home/vagrant/workspace/splat/target/classes
to
[INFO] Compiling 1 source file to /home/vagrant/workspace/splat/target/classes
which has shaved a few seconds of my incremental build. Now all I've got to do is figure out how to disable all the other unnecessary cruft that's slowing down my edit/evaluate cycles ...
-
To disable all the other unnecessary cruft, try look this post https://stackoverflow.com/a/11081252/1767021 – deldev Feb 13 '20 at 18:00
-
I decided to treat my build issues as an X/Y problem, and just eliminated Maven. Everybody is much happier now. – robert Feb 14 '20 at 14:18
You can use the maven-incremental build plugin, if your project has hundreds of modules. It saves lot of time.

- 3,928
- 6
- 39
- 54

- 659
- 3
- 8
-
I am assuming this is similar to maven 3.1 incremental compilation https://maven.apache.org/plugins/maven-compiler-plugin/compile-mojo.html#useIncrementalCompilation – kisna Apr 05 '16 at 23:09
-
5
-
It's been moved to GitHub: https://github.com/vsellier/maven-incremental-build – Julien Carsique Feb 16 '21 at 10:46
Takari Maven Lifecycle
Yes, it is possible now thanks to takari-lifecycle-plugin. Take look at this sample project: maven-incremental-compilation
Sample output
[INFO] --- takari-lifecycle-plugin:1.10.2:compile (default-compile) @ maven-incremental-compilation ---
[INFO] Performing incremental build
[INFO] Compiling 2 sources to /home/mariuszs/maven-incremental-compilation/target/classes
[INFO] Compiled 1 out of 2 sources (670 ms)
More information

- 30,646
- 12
- 114
- 155
Maven builds incrementally by default, but it turns out that the compiler plugin (i.e., the core of javac) is so fast that building fresh every time is not a bottleneck with sane codebase sizes, not by comparison with constructing large assemblies or running large test suites. (Java, like most languages, is much faster to compile than C++.)

- 133,037
- 18
- 149
- 215
-
7For comparison, in my code the time spent compiling Java code is about 1% of the total time to build, maybe less. It's short enough that I _simply don't care_ to try to optimize it further. – Donal Fellows Jan 18 '12 at 23:11
-
17The problem is exactly in all other build steps (primarily unit testing, packaging, other plugins like assembly, etc.) which depend on compilation phase. If they got triggered regardless of whether there were any changes to compile or not, you've got all the time wasted people try to avoid when they ask for incremental builds. – uvsmtid Nov 20 '15 at 02:13
-
9Maven does not build incrementally. The creator of Maven is working on this with a new lifecycle. I have absolutely no idea why the moderator @Ryan deleted an answered that referenced this: http://stackoverflow.com/posts/28224819/revisions – Adam Gent Aug 09 '16 at 18:43
-
33`the compiler plugin is so fast that building fresh every time is not a bottleneck with sane codebase sizes` - Seriously? That reasoning is the same as: Why would you fix a bug that keeps crashing your application which automatically restarts super fast while fixing the bug would take hours. – Ben Oct 06 '16 at 10:21
-
5The likes of this answer, is exactly the reason I'll never willingly use maven. Poor documentation. No straight answers. Far too much "Woo" hocus pocus. – robert Dec 23 '16 at 11:40
-
3Yes, compilation is fast. But unnecessary compilation still slows down a build, because changed timestamps of classes/jars make it impossible/hard to reliably avoid unnecessary rerunning of tests etc. The whole build system needs to be designed with incremental object graphs in mind. Unfortunately maven hasn't been. – Ville Oikarinen May 09 '17 at 11:40
-
3Using tools like gradle, it's not just about avoiding the running of javac, it's also about avoiding tasks that depend on the output of javac. That is, gradle can also avoid running test tasks if the sources for that module haven't changed. Maven fails because it has to run all tasks every time, as it has no notion of "if you didn't change the jar from projectA, don't build or test projectB". – Ajax Mar 02 '18 at 12:41
-
1IDK, my simple Java API server takes at least 5 seconds to build/run using `mvn -offline -T 16 package exec:java -DskipTests` and I am on a very fast machine. – Alexander Mills Dec 20 '18 at 11:08
-
This answer is not correct as compiling is just a small part of building. – Jason Young Sep 13 '19 at 17:12
Update: truly incremental build support - similar to one in Gradle - has made it in Maven code base. Introduction video is here: https://youtu.be/DEQG4CNFMFE
Update2: maven build cache extension officially released - https://github.com/apache/maven-build-cache-extension
Kind regards Alex

- 151
- 1
- 6
-
Just a hint what to expect - build time for 700+ modules project reduces from 50 mins to 4 mins – ursa Aug 31 '21 at 17:34
Maven supports building subsets of multi module projects using the command line arguments -pl
, -am
and -amd
to specify modules to build, also build dependencies and also build dependents, respectively. It will also only compile changed source files in any given module (not really a Maven feature so much as a javac feature).

- 11,919
- 3
- 40
- 53