Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import org.gradle.util.NameMatcher
def isTaskSelected(String... taskNames) {
def matcher = new NameMatcher()
def nameList = Arrays.asList(taskNames)
def requestedTaskNames = gradle.startParameter.taskNames
for (def requestedTaskName : requestedTaskNames) {
if (matcher.find(requestedTaskName, nameList) != null) {
return true
}
}
return false
}
// Example:
isTaskSelected(":app:assembleRelease", "assembleRelease")
// Note that the result is not necessarily accurate!!!
// More information about the NameMatcher:
// https://github.com/gradle/gradle/blob/master/subprojects/core/src/test/groovy/org/gradle/util/NameMatcherTest.java
import org.gradle.api.internal.GradleInternal
import org.gradle.execution.commandline.CommandLineTaskParser
import org.gradle.execution.TaskSelector
project.gradle.taskGraph.whenReady {
def parser = ((GradleInternal) project.gradle).getServices().get(CommandLineTaskParser)
final List<TaskSelector.TaskSelection> selections = new ArrayList<>()
for (def taskRequest : project.gradle.startParameter.taskRequests) {
selections.addAll(parser.parseTasks(taskRequest))
}
// Put your logic here, for example:
for (def selection : selections) {
if (selection.taskName == ":example:assembleRelease") {
// TODO
}
}
}
// This result is accurate. But you can't use it in the configuration phase.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.