1
votes

I trying to build a React Native application for android but in my enterprise, external repositories are blocked (external network not allowed).

So we use an internal maven repository to get artifacts. But I encountered some problems to build a react native application because the project with node modules dependencies, have native android code to build that refers to jcenter() repository (I don't want to override node_modules/react*/android/build.gradle manually).

So there is a way to override jcenter URL with gradle ?

I have try init script init.gradle in my user home (inspired from 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
                }
            }
        }
    }
}

But when I start gradlew, I get this output :

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

Plugin doesn't seems to intercept jcenter repository...

1

1 Answers

1
votes

In your init script, you are setting contraints on the Project's repositories. But as you can see in your error message, Gradle is still trying to download some dependencies from 'https://repo1.maven.org/...' and not from your enterprise repo : I guess these are dependencies for the plugins you are applying to your build script. So you have to configure project's buildscript repositories, in addition to project's dependencies repositories. Following code should work (but I could not test ):

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

}