Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can the gradle plugin repository be changed?

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!

like image 879
Tony Pierce Avatar asked May 17 '16 20:05

Tony Pierce


People also ask

How do I add a repository to Gradle?

We can add a Maven repository to our project by using its url address or its location by adding that particular code snippet in our Gradle build script. To add a Maven repository by using its url, add the following code snippet to the 'build. gradle' file: repositories {

How do I set the Gradle plugin version?

You can specify the Gradle version in either the File > Project Structure > Project menu in Android Studio, or update your Gradle version using the command line.

How do I add plugins to build Gradle?

While creating a custom plugin, you need to write an implementation of plugin. Gradle instantiates the plugin and calls the plugin instance using Plugin. apply() method. The following example contains a greeting plugin, which adds a hello task to the project.

Does Gradle have a repository?

Gradle can consume dependencies available in the local Maven repository. Declaring this repository is beneficial for teams that publish to the local Maven repository with one project and consume the artifacts by Gradle in another project. Gradle stores resolved dependencies in its own cache.


2 Answers

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'   } } 
like image 55
willyjoker Avatar answered Sep 28 '22 00:09

willyjoker


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.

like image 33
Erik Pragt Avatar answered Sep 28 '22 00:09

Erik Pragt