Gradle插件仓库如何更改?

59

我在一家有严格政策禁止使用未过滤的外部库的大公司工作。我们必须从公司指定的代码仓库中拉取所有东西,而不是从裸露的互联网上,包括gradle.org。

使用gradle的原始apply插件语法,结合buildscript块,我可以将(经过认可的)插件添加到我们的代码仓库中并在构建中使用它们。换句话说:

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

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

apply plugin: "someplugin"

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

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

我意识到私有存储库的网址需要在某处定义。

新插件语法总是前往gradle.org,似乎没有提供替代下载网址的方法。有人知道吗?

我已经仔细查阅了文档和互联网,但找不到答案。如果答案显而易见,请原谅。

非常感谢!


2
FYI,能够完全做到这一点的能力将在Gradle 2.14中实现,其第一个发布候选版应该在本周发布。 - Mark Vieira
注意了,今天早上发布了发行候选版。http://gradle.org/release-candidate/ - Mark Vieira
1
这个问题有解决方法吗? - sytolk
4个回答

35

Gradle 3.5和(可能)更高版本

Gradle 3.5有一个新的(孵化)功能,允许更精细地控制插件依赖项的解析,使用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.

pluginManagement内的repositories块与早期版本中的pluginRepositories块相同。

在Gradle 3.5之前

在Gradle 3.5之前,你需要在settings.gradle中定义pluginRepositories块,如sytolk的回答所述:

define the pluginRepositories block

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

1
在 build.gradle 中难道没有其他方法实现吗? - kbolino

10

自从Gradle的最新版本以来,插件语法发生了一些变化,适用于Gradle 4.x的正确语法现在是:

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

例如,这个 settings.gradle 文件会使用内部 Nexus 镜像:

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

rootProject.name = 'My Project Name'

有关更多信息,请参阅Gradle插件文档


8
为了全局应用该设置,可以将其 添加到USER_HOME/.gradle/init.gradle 中,如下所示:
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" }
        }
    }
}

Flutter插件失败,例如apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"仍然尝试下载gradle-3-2-1.jar文件;但是通过扩展该插件的flutter.gradle文件,如buildscript { repositories { maven { url uri(offlineRepoDir) } } }可以解决问题吗? - Top-Master
用户级别的配置应该用于偏好设置(通常情况下,不仅仅是针对Gradle)。像这样关键的配置应该与项目一起存储。 - TastyWheat
对于那些需要使用KotlinScript DSL解决此问题的人,请参见https://github.com/gradle/gradle/issues/11885上的讨论。 - Zaur Nasibov

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接