如何更改 gradle 插件存储库? [英] How can the gradle plugin repository be changed?

查看:39
本文介绍了如何更改 gradle 插件存储库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一家大公司工作,有严格的政策禁止未经过滤地使用外部库.我们必须从受祝福的公司存储库中提取所有内容,而不是从裸互联网(包括 gradle.org)中提取所有内容.

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.

使用 gradle 的原始应用插件语法,结合 buildscript 块,我可以将(受祝福的)插件添加到我们的存储库中并在构建中使用它们.换句话说:

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"

相反,我希望能够使用新的插件 DSL,即

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

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

(我意识到需要在某处定义私有 repo url)

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

新的插件语法总是转到 gradle.org 并且似乎没有任何方法提供备用下载 url.有人知道方法吗?

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?

我仔细查看了文档和 Internet,但找不到答案.如果答案很明显,我们深表歉意.

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

非常感谢!

推荐答案

Gradle 3.5 和(大概)以后

Gradle 3.5 有一个新的(incubating)功能,允许更好地控制插件依赖解析,使用pluginManagement DSL:

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:

插件解析规则允许你修改插件请求plugins {} 块,例如更改请求的版本或明确指定实现工件坐标.

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.

要添加解析规则,请使用 resolutionStrategy {}pluginManagement {} 块:

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

示例 27.6.插件解析策略.

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

这告诉 Gradle 使用指定的插件实现工件而不是使用其内置的默认映射从插件 ID 到Maven/Ivy 坐标.

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.

pluginManagement {} 块只能出现在 settings.gradle文件,并且必须是文件中的第一个块.自定义 Maven 和 Ivy插件存储库还必须包含插件标记工件到实际实现插件的工件.

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.

pluginManagement 中的 repositories 块与之前版本中的 pluginRepositories 块的工作方式相同.

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

在 Gradle 3.5 之前,您必须定义 pluginRepositories 在您的 settings.gradle 中,如 sytolk 的回答中所述:

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

pluginRepositories {} 块可能只出现在 settings.gradle 中文件,并且必须是文件中的第一个块.

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

这篇关于如何更改 gradle 插件存储库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆