Factoring out common buildscript() definition in multi-project build

We have a multi-project build, and I am trying to apply a third-party plugin to all of the individual projects (in particular, the gradle-cobertura-plugin to provide code coverage metrics).

I currently have a solution that works, but it requires the following code to be inserted into every individual project’s build script (‘maven-main-cache’ is defined in another script):

buildscript {
    repositories {
        add project.repositories.'maven-main-cache'
    dependencies {
        classpath group: 'net.saliman', name: 'gradle-cobertura-plugin', version: '1.1.2'
apply plugin: 'cobertura'
cobertura {
   coverageFormats = [ 'xml' ]

This code has to be manually added to any new projects, and pretty much doubles the length of all our build scripts. Ideally I want to factor this code out into a separate file that can be included in each build script using the “apply from” syntax.

The solution from here looks like it should work, but the buildscript.gradle file doesn’t seem to have access to properties defined in gradle.properties, so cannot find basic things we define there, such as repository location or user credentials, even if I try to refer to the repository directly rather than through the project.repositories object.

Is my approach to this just all wrong?

The ‘buildscript’ block is very special and cannot be factored out into a separate file. The best you can currently do is to have a single ‘buildscript’ block in the root project’s build file. Then all other build files will “inherit” that class path.

Thank you, that was the nugget of information I needed to move forward - classpath inherited from the root project, and task configuration done in a common file that is already included by all individual projects.


I’m trying to do something similar (get a dependency from a enterprise ivy during the buildscript phase) but I can’t get access to the gradle.properties from inside a initscript block. I need a keyFile path that is in gradle.properties.

It works in a builtscript block but not in a initscript black