In my gradle file I want to know which task triggered the code block. e.g. If I run
gradle assembleVanillaDebug
from the terminal, I want to know in my gradle file that assembleVanillaDebug task is being executed. This will also help me in figuring out I am running debug build type task or release build type task. Can we know which task is being executed?
To see which tasks are available for our build we can run Gradle with the command-line option -t or --tasks. Gradle outputs the available tasks from our build script. By default only the tasks which are dependencies on other tasks are shown. To see all tasks we must add the command-line option --all.
The work that Gradle can do on a project is defined by one or more tasks. A task represents some atomic piece of work which a build performs. This might be compiling some classes, creating a JAR, generating Javadoc, or publishing some archives to a repository.
To get an overview of all Gradle tasks in our project, we need to run the tasks task. Since Gradle 5.1, we can use the --group option followed by a group name. Gradle will then show all tasks belonging to the group and not the other tasks in the project.
> gradle -q test Adding test task to project ':project-a' Running tests for project ':project-a' This example uses method Project. afterEvaluate() to add a closure which is executed after the project is evaluated. It is also possible to receive notifications when any project is evaluated.
In the end,
gradle.startParameter.taskNames
proved to be my friend. gradle.startParameter.getTaskNames()
would return you the list of all tasks being executed in current build.
e.g. For gradle clean assembleVanillaDebug
, it will return you a list of tasks clean
and assembleVanillaDebug
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With