How can the gradle plugin repository be changed?

Tony Pierce picture Tony Pierce · May 17, 2016 · Viewed 23.5k times · Source

I work at a big company with a strict policy forbidding the unfiltered use of outside libraries. We have to pull everything from blessed corporate repositories, and not the bare internet, including gradle.org.

Using gradle's original apply plugin syntax, in conjunction with a buildscript block, I can add (blessed) plugins to our repo and use them in builds. In other words:

buildscript {
    repositories {
        maven { url "https://privaterepo.myemployer.com" }
    }

    dependencies {
        // various dependencies
        classpath "org.something:some-plugin:1.0"
        ...
    }

apply plugin: "someplugin"

Instead, I want to be able to use the new plugins DSL, i.e.

plugins {
    id 'org.something.some-plugin' version '1.0'
}

(I realize that the private repo url would need to be defined somewhere)

The new plugin syntax always goes to gradle.org and does not appear to have any means to provide an alternate download url. Does anyone know of a way?

I've scrutinized the documentation and the Internet and can't find the answer. Apologies if the answer is completely obvious.

Many Thanks!

Answer

willyjoker picture willyjoker · May 24, 2017

Gradle 3.5 and (presumably) later

Gradle 3.5 has a new (incubating) feature, allowing finer control of the plugin dependency resolution, using the pluginManagement DSL:

Plugin resolution rules allow you to modify plugin requests made in plugins {} blocks, e.g. changing the requested version or explicitly specifying the implementation artifact coordinates.

To add resolution rules, use the resolutionStrategy {} inside the pluginManagement {} block:

Example 27.6. Plugin resolution strategy.

settings.gradle
pluginManagement {
  resolutionStrategy {
      eachPlugin {
          if (requested.id.namespace == 'org.gradle.sample') {
              useModule('org.gradle.sample:sample-plugins:1.0.0')
          }
      }
  }
  repositories {
      maven {
        url 'maven-repo'
      }
      gradlePluginPortal()
      ivy {
        url 'ivy-repo'
      }
  }
}

This tells Gradle to use the specified plugin implementation artifact instead of using its built-in default mapping from plugin ID to Maven/Ivy coordinates.

The pluginManagement {} block may only appear in the settings.gradle file, and must be the first block in the file. Custom Maven and Ivy plugin repositories must contain plugin marker artifacts in addition to the artifacts which actually implement the plugin.

The repositories block inside pluginManagement works the same as the pluginRepositories block from previous versions.


Prior to Gradle 3.5

Prior to Gradle 3.5, you had to define the pluginRepositories block in your settings.gradle, as explained in sytolk's answer:

The pluginRepositories {} block may only appear in the settings.gradle file, and must be the first block in the file.

pluginRepositories {
  maven {
    url 'maven-repo'
  }
  gradlePluginPortal()
  ivy {
    url 'ivy-repo'
  }
}