J S*_*ith 7 java eclipse ant compilation
这使用Eclipse编译find:
abstract class CollectionView implements Collection<Object> {
...
public Object[] toArray(Object[] o) {
if (fast) {
return get(map).toArray(o);
} else {
synchronized (map) {
return get(map).toArray(o);
}
}
}
...
}
class KeySet extends CollectionView implements Set<Object> {
protected Collection<Object> get(Map<Object, Object> map) {
return map.keySet();
}
protected Object iteratorNext(Map.Entry entry) {
return entry.getKey();
}
}
Run Code Online (Sandbox Code Playgroud)
但是使用Ant时无法编译:
错误:KeySet不是抽象的,并且不会覆盖Set中的抽象方法toArray(T [])
我可以看到代码使用Eclipse编译的原因:KeySet已经从CollectionView继承了toArray(T [])方法的实现.
但是当我使用Ant编译时为什么会失败?
<javac srcdir="src" destdir="bin" debug="on">
<compilerarg value="-Xlint:unchecked"/>
<compilerarg value="-Xlint:deprecation"/>
</javac>
Run Code Online (Sandbox Code Playgroud)
在某些情况下,eclipse 可以正常编译,而 javac 则不能。如果您不介意的话,我知道可以通过三种方法使用 eclipse 编译器进行构建。
打包 eclipse 预编译类(hacky,不推荐)
将 eclipse 编译器适配器与 Ant 结合使用。当您指定属性 build.compiler 时,此后的所有 javac 任务都将影响您的 Ant 构建。您可以将其设置为“org.eclipse.jdt.core.JDTCompilerAdapter”。请注意,您必须将此类(及其依赖的类)包含在 ant 构建类路径中。最直接的方法是将必要的 jar 添加到 Ant 安装的 lib 文件夹中
使用 maven 构建时配置此
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<compilerId>eclipse</compilerId>
<compilerVersion>1.6</compilerVersion>
<source>1.6</source>
<target>1.6</target>
<optimize>true</optimize>
</configuration>
<dependencies>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-compiler-eclipse</artifactId>
<version>2.2</version>
</dependency>
</dependencies>
</plugin>
Run Code Online (Sandbox Code Playgroud)在 pom.xml 的构建部分的插件部分