Change Gradle mavenCentral URL to My repo

20k views Asked by At

While working with Gradle in my country, download jars from maven central is a very timely-costed work.
I wanna change the mavenCentral to maven-nexus:
which means whenever I use mavenCentral it always points to oschina

------edit------
I have lots of projects with mavenCentral, so I do not want change each file.

Now in new scripts I use maven{ url ...} Any easy way??

Any one can hlep?
Thanks!

4

There are 4 answers

0
Rene Groeschke On BEST ANSWER

The easiest way to apply this change for all projects is to use an gradle init script that forces the use of the oschina repository instead of mavenCentral. you can put this:

allprojects{
    repositories {
        all { ArtifactRepository repo ->
            println repo.url.toString()
            if ((repo instanceof MavenArtifactRepository) && repo.url.toString().startsWith("https://repo1.maven.org/maven2")) {
                project.logger.warn "Repository ${repo.url} removed. Only $coporateRepoUrl is allowed"
                remove repo
            }   
        }
        maven { 
            url "http://maven.oschina.net/content/groups/public" 
        }
    }
}

into an gradle init file. Now you can use this by calling "gradle build -I yourInitFile.gradle" or you put this logic into a init.gradle file stored in your gradle home directory in USER_HOME/.gradle/ directory. Now this will be picked up by every gradle invocation without explicitly setting -I

Another option is to create a custom gradle distribution where this file is store in the init.d directory of the distribution.

4
Toochka On

Its easy! Instead of this:

repositories {
    mavenCentral()
}

Use this:

repositories {
    maven {
        url "https://..."
    }
}
1
erakitin On

Just set up the repository in build.gradle file in your module's directory, like this:

repositories {
    maven { url "http://maven.oschina.net/content/groups/public" }
}
2
Toochka On

If you have lots of dependencies (i.e. in all build.gradle files) and for repository urls you are using

repositories {
    maven {
        url "https://www.myrepourl.com"
    }
}

but you want to change that url in only one file then do the following:

  • Create gradle.properties file and there add this line: REPO_URL=https://www.myrepourl.com
  • In all other build.gradle files copy the following:

    repositories {
        maven {
            url project.REPO_URL
        }
    }
    

You can add & use more properties in this way. This worked like charm for me. Cheers!