Even if I only change one of my classes, Maven always recompiles all of them. I use this plugin configuration:
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<staleMillis>1</slateMillis>
<useIncrementalCompilation>true</useIncrementalCompilation>
</configuration>
</plugin>
</plugins>
This happens with mvn compile
, mvn package
and mvn install
.
Of course this is not a problem if you have 10-15 files. However, I have more than one thousand source files and it takes a lot of time.
Does the Maven compiler plugin have some hidden settings to recompile only modified files? Are there any workarounds?
While you can tell Maven "to recompile only modified files", doing so will lead to wrong results. The default behavior is not a bug, but an intentional design decision.
useIncrementalCompilation
really doesThe documentation on this topic is, to put it mildly, not optimal. Here is what really happens (based on AbstractCompilerMojo source from maven-compiler-plugin
3.3):
useIncrementalCompilation
set to false
(not recommended)
useIncrementalCompilation
set to true
(default)
Changes detected - recompiling the module!
So in summary, useIncrementalCompilation
should always be left at the default of true
.
Understandably, one might ask: why does the plugin not determine which classes are affected by the changes, and recompile only those classes? In the comments on MCOMPILER-205, Maven developer Robert Scholte gave a brief rationale and later confirmed the following detailed explanation:
If any source file has been changed or removed, all files are deleted and recompiled. The reason for this is that simply recompiling everything with the default java compiler is quite fast, likely much faster than the alternative, which would look similar to this:
- detect all changed files
- analyze all source files to map all relations between classes
- calculate all affected files
- recompile affected files
However, as Robert also writes, recompiling everything is probably not necessary if the project uses the Eclipse compiler, which does this analysis. But for today's Maven users, this is a moot point as maven-compiler-plugin
does not yet change its behavior based on the choice of compiler.