0

When building our Cordova Android application, the build fails because of failing connections to https://repo.maven.apache.org/maven2

e.g.

Could not get resource 'https://repo.maven.apache.org/maven2/io/github/g00fy2/versioncompare/1.4.1/versioncompare-1.4.1.pom'.

We sit behind a company network and all the dependencies are available via https://mycompany.com

We struggle to set the gradle repositories globally to https://mycompany.com

the init.gradle seems to not affect the gradle repos in de Cordova project.

init.gradle content

allprojects {
    buildscript {
        repositories {
            maven {
                url "https://mycompany.com"
                allowInsecureProtocol = false
            }
            google()
        }
    }
    
    repositories {
        maven {
            url "https://mycompany.com"
            allowInsecureProtocol = false
        }
        google()
    }
}

I managed to build successfully by hardcoding the repos in this files

  • platforms/android/repositories.gradle
  • platforms/android/app/repositories.gradle
  • platforms/android/CordovaLib/repositories.gradle

with this content

ext.repos = {
    maven {
        url "https://mycompany.com"
        allowInsecureProtocol = false
    }
    google()
}

Of course, this is not a great solution.

Is there a way to enforce the gradle repos in a Cordova project?

I tried the build-extras.gradle approach as described here, but the build still tries to connect to https://repo.maven.apache.org/maven2

System info:

  • win 11

  • cordova android version: 15.0.0-nightly.20251110002700134.sha.655aa0a5, but similar issue with 14.0.1

  • gradle version 9.2.0

2
  • Your hardcoded repositories.gradle files are actually the recommended workaround for Cordova behind corporate networks many teams and devs do this. For a slightly cleaner approach, try putting the allprojects block in platforms/android/build-extras.gradle, though the hardcoded files are often more reliable with Cordova. github.com/apache/cordova-android/issues/1647 Commented Nov 11 at 11:08
  • 1
    Hi, what a nice question. Good ol' Cordova, still active and running the show. Two questions: 1. where on your file system did you place the init.gradle file? 2. What is your $GRADLE_USER_HOME location? Gradle is a mistery for Hybrid developers. Can be Gradle stuff. Reference: - Cordova docs about Gradle - Gradle docs Commented Nov 14 at 14:56

1 Answer 1

0

this init.gradle redirects all repos to the company url

inspired by this script from the gradle docs: https://docs.gradle.org/current/userguide/init_scripts.html#sec:init_script_plugins

the init.gradle file is normally located in userfolder/.gradle

apply plugin: EnterpriseRepositoryPlugin 

class EnterpriseRepositoryPlugin implements Plugin<Gradle> {

    private static String ENTERPRISE_REPOSITORY_URL = "https://mycompany.com"

    final static String LOG_PREFIX = "init.gradle/EnterpriseRepositoryPlugin:"

    final Closure EnterpriseRepoConfig = {
        maven {
            name = "STANDARD_ENTERPRISE_REPO"
            url = ENTERPRISE_REPOSITORY_URL
        }
    }

    final Closure RepoHandler = {
        // Remove all repositories not pointing to the enterprise repository url
        all { ArtifactRepository repo ->
            if (!(repo instanceof MavenArtifactRepository) ||
                  repo.url.toString() != ENTERPRISE_REPOSITORY_URL) {
                println "$LOG_PREFIX Repository ${repo.url} removed. Only $ENTERPRISE_REPOSITORY_URL is allowed"
                remove repo
            }
        }
    }


    void apply(Gradle gradle) {
        // Override all project specified Maven repos with standard
        // defined in here
        gradle.allprojects { project ->
            println "$LOG_PREFIX Reconfiguring repositories."
            project.repositories RepoHandler
            project.buildscript.repositories RepoHandler

            project.repositories EnterpriseRepoConfig
            project.buildscript.repositories EnterpriseRepoConfig
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.