Home > Back-end >  How to substitute a locally unavailable repository in Gradle?
How to substitute a locally unavailable repository in Gradle?

Time:08-03

For a project I need to get a JAR from a repository that is only available on customers network. As this isn't available on public repositories and I locally don't have access to the customers network, I want to substitute this repository locally.

I copied the jar into a local repository and declared it with:

maven {
    url = uri("${project.getRootDir()}/local_repo")
}

That works fine, however I want to keep the build files and logic the same regardless if I run it locally or on the customers system.

According to Gradle Docs repositories get called in the order they are declared. So according to this logic I first declare MavenCentral, then the customers repository (that is unavailable locally) and then my own local substitute repository:

mavenCentral()
maven {
    url = uri(<costumer_repo>)
}
maven {
    url = uri("${project.getRootDir()}/local_repo")
}

But it seems now gradle stops when it can't reach the unavailable repo and therefore doesn't find the JAR in the local repo. If I turn around the local and the unavailable repo, it works again, but this way it always uses the local repo even on the customer system according to the gradle docs.

Is there a way to tell gradle to keep looking at the other repos even if one is unavailable, or is there a different way to substitute an unavailable repo?

CodePudding user response:

Try this, add a flag like localRepo to your gradle.properties when you build locally, then you can use an if statement for your repositories block, see below as an example:

repositories {
    mavenCentral()
    if( project.hasProperty('localRepo') ){
        maven { url = uri("${project.getRootDir()}/local_repo") }
    } else {
        maven { url = uri(<costumer_repo>) }
    }
}

This ensures project is portable between systems

  • Related