标准的 Sun javac 可以进行增量编译吗?

最近我开始使用 Eclipse 的 java 编译器,因为它比标准的 javac 快得多.有人告诉我它更快,因为它执行增量编译.但我仍然对此有点不确定,因为我找不到任何关于这两者的权威文档 - eclispse 和 sun 的 - 编译器增量功能".是不是 Sun 的编译器总是编译每个源文件,而 Eclipse 的编译器只编译已更改的文件以及受此类更改影响的文件?

Recently I started to use Eclipse's java compiler, because it is significantly faster than standard javac. I was told that it's faster because it performs incremental compiling. But I'm still a bit unsure about this since I can't find any authoritative documentation about both - eclispse's and sun's - compilers "incremental feature". Is it true that Sun's compiler always compiles every source file and Eclipse's compiler compile only changed files and those that are affected by such a change?

我没有使用 Eclipse 自动构建功能,而是在设置

I'm not using Eclipse autobuild feature but instead I'm setting

-Dbuild.compiler=org.eclipse.jdt.core.JDTCompilerAdapter

对于我的 ant 构建.

for my ant builds.

推荐答案

Sun 的编译器是否总是编译每个源文件而 Eclipse 的编译器只编译已更改的文件以及受此类更改影响的文件?

Is it true that Sun's compiler always compiles every source file and Eclipse's compiler compile only changed files and those that are affected by such a change?

我相信你在这两个方面都是正确的.

I believe that you are correct on both counts.

您当然可以强制 Eclipse 重新编译所有内容.

You can of course force Eclipse to recompile everything.

但等式的另一部分是,Ant 和 Maven 等 Java 构建工具只能编译已更改的类及其依赖类树.

But the other part of the equation is that Java build tools like Ant and Maven are capable of only compiling classes that have changed, and their tree of dependent classes.

编辑

在 Ant 中,增量编译可以通过两种方式完成:

In Ant, incremental compilation can be done in two ways:

  • 默认情况下 <javac>task 比较 .java 和对应的 .class 文件的时间戳,并且只告诉 Java 编译器重新编译比它们更新的源 (.java) 文件对应的目标(.class)文件,或者根本没有目标文件.

  • By default the <javac> task compares the timestamps of .java and corresponding .class files, and only tells the Java compiler to recompile source (.java) files that are newer than their corresponding target (.class) files, or that don't have a target file at all.

<depend> 任务 还考虑了类之间的依赖关系,它通过读取和分析嵌入在 .class 文件中的依赖关系信息来确定.在确定了哪些 .class 文件已过期后,<depend> 任务将它们删除,因此后面的 <javac> 任务将重新编译他们.然而,这并不完全是万无一失的.例如,对源代码的大量更改可能导致 <depend> 任务可能正在分析陈旧的依赖关系.在 .class 文件格式中,某些类型的依赖关系(例如静态常量)也不明显.

The <depend> task also takes into account dependencies between classes, which it determines by reading and analysing the dependency information embedded in the .class files. Having determined which .class files are out of date, the <depend> task deletes them so a following <javac> task will recompile them. However, this is not entirely fool-proof. For example, extensive changes to the source code can lead to the <depend> task may be analysing stale dependencies. Also certain kinds of dependency (e.g. on static constants) are not apparent in the .class file format.

要了解为什么 Ant <depend> 不是万无一失的,请阅读 文档.

To understand why Ant <depend> is not fool-proof, read the "Limitations" section of the documentation.

相关文章