【问题标题】:gradlew change jcenter() URL for React Native buildgradlew 更改 React Native 构建的 jcenter() URL
【发布时间】:2018-08-20 16:23:37
【问题描述】:

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

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

那么有办法用 gradle 覆盖 jcenter URL 吗?

我在我的用户主页中尝试了init scriptinit.gradle(灵感来自 doc):

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 存储库...

【问题讨论】:

    标签: android react-native gradle gradlew jcenter


    【解决方案1】:

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

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

    【讨论】:

    • 谢谢! project.buildscript.repositories 是我想念的把戏。
    猜你喜欢
    • 2016-04-13
    • 2019-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-18
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多