【问题标题】:In gradle, how can a test configuration declare a dependency upon another project's test configuration?在 gradle 中,测试配置如何声明对另一个项目的测试配置的依赖?
【发布时间】:2020-08-05 20:04:57
【问题描述】:

在 gradle 5.6.2 中,我有一个带有子项目的项目。子项目中的测试共享很多代码,所以我希望子项目测试依赖于父项目测试。但是测试中的引用无法解析,即使主项目中的引用可以解析。 Gradle 文档说使用“map notation”依赖声明,并且这个依赖块不会产生错误:

dependencies {
    api project(":henchbot-api")
    testImplementation project(path: ':henchbot-api', configuration: 'testRuntime')
}

...但是测试不会编译。如果我将 testRunTime 更改为 testRuntimeClasspath 则 gradle 无法解决该项目依赖关系。

【问题讨论】:

    标签: testing gradle dependencies classpath


    【解决方案1】:

    您还需要声明测试工件 - 请参阅 https://docs.gradle.org/current/userguide/multi_project_builds.html#sec:project_jar_dependencies

    在项目henchbot-api

    configurations {
        testLib
    }
    
    task testLibJar(type: Jar) {
        archiveBaseName = archivesBaseName + '-testLib'
        from sourceSets.test.output
    }
    
    artifacts {
        testLib testLibJar
    }
    

    在另一个 - 依赖是

    testImplementation project(path: ':henchbot-api', configuration: 'testLib')
    

    【讨论】:

    • 谢谢!这行得通,我将三个块添加到我的子项目块中。是否有需要创建新配置的原因?为内置测试配置定义工件是否存在问题?
    • 这不是一个 gradle 错误,当项目依赖声明无法读取“testRuntime”配置所需但不存在的工件时,不会引发异常?
    猜你喜欢
    • 2015-04-29
    • 2016-10-29
    • 1970-01-01
    • 2016-06-12
    • 2022-09-29
    • 1970-01-01
    • 2020-02-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多