Goku
Goku

Reputation: 534

Does Gradle support defining a different repo for one single compile time dependency?

Below is the snippet of my gradle file and what I'm trying to achieve is that, for the dependency artifact - com.google.code.gson:2.8.2 alone, should be fetched from a different repo instead of one mentioned under repositories section ? Is it possible ?

Say, com.google.code.gson:2.8.2 should be fetched only from https://internal-repo-url/artifactory/second-repo instead of 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'

Upvotes: 2

Views: 1341

Answers (1)

Thomas K.
Thomas K.

Reputation: 6770

Yes, this is possible via Gradle's repository filtering mechanism: Declaring content exclusively found in one repository.

For example (untested):

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"
        }
    }
}

Have a look at Repository content filtering for other available options.

Upvotes: 5

Related Questions