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?
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 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.
doFirst
will add the closure as the first task action for the build task. Task actions are executed in the execution phase. –
Screwdriver Could not get unknown property 'preBuild' for root project
in Android Studio 3.1.3. Could you show me the complete gradle files? –
Horseleech gradlew assemble
task and if so how? –
Dusa java
api instead of groovy? I am asking because I am developing a gradle plugin that should run kapt{variantName}kotlin
task to process some annotations before a build happens. I have tried dependsOn
but it doesn't seem to work. –
Puma 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
assembleDebug
? –
Trachyte There is one more way how to do this
task myTask << {
println "here's a task"
}
tasks.whenTaskAdded { task ->
if (task.name == 'assembleDebug') {
task.dependsOn myTask
}
preBuild
task but it doesn't seem to be taking. –
Pirn whenTaskAdded
. I don't see it when I print the task.name
in this closure. –
Pirn if (task.name == 'preDebugBuild' || task.name == 'preReleaseBuild')
works for me instead of preBuild
. –
Pirn whenTaskAdded
mechanism does not seem to work for the preBuild
task. It seems this task does not invoke this closure. I used the above examples to define dependents on preBuild
, but used whenTaskAdded
to add dependents on other tasks, such as validateReleaseSigning
. –
Pirn This is for Kotlin DSL (build.gradle.kts):
tasks.register/* OR .create */("MyTask") {
doLast {
println("I am the task MyTask")
}
}
tasks.build {
dependsOn("MyTask")
}
// OR another notation
// tasks.named("build") {
// dependsOn(tasks["MyTask"])
// }
For more information see Gradle documentation: Adding dependencies to a task.
The left shift operator <<
was removed in Gradle 5.
In my case I had an Android project using a Java sub project and this worked:
task myTask {
doLast {
println 'do it before build'
}
}
assemble.dependsOn myTask
Regarding the initial question this should be the syntax now:
task myTask {
doLast {
println 'do it before build'
}
}
build.dependsOn myTask
// or for Android
preBuild.dependsOn myTask
could not get unknown property 'build' for root project 'core' of type org.gradle.api.Project
–
Mcclary build.gradle
? Is defining `defaultTasks 'build' help? –
Cyzicus build.gradle
and no, adding defaultTasks 'build'
in my build file does not help. –
Mcclary In Gradle 5.4.x
// File: app/build.gradle
// See: https://docs.gradle.org/current/dsl/org.gradle.api.tasks.Exec.html
task ruby(type:Exec) {
workingDir '../'
executable = '/usr/bin/env'
args = ["ruby", "--version"]
}
preBuild.dependsOn ruby
If the task to be run is already defined (for example publishToMavenLocal
), you can add it into your gradle build
task with:
build.dependsOn publishToMavenLocal
© 2022 - 2024 — McMap. All rights reserved.