FIXED: this is a known bug in maven-compiler-plugin 3.1
I am converting an ant-based build of a 1000+ java-sources project to maven. So far so good, but every time l
Mismatch of classname and filename also produce this error.
Maven may show a message like:
[INFO] Changes detected - recompiling the module!
Because you have an empty java file (or all commented out) in the project that never compiles into a class file.
You can identify the reason why maven rebuilds by running maven with -X. Look near the above message.
Faced the same problem. Maven mvn compile
with -X
option showed that the problem is caused by package-info.java
file.
[DEBUG] Stale source detected: .../package-info.java
The problem is that there is no .class file generated for package-info.java
.
The solution was found in this PR: https://github.com/apache/flink/pull/5644/files referencing MCOMPILER-205
<compilerArgs>
<arg>-Xpkginfo:always</arg>
</compilerArgs>
which means:
Always generate package-info.class for every package-info.java file.
Another way this can happen is if your source file location and package name are mismatched.
e.g. if you have a source file in src/main/java/com/example/MyClass.java
but the class is declared in a mismatched com.example.util
package:
package com.example.util;
class MyClass { ... }
The compiled class file will end up in target/classes/com/example/util/MyClass.class
and this will confuse the 'stale file' check.
My situation was slightly different, so I'm just adding this in case someone else has the same issue. My project has no generated classes and no package-info.java
; only .java
files in src/main/java
.
tl;dr
Update to maven-compiler-plugin
3.1 or use maven-compiler-plugin
3.0 and do not set <overwrite>true</overwrite>
in maven-resources-plugin
.
Long version
With zero src tree changes, Maven was always showing output like:
$ mvn -o compile
[INFO] --- maven-compiler-plugin:3.0:compile (default-compile) @ my-project ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 134 source files to /home/me/my/project/target/classes
I thought it was the configuration of the maven-resources-plugin
in a parent POM my project is using.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<overwrite>true</overwrite>
</configuration>
</plugin>
Removing this plugin from the parent POM, or redefining in my project with <overwrite>false</overwrite>
fixed the incremental build problem.
I wondered why I had to do two builds after setting <overwrite>false</overwrite>
for Maven to do incremental builds again, so investigated further. That is simply because the first compile generates a file (called inputFiles.lst
) that is used to determine the files that have changed, so on the next compile it can use that file to detect changes. This is confirmed by a comment on MCOMPILER-187.
I realised I was using maven-compiler-plugin
3.0 and could just upgrade to
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
</plugin>
which also fixed the problem. 3.1 uses maven-shared-incremental 1.1 (instead of 1.0 which maven-compiler-plugin
3.0 uses. Note that MCOMPILER-187 and MSHARED-264 are the 2 bugs covering the change.
So back with maven-compiler-plugin 3.0, I observed that the target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst
was not being generated with <overwrite>true</overwrite>
set. So this could be a reason why a project fails to have incremental builds when using maven-compiler-plugin 3.0.
Clearly, overwriting the resources every compile is not usually desired, but the main problem here is that inputFiles.lst
is never generated, so Maven will never be able to make an incremental build. So check for the existence of inputFiles.lst
as maybe another plugin has somehow caused it to not be generated.
I rolled back maven compiler plugin to version 2.3.2 and it compiles only modified classes with java 8 without problems.