gradlew change jcenter() URL 用于 React Native 构建

gradlew change jcenter() URL for React Native build

我试图为 android 构建 React Native 应用程序,但在我的企业中,外部存储库被阻止(不允许外部网络)。

所以我们使用内部 Maven 存储库来获取工件。 但是我在构建本机应用程序时遇到了一些问题,因为具有节点模块依赖项的项目具有本机 android 代码来构建,该代码引用 jcenter() 存储库(我不想覆盖 node_modules/react*/android/build.gradle 手动).

所以有一种方法可以用 gradle 覆盖 jcenter URL ?

我已经在我的用户主页中尝试 init script init.gradle(灵感来自文档):

apply plugin:EnterpriseRepositoryPlugin

class EnterpriseRepositoryPlugin implements Plugin<Gradle> {

    private static String ENTERPRISE_REPOSITORY_URL = "https://my.enterprise.repo"

    void apply(Gradle gradle) {
        // ONLY USE ENTERPRISE REPO FOR DEPENDENCIES
        gradle.allprojects{ project ->
            project.repositories {

                // Remove all repositories not pointing to the enterprise repository url
                all { ArtifactRepository repo ->
                    project.logger.lifecycle "DEBUG : Repository ${repo.url}"
                    if (!(repo instanceof MavenArtifactRepository) ||
                          repo.url.toString() != ENTERPRISE_REPOSITORY_URL) {
                        project.logger.lifecycle "Repository ${repo.url} removed. Only $ENTERPRISE_REPOSITORY_URL is allowed"
                        remove repo
                    }
                }

                // add the enterprise repository
                jcenter {
                    name "BintrayJCenter"
                    url ENTERPRISE_REPOSITORY_URL
                }
            }
        }
    }
}

但是当我开始 gradlew 时,我得到了这个输出:

DEBUG : Repository https://my.enterprise.repo
DEBUG : Repository https://my.enterprise.repo
DEBUG : Repository https://my.enterprise.repo
DEBUG : Repository https://my.enterprise.repo
DEBUG : Repository https://my.enterprise.repo
DEBUG : Repository https://my.enterprise.repo
DEBUG : Repository https://my.enterprise.repo

FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring root project 'AppliTest'.
> Could not resolve all dependencies for configuration ':classpath'.
   > Could not resolve com.android.tools.build:gradle:2.2.3.
     Required by:
         :AppliLabChatbot:unspecified
      > Could not resolve com.android.tools.build:gradle:2.2.3.
         > Could not get resource 'https://repo1.maven.org/maven2/com/android/tools/build/gradle/2.2.3/gradle-2.2.3.pom'.
            > Could not HEAD 'https://repo1.maven.org/maven2/com/android/tools/build/gradle/2.2.3/gradle-2.2.3.pom'.
               > repo1.maven.org
      > Could not resolve com.android.tools.build:gradle:2.2.3.
         > Could not get resource 'https://jcenter.bintray.com/com/android/tools/build/gradle/2.2.3/gradle-2.2.3.pom'.
            > Could not HEAD 'https://jcenter.bintray.com/com/android/tools/build/gradle/2.2.3/gradle-2.2.3.pom'.
               > jcenter.bintray.com
   > Could not resolve de.undercouch:gradle-download-task:3.1.2.
     Required by:
         :AppliLabChatbot:unspecified
      > Could not resolve de.undercouch:gradle-download-task:3.1.2.
         > Could not get resource 'https://repo1.maven.org/maven2/de/undercouch/gradle-download-task/3.1.2/gradle-download-task-3.1.2.pom'.
            > Could not HEAD 'https://repo1.maven.org/maven2/de/undercouch/gradle-download-task/3.1.2/gradle-download-task-3.1.2.pom'.
               > repo1.maven.org
      > Could not resolve de.undercouch:gradle-download-task:3.1.2.
         > Could not get resource 'https://jcenter.bintray.com/de/undercouch/gradle-download-task/3.1.2/gradle-download-task-3.1.2.pom'.
            > Could not HEAD 'https://jcenter.bintray.com/de/undercouch/gradle-download-task/3.1.2/gradle-download-task-3.1.2.pom'.
               > jcenter.bintray.com

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

插件似乎没有拦截 jcenter 存储库...

在您的初始化脚本中,您正在对项目的存储库设置约束。但是正如您在错误消息中看到的那样,Gradle 仍在尝试从“https://repo1.maven.org/...”而不是从您的企业存储库下载一些依赖项:我猜这些是您使用的插件的依赖项适用于您的构建脚本。 因此,除了项目的依赖项存储库之外,您还必须配置项目的构建脚本存储库。 以下代码应该可以工作(但我无法测试):

apply plugin: EnterpriseRepositoryPlugin

class EnterpriseRepositoryPlugin implements Plugin<Gradle> {

    private static String ENTERPRISE_REPOSITORY_URL = "https://repo.gradle.org/gradle/repo"

    def configRepositories = { ->
        // Remove all repositories not pointing to the enterprise repository url
        all { ArtifactRepository repo ->
            if (!(repo instanceof MavenArtifactRepository) ||
                    repo.url.toString() != ENTERPRISE_REPOSITORY_URL) {
                println "Repository ${repo.name} removed. Only $ENTERPRISE_REPOSITORY_URL is allowed"
                remove repo
            }
        }
        // add the enterprise repository
        jcenter {
            name "BintrayJCenter"
            url ENTERPRISE_REPOSITORY_URL
        }
    }

    void apply(Gradle gradle) {    

        gradle.allprojects { project ->
            // ONLY USE ENTERPRISE REPO FOR DEPENDENCIES
            project.repositories(configRepositories)
            // Only use enterprise repo for plugins classpath as well.
            project.buildscript.repositories(configRepositories)
        }
    }

}