Gradle 是否支持为一个编译时依赖项定义不同的存储库?

Gok*_*oku 2 gradle

下面是我的 gradle 文件的片段,我想要实现的是,对于依赖项工件 - com.google.code.gson:2.8.2 来说,应该从另一个存储库中获取,而不是从存储库中提到的存储库中获取部分 ?是否可以 ?

比如说, com.google.code.gson:2.8.2 应该仅从 https://internal-repo-url/artifactory/second-repo 获取,而不是 https://internal-repo-url/artifactory/first-repo 。

buildscript {
    repositories {
        mavenLocal()
        maven {
            url 'https://internal-repo-url/artifactory/first-repo'
        }
    }

apply plugin: 'com.choko-build'

dependencies {

    compile group: 'org.influxdb', name: 'influxdb-java', version: '2.20'
    compile ('org.repo.strung:javacontainer:1.8.16') {
        exclude group: 'org.apache.logging.log4j', module: 'log4j-slf4j-impl'
        exclude group: 'com.google.code.gson', module: 'gson'
    }
    compile group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.5.13'
    compile group: 'com.google.code.gson', name: 'gson', version: '2.8.2'
Run Code Online (Sandbox Code Playgroud)

tho*_*est 5

是的,这可以通过 Gradle 的存储库过滤机制实现:声明仅在一个存储库中找到的内容

例如(未经测试):

repositories {
    // This repository will _not_ be searched for artifact "com.google.code.gson:gson:2.8.2"
    // despite being declared first
    mavenCentral()
    exclusiveContent {
        forRepository {
            maven {
                url 'https://internal-repo-url/artifactory/second-repo'
            }
        }
        filter {
            // this repository *only* contains artifacts with group "com.google.code.gson:gson:2.8.2"
            includeVersion "com.google.code.gson", "gson", "2.8.2"
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

查看存储库内容过滤以了解其他可用选项。