Skip to content

Instantly share code, notes, and snippets.

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:
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) {
// Put your logic here, for example:
for (def selection : selections) {
if (selection.taskName == ":example:assembleRelease") {
// 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