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

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') {
        }
    }
}

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

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

class SvnCheckout extends SvnTasks {
    String localDir
      @TaskAction
    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)
            }
        }
    }
}

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?

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

allprojects {
    configurations {
        svnAnt
    }
    dependencies {
        svnAnt('org.tigris.subversion:svnClientAdapter:0.9.102')
        svnAnt('org.tigris.subversion:svnant:1.3.1')
    }
      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.

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

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

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

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.
:svnInit
:get SKIPPED
:mscore:svnInit
:mscore:get
Getting http://bps-svn.turner.com/mediasource/mscore/trunk
:omneonserver:svnInit
:omneonserver:get
Getting http://bps-svn.turner.com/mediasource/omneonserver/trunk
:webapp:svnInit
:webapp:get
Getting http://bps-svn.turner.com/mediasource/webapp/trunk
  BUILD SUCCESSFUL

Great, happy Gradling!