gradle - 次要Gradle升级中断测试编译

标签 gradle groovy

我想逐步升级Gradle包装器,以提高构建速度。从2.3迁移到2.4后,测试编译失败,并出现incompatible types错误,我正在为依赖项而苦苦挣扎。

考虑一下此Spock测试:

class DetailsSortKeySpec extends Specification {
    def 'Simple Test'() {
        given:
        TestDetailsSortKey testDetailsSortKey = new TestDetailsSortKey(details, collationKey)

        expect:
        testDetailsSortKey.details.equals(details)

        where:
        details                | collationKey
        new TestDetails(id: 0) | Collator.getInstance(Locale.ENGLISH).getCollationKey('')
    }

    private class TestDetailsSortKey extends DetailsSortKey<TestDetails> {
        TestDetailsSortKey(TestDetails details, CollationKey collationKey) {
            super(details, collationKey)
        }
    }
}

这个Java类:

public class DetailsSortKey<T extends Details> {

    private final T details;
    private final CollationKey collationKey;

    public DetailsSortKey(final T details, final CollationKey collationKey) {
        this.details = details;
        this.collationKey = Objects.requireNonNull(collationKey);
    }

    public final T getDetails() {
        return details;
    }
}

运行compileTestGroovy时出现以下错误消息:
/var/lib/jenkins/workspace/gradle_upgrade/build/tmp/compileTestGroovy/groovy-java-stubs/com/vendor/transfer/sorting/DetailsSortKeySpec.java:25: error: incompatible types: Details cannot be converted to TestDetails
super ((com.vendor.common.transfer.Details)null, (java.text.CollationKey)null);
       ^
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
1 error
startup failed:
Compilation failed; see the compiler error output for details.

1 error

 FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':WEB-Commons:compileTestGroovy'.
> Compilation failed; see the compiler error output for details.

Release Notes没有列出似乎对此负责的任何问题,因此我仔细研究了Groovy Issues,这可能是根本原因,并且确实找到了issue about Generics。但是该错误应该已经修复。我不明白在哪里以及为何尝试将Details隐蔽为TestDetails,这只是Details类的简单而空的派生。

这些是两个Gradle发行版中使用的Groovy版本:

Gradle 2.3 :Groovy:2.3.9

Gradle 2.4 :Groovy 2.3.10

在我看来,这似乎完全像所引用的Groovy错误一样,但是应该从2.3.8开始就修复该错误,并且不应该影响此构建。此外,在项目中声明依赖项如下:
dependencies {
    // mandatory dependencies for using Spock
    compile "org.codehaus.groovy:groovy-all:2.4.11"
    testCompile "org.spockframework:spock-core:1.1-groovy-2.4"
}

两个gradlew :dependencies的输出之间的差异仅包含发行说明中提到的工具,但不属于测试范围。

最终,这里使用哪个Groovy版本?

最佳答案

我认为您希望使用与Gradle bundle 在一起的版本不同的Groovy进行编译。参见GroovyCompile.groovyClasspath

例如:

configurations {
    groovy
}
dependencies {
    groovy 'org.codehaus.groovy:groovy-all:2.4.11'
    compile 'org.codehaus.groovy:groovy-all:2.4.11'
    testCompile('org.spockframework:spock-core:1.0-groovy-2.4') {
        exclude module: 'groovy-all'
    }
}
compileGroovy {
    groovyClasspath = configurations.groovy
}

*编辑*

要查看选择了哪个版本(由于依赖关系解析),您可以
gradle dependencies

或(其中xxx是子项目名称)
gradle xxx:dependencies

要强制特定的依赖版本,您可以
configurations.all {
    resolutionStrategy {
        force 'org.codehaus.groovy:groovy-all:2.4.11'
    }   
}

参见ResolutionStrategy

关于gradle - 次要Gradle升级中断测试编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48383397/

相关文章:

android - 使用 Gradle 在 IDEA 中设置项目

java - 我无法导入 gradle 项目 Eclipse。 "Finish"按钮什么都不做

java - 映射字符串数组列表获取键和值

android - Android Gradle Read超时

gradle - Gradle:来自文件的项目依赖项

java - 我正在尝试安装Apache Gobblin。如何使用Gradle安装它?

java - 我如何获取spock中设置方法中的方法?

Grails 简单插件 "not a valid plugin"

java - gmongo 的重复项也不异常(exception)

security - Groovy/Grails:有没有办法使.evaluate()完全安全?