mdzh
mdzh

Reputation: 1050

Gradle - Can I manage task dependencies using convention properties?

I'm using Gradle as a build system for my project. What I want is to make task A depend on task B if a given property is set to "true". Is this doable, and if the answer is yes, how can I do that?

Currently, I'm using conventionMapping but this doesn't seem to work. My code looks like this:

MyTask.conventionMapping.propertyName = { MyPluginConvention.propertyName }

if (MyTask.propertyName.equals("true")) {
    MyTask.dependsOn ...
}

Thanks in advance, Marin

Upvotes: 0

Views: 666

Answers (1)

Peter Niederwieser
Peter Niederwieser

Reputation: 123910

Instead of working with task/convention classes, you'll have to work with their instances. Also, you'll have to defer the decision whether to add a task dependency. For example:

def myTask = project.tasks.create("myTask", MyTask)
def otherTask = ...
def myConvention = new MyConvention()    
...
myTask.conventionMapping.propertyName = { myConvention.propertyName }
// defer decision whether to depend on 'otherTask'
myTask.dependsOn { myTask.propertyName == "true" ? otherTask : [] }

If there's no task variable in scope, you can also reference existing tasks via project.myTask or project.tasks["myTask"].

PS: Convention objects have been largely replaced by extension objects.

Upvotes: 1

Related Questions