首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为多个配置指定Gradle版本约束

为多个配置指定Gradle版本约束
EN

Stack Overflow用户
提问于 2020-06-19 18:57:29
回答 1查看 256关注 0票数 2

我有一个多模块的Gradle 6.5项目,其中还包括一些测试夹具代码。为了避免依赖问题,我想在一个地方设置(和维护)版本,并且只引用各个模块中的无版本依赖关系:

代码语言:javascript
复制
subprojects {
    apply plugin: 'java'

    dependencies {
        implementation 'com.google.guava:guava'

        constraints {
            implementation 'com.google.guava:guava:20.0'
            compileOnly 'com.google.code.findbugs:jsr305:3.0.2'
        }
    }
}

现在,如果模块包含compilyOnly 'com.google.code.findbugs:jsr305',则根据上面的约束获取版本3.0.2。如果我知道配置(implementationcompileOnly等),就可以很好地工作。

现在的问题是:如何指定适用于所有配置的版本?如果一个模块决定对测试夹具代码使用JSR305注释怎么办?testFixtures 'com.google.code.findbugs:jsr305'失败了,因为没有在任何地方指定版本。我也认为为所有(可能的)配置重复版本规范是一个坏主意:

代码语言:javascript
复制
implementation 'com.google.code.findbugs:jsr305:3.0.2'
testFixturesImplementation 'com.google.code.findbugs:jsr305:3.0.2'
compileOnly 'com.google.code.findbugs:jsr305:3.0.2'

有什么捷径可以做到吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-06-19 21:39:47

如果使用java-platform plugin修复此问题。

平台:

代码语言:javascript
复制
plugins {
    id 'java-platform'
}

dependencies {
    constraints {
        api 'com.google.guava:guava:20.0'
        api 'com.google.code.findbugs:jsr305:3.0.2'
    }
}

带有测试夹具的常规模块:

代码语言:javascript
复制
plugins {
    id "java-test-fixtures"
}

dependencies {
    testFixturesImplementation platform(project(':platform-module'))
    testFixturesCompileOnly 'com.google.code.findbugs:jsr305'
}

./gradlew -q module-with-test-fixtures:dependencies提供了:

代码语言:javascript
复制
[...]
testFixturesCompileClasspath - Compile classpath for source set 'test fixtures'.
+--- project :module-with-test-fixtures (*)
+--- com.google.code.findbugs:jsr305 -> 3.0.2
\--- project :platform-module
     \--- com.google.code.findbugs:jsr305:3.0.2 (c)
[...]
(c) - dependency constraint
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62468768

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档