Caleb Cushing
09/26/2025, 12:10 PM./gradlew mytask even just running ./gradlew would execute itMartin
09/26/2025, 12:19 PMgradle.startParameters?Vampire
09/26/2025, 12:20 PMMartin
09/26/2025, 12:20 PMgradle.startParameter.taskNames.add("mytask")
(haven't tried it but sounds like it should work)Vampire
09/26/2025, 12:20 PMtasks.configureEach {
dependsOn(yourTask)
}Vampire
09/26/2025, 12:21 PMgradlew runs a task, the defaultTask which by default is helpVampire
09/26/2025, 12:21 PMCaleb Cushing
09/26/2025, 12:25 PM./gradlew but I would expect the tooling(?) api that idea uses to run it as wellVampire
09/26/2025, 12:38 PMstartParameter works, but you have to reprogram the default task logic:
val foo by tasks.registering {
doLast {
println("foo")
}
}
val startTasks = gradle.startParameter.taskNames
gradle.startParameter.setTaskNames(
if (startTasks.isEmpty()) {
if (defaultTasks.isEmpty()) {
listOf("help")
} else {
defaultTasks
}
} else {
startTasks
} + foo.get().path
)
Otherwise if no startParameter.taskNames are set the defaultTasks run and if no defaultTasks are set help runs.
If you add to the startParameter.taskNames neither the defaultTasks nor the help is ever used so just calling ./gradlew will only execute your task, hence the fancy logicVampire
09/26/2025, 12:38 PMval foo by tasks.registering {
doLast {
println("foo")
}
}
tasks.named { it != foo.name }.configureEach {
dependsOn(foo)
}Vampire
09/26/2025, 12:41 PMstartParameter.taskNames are also run, but none of the default tasks would run, so actually it would be
val foo by tasks.registering {
doLast {
println("foo")
}
}
val startTasks = gradle.startParameter.taskNames
gradle.startParameter.setTaskNames(
if (startTasks.isEmpty()) {
if (System.getProperty("idea.sync.active")?.toBoolean() == true) {
emptyList()
} else if (defaultTasks.isEmpty()) {
listOf("help")
} else {
defaultTasks
}
} else {
startTasks
} + foo.get().path
)Vampire
09/26/2025, 12:42 PMdependsOn trick, it does not run during the sync if no tasks are run during sync, but if you use Kotlin DSL, IJ will trigger prepareKotlinBuildScriptModel and thus the dependsOn also runs your task.Vampire
09/26/2025, 12:42 PM