Home > front end >  Override ~/.gradle/init.gradle repository location
Override ~/.gradle/init.gradle repository location

Time:10-10

Is there a way to change the location of the default repositories in a projects build.gradle? I'm using a company PC which has a ~/.gradle/init.gradle file that globally specifies the default gradle repos like:

allProjects {
  buildscript {
    repositories {
       mavenLocal()
       maven { url "<company url>"}
    }
  } 
  repositories {
    mavenLocal()
    maven { url "< company repo url>"}
  }
}

I can only access this while on the company network which means for any side project the build fails if I don't log on. Is there a way I can override this repo in the project to point to a different (public) repo? I've tried various things like changing the project build.gradle to have:

repositories {
  maven {
    url "https://maven.springframwork.org/release"
  }
}

But it still tries to point to the company repo and fails to resolve it.

CodePudding user response:

If you need to add some repositories for dependencies - you can do it right in the build.gradle. If you want to override Gradle plugin repositories, defined in global init script, only for a specific project(s), you need to:

  1. Create an init script, which would be executed after init.gradle in USER_HOME/.gradle/ directory (for instance, init.gradle.kts in the USER_HOME/.gradle/init.d/ directory)
  2. Use some if-condition to specify what projects will be covered with this override (for instance, in some specific folder). Gradle scripts are not only a DSL, you can use all power of Turing-complete Groovy/Kotlin.

I'll use Kotlin here (probably, it's a valid Groovy code too):

settingsEvaluated {
    if (rootProject.projectDir.toString().startsWith("path/to/my/side/projects/folder")) {
        pluginManagement {
            repositories {
                gradlePluginPortal()
                mavenLocal()
            }
        }
    }
}
  • Related