我想将我们的一个项目从 ant+ivy 迁移到 gradle。当我使用 gradle 编译源代码时,出现此错误:
...\guava-gwt-14.0.1.jar(com/google/common/collect/AbstractIterator.java):64: error: duplicate class: com.google.common.collect.AbstractIterator
public abstract class AbstractIterator<T> extends UnmodifiableIterator<T> {
^
...\guava-gwt-14.0.1.jar(com/google/common/base/Optional.java):223: error: cannot access AbstractIterator
return new AbstractIterator<T>() {
^
bad source file: ...\guava-gwt-14.0.1.jar(com/google/common/base/AbstractIterator.java)
file does not contain class com.google.common.base.AbstractIterator
Please remove or make sure it appears in the correct subdirectory of the sourcepath.
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
2 errors
我检查了它AbstractIterator.java
是com.google.common.collect
和com.google.common.base
(如错误所写)。如何解决这个问题呢?
[更新]
这是我的 gradle 构建(非常简单 - 这是从 ant 迁移到 gradle 的开始)
apply plugin: 'java'
compileJava.options.encoding = 'ISO-8859-1'
repositories {
mavenCentral()
maven {
url 'http://gwtquery-plugins.googlecode.com/svn/mavenrepo'
}
ivy {
url 'http://ivyrep/shared'
url 'http://ivyrep/public'
layout "pattern", { artifact "[organisation]/[module]/[revision]/[type]s/[artifact].[ext]" }
}
}
// I know that conigurations should be repaired (testCompile, runtime) but this is just the beggining of migration from ant to gradle
dependencies {
compile 'net.sourceforge.cobertura:cobertura:1.9.4.1'
compile 'com.google.gwt:gwt-servlet:2.5.0'
compile 'com.google.gwt:gwt-user:2.5.0'
compile 'com.google.gwt:gwt-dev:2.5.0'
compile 'com.google.gwt.inject:gin:2.0.0'
compile 'com.googlecode.gwtquery:gwtquery:1.3.2'
compile 'com.googlecode.gwtquery.bundles:gquery-dnd-bundle:1.0.6'
compile 'org.hamcrest:hamcrest-library:1.3'
compile 'org.mockito:mockito-all:1.9.5'
compile 'junit:junit:4.10'
compile 'org.easytesting:fest-assert-core:2.0M10'
compile 'xmlunit:xmlunit:1.3'
compile 'org.reflections:reflections:0.9.9-RC1'
}
我还应该补充一点,这是一个GWT 项目。
[更新]
问题解决了。我已经从编译范围中排除了 guava-gwt 并且它开始工作了。这可能不是最好的解决方案,但它确实有效。
apply plugin: 'java'
configurations { guavaGwt }
dependencies {
guavaGwt 'com.google.guava:guava-gwt:14.0.1'
// other dependencies
}
task compileGwt (dependsOn: classes) << {
// [...]
javaexec {
main = 'com.google.gwt.dev.Compiler'
maxHeapSize = '512M'
classpath {
[
sourceSets.main.java.srcDirs,
sourceSets.main.output.resourcesDir,
sourceSets.main.output.classesDir,
sourceSets.main.compileClasspath,
configurations.guavaGwt, // USE guava-gwt
]
}
args =
[
moduleName,
'-war',
buildDir,
'-logLevel',
'INFO',
// '-draftCompile' // Speeds up compile with 25%
]
}
// [...]
}
compileJava {
configurations.compile.exclude module:'guava-gwt' // exclude guava-gwt
// do the job
}