3

我想将我们的一个项目从 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.javacom.google.common.collectcom.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
}
4

3 回答 3

2

I got the same issue today, and fixed it by adding this to my gradle build file:

tasks.withType(JavaCompile) {
    options.compilerArgs += ["-sourcepath", ""]
}

Thanks for Peter's answer in this post: http://forums.gradle.org/gradle/topics/compilation_fails_when_i_use_guavas_optional_class_duplicate_class_com_google_common_collect_abstractiterator

于 2015-01-11T08:11:01.573 回答
0

一些 GWT Jars 包含类源。默认情况下,javac 将尝试编译类路径上的源代码,这可能就是您收到此错误的原因。这应该解决它:

tasks.withType(JavaCompile) {
    options.compilerArgs << '-implicit:none'
}

PS:您的常春藤回购声明正在覆盖它自己的 URL。相反,您需要声明两个 Ivy 存储库。

于 2013-06-14T13:52:27.140 回答
0

I also came across this error when building a GWT project with maven on ubuntu. A colleague did some research and found the following compiler arg solves the issue:

-Xprefer:newer
于 2014-03-20T07:20:14.980 回答