Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find `type` of Gradle task?

Tags:

gradle

I have the following code which works fine:

allprojects {
  tasks.whenTaskAdded { task ->
    if (task.name =~ /generate.*Proto/) { // ①
      task.dependsOn(protolockStatus)
      task.finalizedBy(protolockCommit)
    }
  }
}

Rather than assuming some task naming convention on the line commented with ①, I'd like to make the condition based on the type of the task. I had thought type instanceof would work, but it did not. I also don't see any methods that would provide the type of the task. Is there a way to do this?

like image 447
Noel Yap Avatar asked Sep 17 '25 23:09

Noel Yap


1 Answers

You could use the class of the task for that. So for example, you could try this:

allprojects {
    tasks.whenTaskAdded { task ->
        println "Type is " + task.class.simpleName
    }
}
like image 148
Michael Altenburger Avatar answered Sep 20 '25 14:09

Michael Altenburger