How can the gradle plugin repository be changed?

人走茶凉 提交于 2019-11-29 02:57:29
willyjoker

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

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

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.

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'
  }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!