How to configure buildscript in a different gradle file?


(nick.f.russell) #1

I have a bunch of Team Foundation Server projects to be built that all use common build logic, saving a few properties that need to be set. Since I don’t want to replicate all of this code in each project, I have moved the logic into a common gradle file, and have a gradle.properties file in each project. There exists a build.gradle file in each project as well. It’s job is to pull down the common build script from Artifactory. The common build script then pulls down a custom plugin that I’ve written to handle a lot of the dirty work of building and adds a couple of tasks. The custom plugin is also in Artifactory. How can I get this chain of grabbing files working? I currently have:

project/build.gradle:

apply from: "http://artifactory/.../CommonBuildScript.gradle"

CommonBuildScript.gradle:

buildscript {
    repositories {
        ...
    }
    dependencies {
        classpath(group: 'org.jfrog.buildinfo', name: 'build-info-extractor-gradle', version: '2.0.9')
        classpath(group: '..', name: 'customplugin', version: '1.0') {
            changing = true
        }
    }
}
buildscript {
    configurations.classpath.resolutionStrategy.cacheChangingModulesFor 0, 'seconds'
}
  apply plugin: 'customplugin'
  artifactory {
    ...
    artifacts {
        ...
    }
}
  task taskname(type: custompluginType) {
    ...
}

It seems that I cannot change buildscript from the CommonBuildScript after ‘apply from’. Or, it will simply ignore it and then not grab my dependencies, so the plugins cannot be found. I would like to move as much from the project/build.gradle as possible, but I fear it may not be possible. I moved my CommonBuildScript.gradle as is into a project/build.gradle, and everything worked perfectly. Any suggestions?

Thank you for your time. Love love love Gradle in the couple weeks I’ve been able to use it. Very powerful.


(Luke Daley) #2

Changing the buildscript classpath has to happen very early in the lifecycle, which is before any ‘apply()’ statements are processed.

The only way to influence a script’s buildscript externally is to use an init script.

It might be worth watching the recent webinar I did on this topic. The last 15m or so discusses using an init script to implicitly change the classpath of all your buildscripts.

Might be worth also looking at this post.


(nick.f.russell) #3

Luke, Thank you very much for the reply. I will look into it!


(nick.f.russell) #4

Luke,

Thank you very much for the reply. I will look into it!