Armand
Armand

Reputation: 24333

Is it possible to restrict a gradle repository to a particular configuration?

I want to tie gradle repositories to specific configurations in my build.gradle file, e.g.:

repositories {
    testCompile {
        mavenCentral()
    }
    compile {
        maven { url 'https://vetted-repo.example.com' }
    }
}

I can't find a simple way to do this from the gradle documentation. Do I need to write my own plugin?

Upvotes: 4

Views: 2517

Answers (3)

Adrian Baker
Adrian Baker

Reputation: 9999

As of Gradle 5.1 this is now possible, from the release notes:

It is now possible to match repositories to dependencies, so that Gradle doesn't search for a dependency in a repository if it's never going to be found there.

Example:

repositories {
  maven {
    url "https://repo.mycompany.com"
    content {
      includeGroupByRegex "com\\.mycompany.*"
    }
  }
}

For the specific requirement of restricting a repository to a specific configuration such as compile, you could use the onlyForConfigurations property:

repositories {
  maven { 
    url "https://vetted-repo.example.com"
    content {
      onlyForConfigurations "compile"
    }
  }
}

Upvotes: 5

Rene Groeschke
Rene Groeschke

Reputation: 28653

It is now possible to declare a repository filter in gradle. see the documentation at https://docs.gradle.org/current/userguide/declaring_repositories.html#declaring_a_repository_filter this does exactly what you need here.

Upvotes: 1

Rene Groeschke
Rene Groeschke

Reputation: 28653

This is not supported by gradle at the moment. When resolving dependencies, gradle tries al listed repositories (from top to bottom) to resolve a dependency. Once the dependency is found it stops looking for it in other repositories

Upvotes: 2

Related Questions