Search code examples
gradlebuildbuild-process

Gradle: Prevent execution of tasks defined in allProjects for root project


I have a flat multi-project configuration like this:

projA
projB
projC
build_scripts

build_scripts contains build.gradle

build.gradle looks like this:

allprojects {
    ...
    task gitPull(type: Exec) {
        description 'Pulls git.'
        commandLine "git", "pull"
    }
}
project(':projA') {
...
}
project(':projB') {
...
}
project(':projC') {
...
}

When I run gradle gitPull from the build_scripts directory then I want gradle to execute git pull in directories of projA, projB and projC (and nowhere else).

However, since gralde treats build_scripts as the root project, it executes git pull there as well - which fails because it is not a git repository (or even if it is, I would not want to execute git pull on it while building the project)

In general, I don't want the build_scripts folder i.e. the root project to participate in any build related activity. How do I exclude the root project from been acted upon by tasks specified in allProjects?


Solution

  • From Gradle turorials:

    In this example you can see how configure filters out the project tropicalFish from executing the task hello.

    Example 56.8. Adding custom behaviour to some projects (filtered by project name)

       ....
    

    Note: The code for this example can be found at samples/userguide/multiproject/addTropical/water in the ‘-all’ distribution of Gradle.

    Settings.gradle

    include 'bluewhale', 'krill','tropicalFish' 
    

    build.gradle

    allprojects {
        task hello << {task -> println "I'm $task.project.name" }
    }
    subprojects {
        hello << {println "- I depend on water"}
    }
    configure(subprojects.findAll {it.name != 'tropicalFish'}) {
        hello << {println '- I love to spend time in the arctic waters.'}
    }