Does Maven support incremental builds?
JavaMavenJava Problem Overview
How does one use Maven to support incremental builds? Is there a guide somewhere? (top Google results are disappointing)
Java Solutions
Solution 1 - Java
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 ...
Solution 2 - Java
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++.)
Solution 3 - Java
You can use the maven-incremental build plugin, if your project has hundreds of modules. It saves lot of time.
Solution 4 - Java
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
Solution 5 - Java
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).
Solution 6 - Java
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
Though maven core doesnt support caching so far, you can check this PR - it is a real incremental build with support for remote cache. It also support literally all plugins - https://github.com/apache/maven/pull/526
You can build and try maven from the branch and enjoy. We are working to bring caching in a form of core extension into Maven
Kind regards Alex