How does maven compile only the modified java file

2020-02-06 18:59发布

问题:

I was just curious to know this, when i give mvn install without doing 'clean', maven compiles only the modified java files. How does maven identify a java file is modified or not? I believe it is not using the last modified property of the file.

Reason for my belief: I had a module, after merging a change from svn, i gave mvn install and it didn't compile the modified file and when i looked at the change i saw that 'long' were modified to 'Long' in getters and setters.

So i just want to know how maven identifies if a java file has changed or not?

(P.S I'm using Apache Maven 3.0.3, if that matters)

回答1:

I believe the Maven compiler plugin uses last modified dates on the source and class files to determine whether recompilation is necessary.

The compiler website is rather short on information, but the compiler:compile goal page has information on the following attribute, which finely tunes the staleness calculations: http://maven.apache.org/plugins/maven-compiler-plugin/compile-mojo.html#staleMillis. That's about the only official statement regarding staleness.



回答2:

Without knowing much about maven, I can tell you that generally speaking, "make"-like tools use the "last changed" timestamp, which would explain the issue you had with svn ( see Wikipedia on Subversion's weaknesses.



回答3:

Robert Scholte's comment at https://issues.apache.org/jira/browse/MCOMPILER-205 explains the process. It depends on the "useIncrementalCompilation" option of the "maven-compiler-plugin" (and on the version of it btw, I've only managed to have "useIncrementalCompilation" work with 3.1, not 3.0):

I see there's some confusion, so something needs to be changed, maybe improving documentation is good enough. Looking at the code, you'll see that non-incremental will only look at changed sourcefiles. Incremental will also verifies if dependencies have changed and if files have been added or removed. If it has changed, it'll remove the complete classes-directory. The reason is that the default java compiler is quite fast, likely much faster than analyzing per file what to do with it. IIUC the eclipse compiler is a real incremental compiler, so we could decide that based that based on the used compiler not to drop the classes directory.