Alex Latchford
Alex Latchford

Reputation: 655

How to handle generically a not found task error within Gradle

We're building an archetype pattern using cookiecutter. This essentially means we have a nested structure with an outer build.gradle and an inner build.gradle. Is there a way make the tasks from the inner build.gradle available as tasks on the outer project?

I'm thinking of using a GradleBuild task in the outer build.gradle to invoke the inner tasks but I'm not certain how to parse and load all the tasks from the inner build.gradle within the outer one. What I was thinking was just catching the <task> not found error on the outer build.gradle and then trying to run that task on the inner build.gradle. Does anyone know how to catch the <task> not found exceptions and run some arbitrary task at that point?

Upvotes: 0

Views: 81

Answers (1)

Lukas K&#246;rfer
Lukas K&#246;rfer

Reputation: 14493

Did you consider using composite builds?

However, it is not necessary to catch that error as you can simply define a task rule that will create any missing task on the run. Simply register a rule like the one below:

tasks.addRule("Pattern: <task>") { name ->
    task(name, type: GradleBuild) {
        dir = // apply your setup here
        tasks = [name]
    }
}

Upvotes: 1

Related Questions