Configure buildscript repositories for all project globally in init.gradle?

Can buildscript repositories be defined in init scripts? For example, currently I have in my build script:

buildscript {
    repositories {
        maven {
            url = 'http://mydomain/nexus/content/groups/public'
        }
    }
    ...
}

Is it possible to write an init.gradle file that defines this repository for each build, so that build.gradle can only say this:

buildscript {
    dependencies {
        classpath 'group:module:version'
    }

and the globally configured repository is used to fetch the dependency?

wujek

I have the following: ~/.gradle/init.gradle

allprojects {
    repositories {
        maven {
            url = 'http://mydomain/nexus/content/groups/public'
        }
    }
    buildscript {
        repositories {
            maven {
                url = 'http://mydomain/nexus/content/groups/public'
            }
        }
    }
}

webapp-parent.gradle

buildscript {
    dependencies {
        classpath group: 'org.gradle.api.plugins', name: 'gradle-tomcat-plugin', version: '0.9.8'
    }
}
  apply plugin: org.gradle.api.plugins.tomcat.TomcatPlugin

build.gradle

apply {
    from 'webapp-parent.gradle'
}

Now, when I run the webapp-parent.gradle, it works:

$ ./gradlew -b webapp-parent.gradle
 Download http://mydomain/nexus/content/groups/public/org/gradle/api/plugins/gradle-tomcat-plugin/0.9.8/gradle-tomcat-plugin-0.9.8.jar
...
BUILD SUCCESSFUL

but when I run the build.gradle file, it fails:

$ ./gradlew
  FAILURE: Build failed with an exception.
  * Where:
Build file '/home/wujek/IdeaWorkspace/gradle-test/build.gradle' line: 1
  * What went wrong:
A problem occurred evaluating root project 'gradle'.
> Could not resolve all dependencies for configuration 'classpath'.
   > Could not find org.gradle.api.plugins:gradle-tomcat-plugin:0.9.8.
     Required by:
         unspecified:unspecified:unspecified

What am I doing wrong?

wujek

As background, here is what I am trying to achieve: 1. have a single place where all repositories are defined, and which point to our nexus - it should be global, not per build 2. have a parent boilerplate build file that applies some plugins, in this case, the gradle-tomcat-plugin 3. have all webapps apply the parent build and be able to use its configuration, classpath and so on and only refine the configuration as the webapps need it

wujek

This isn’t possible right now. I’ve raised GRADLE-2801.

Is there any work around to define the build script dependencies in init.gradle instead of the individual project build files ?

When you use ‘buildscript {}’ in a script plugin (i.e. apply from: ) it only applies to that script.