java maven如何只编译修改后的java文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11772747/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 06:18:50  来源:igfitidea点击:

How does maven compile only the modified java files?

javamavencompilationmaven-3last-modified

提问by Senthil Kumar

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.

我只是想知道这一点,当我给 mvn install 而不做“清理”时,maven 只编译修改过的 java 文件。maven如何识别一个java文件是否被修改?我相信它没有使用文件的最后修改属性。

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.

我相信的原因:我有一个模块,在从 svn 合并更改后,我给了 mvn install 并且它没有编译修改后的文件,当我查看更改时,我看到“long”被修改为“Long”吸气剂和吸气剂。

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

所以我只想知道 maven 如何识别 java 文件是否已更改?

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

(PS 我正在使用 Apache Maven 3.0.3,如果这很重要)

回答by Duncan Jones

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

我相信 Maven 编译器插件使用源文件和类文件上的最后修改日期来确定是否需要重新编译。

The compiler websiteis rather short on information, but the compiler:compilegoal 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.

编译器网站的信息相当少,但compiler:compile目标页面有关于以下属性的信息,可以微调过时计算:http: //maven.apache.org/plugins/maven-compiler-plugin/compile-mojo.html #staleMillis。这是关于陈旧性的唯一官方声明。

回答by ThePadawan

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.

在不太了解 maven 的情况下,我可以告诉你,一般来说,“make”之类的工具使用“上次更改”时间戳,这可以解释你在使用 svn 时遇到的问题(请参阅维基百科关于 Subversion 的弱点

回答by George Birbilis

Robert Scholte's comment at https://issues.apache.org/jira/browse/MCOMPILER-205explains 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):

Robert Scholte 在https://issues.apache.org/jira/browse/MCOMPILER-205 上的评论解释了这个过程。这取决于“maven-compiler-plugin”的“useIncrementalCompilation”选项(顺便说一句,在它的版本上,我只设法让“useIncrementalCompilation”与3.1一起工作,而不是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.

我看到有些混乱,所以需要改变一些东西,也许改进文档就足够了。查看代码,您会看到非增量只会查看更改的源文件。Incremental 还将验证依赖项是否已更改以及文件是否已添加或删除。如果它发生了变化,它将删除完整的类目录。原因是默认的 java 编译器非常快,可能比分析每个文件要做什么要快得多。IIUC eclipse 编译器是一个真正的增量编译器,所以我们可以根据使用的编译器来决定不删除类目录。