How can the gradle plugin repository be changed?
Asked Answered
P

4

62

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!

Plectognath answered 17/5, 2016 at 20:11 Comment(3)
FYI, the ability to do exactly this is coming in Gradle 2.14 whose first release candidate should be out this week.Runthrough
Heads up. The release candidate was published this morning. gradle.org/release-candidateRunthrough
If its have fix for this?Hamachi
I
38

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'
  }
}
Itchy answered 24/5, 2017 at 9:27 Comment(1)
is there no way to do this in build.gradle?Saleable
L
14

The plugin syntax has changed a bit since the latest version of Gradle, and the correct syntax for Gradle 4.x is now:

pluginManagement {
  repositories {
      maven {
        url 'maven-repo'
      }
      gradlePluginPortal()
      ivy {
        url 'ivy-repo'
      }
  }
}

So, for example, this settings.gradle would use your internal Nexus mirror:

pluginManagement {
  repositories {
    maven {
      url 'https://internal.repo.corporate/repository/gradle-proxy/'
    }
  }
}

rootProject.name = 'My Project Name'

More information can be found in the Gradle plugin documentation.

Lycaon answered 24/10, 2018 at 22:30 Comment(0)
M
10

To apply the setting globally, it can be added to USER_HOME/.gradle/init.gradle as follows:

allprojects {       
    repositories {
        mavenLocal()
        maven { url "https://artifactory.mycompany.com/artifactory/maven-repo" }
    }       
}   

settingsEvaluated { settings ->
    settings.pluginManagement {
        repositories {
            mavenLocal()
            maven { url "https://artifactory.mycompany.com/artifactory/maven-repo" }
        }
    }
}
Mchugh answered 12/11, 2018 at 19:48 Comment(3)
Failed for flutter-plugin, for example, apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle" did still try to download the gradle-3-2-1.jar file; but extending that plugin's flutter.gradle file like buildscript { repositories { maven { url uri(offlineRepoDir) } } } works ???Deserted
User-level configs should be used for preferential settings (as a general rule, not just for Gradle). Critical configs like this should be stored with the project.Unfruitful
For those who needs to solve this with KotlinScript DSL, see the discussion at github.com/gradle/gradle/issues/11885Emeritaemeritus
H
2

It`s need to define pluginRepositories in settings Gradle

https://docs.gradle.org/current/userguide/plugins.html#sec:custom_plugin_repositories

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'
  }
}
Hamachi answered 1/4, 2017 at 12:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.