How to delegate the running of a task to all subprojects without running a project level?

(Don Henry) #1

I have created a task (called “get”) as a DSL extension that’s defined for all projects within some gradle code in the init.d folder. This task pulls source code from a Subversion repository.

Some of my projects will be standalone and the get task will be fired at the project level. Other projects will have multiple subprojects and the get task will be fired by each subproject, not at the project level.

In each case, I want to have a singe build.gradle file for each project.

On a project by project basis, I want to be able to either fire the get task at the project level, or do nothing at the project level and have the get task run by the subprojects.

Seems like my problem is that I need to get “in front” of this code because the build fails because localDir is not initialized at the project level:

class SvnPlugin implements Plugin<Project> {
    void apply(Project project) {
        project.task('get', type: SvnCheckout, dependsOn: 'svnInit') {
            outputs.files {
                project.files(project.file(localDir).listFiles()) - project.files(["$localDir/build.gradle", "$localDir/.gradle", "$localDir/.idea"])
        project.task('tag', type: SvnTag, dependsOn: 'svnInit') {

(Luke Daley) #2

What defines localDir? It’s not part of the Gradle API.

(Don Henry) #3

It’s data within the SvnCheckout class. See below.

class SvnCheckout extends SvnTasks {
    String localDir
    def checkout() {
        String svnURL = project.config.svn.url
        String modulePath = project['moduleVCSPath']
          String url
        if (project.hasProperty('tagName')) {
            url = createSvnURL(svnURL, modulePath, UrlType.TAG, project['tagName'])
        } else if (project.hasProperty('branchName')) {
            url = createSvnURL(svnURL, modulePath, UrlType.BRANCH, project['branchName'])
        } else {
            url = createSvnURL(svnURL, modulePath, UrlType.TRUNK)
        println "Getting $url"
        project.ant {
            svn(refid: 'svn.settings') {
                checkout(url: url, destPath: localDir)

(Luke Daley) #4

What do you mean by “have the get task run by the subprojects”? Just that all the sub projects should have this task?

Is the problem that you always want ‘localDir’ to be interpreted relative to the root project?

(Don Henry) #5

The problem seems to be that the get task is visible to all projects because of the code below:

allprojects {
    configurations {
    dependencies {
      task svnInit << {
        String userName = config.svn.http.username
        String userPassword = config.svn.http.password
        ant {
            typedef(resource: 'org/tigris/subversion/svnant/svnantlib.xml', classpath: configurations.svnAnt.asPath)
            svnSetting(svnkit: false, javahl: false, username: userName, password: userPassword, id: 'svn.settings')
      apply plugin: SvnPlugin

But I want to be able within the project’s build.gradle to specify that when I run “gradle get” to skip the “get” call at the project level and just delegate to the subprojects.

(Don Henry) #6

FYI - the above code is within the svntasks.gradle in the init.d folder and not in the project’s build.gradle.

(Luke Daley) #7

Simplest solution would be ‘rootProject.get.enabled = false’.

(Don Henry) #8

OK. That’s simple enough for me. Let me try it. Thanks.

(Don Henry) #9

Worked great! Thanks again.

dohenry@ubuntu-donhenry:~/atom_products/ms2$ gradle get
Initialization script '/home/dohenry/gradle_init.d/bpsrepos.gradle': line 25
The ArtifactRepositoryContainer.add(DependencyResolver, Closure) method has been deprecated and is scheduled to be removed in Gradle 2.0.

(Luke Daley) #10

Great, happy Gradling!