is it possible for Gradle to execute a task before calling
gradle build
Something like precompile
. Someone please help. Is something like this possible and how?
You can execute multiple tasks from a single build file. Gradle can handle the build file using gradle command. This command will compile each task in such an order that they are listed and execute each task along with the dependencies using different options.
Gradle determines the subset of the tasks, created and configured during the configuration phase, to be executed. The subset is determined by the task name arguments passed to the gradle command and the current directory. Gradle then executes each of the selected tasks.
You can do it in this way:
task build << { println 'build' } task preBuild << { println 'do it before build' } build.dependsOn preBuild
Thanks to that task preBuild
will be automatically called before build
task.
If you want to run preBuild
in configuration phase (previous example run preBuild
in execution
phase) you can do it in this way:
task build << { println 'build' } build.doFirst { println 'do it before build' }
More about gradle build lifecycle can be read here http://www.gradle.org/docs/current/userguide/build_lifecycle.html.
For those who are wondering how to do this in an Android project, this worked for me:
task myTask << { println "here's a task" } preBuild.dependsOn myTask
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