Slackbot
04/19/2023, 12:13 PMVampire
04/19/2023, 12:59 PMKotlinCompile class you search for in the init script is most probably not coming from the same class loader that is used to register the tasks in question. So as the classes come from different class loaders, they are technically different classes and you get no matches.
You can easily verify by printing out the class loaders from the init script and from the task where it works.
You might need to instead need to do something like
tasks.configureEach {
if (it.class.name == "org.jetbrains.kotlin.gradle.tasks.KotlinCompile") {
...
}
}
or something similar.
Btw. do not do tasks.withType(...) { ... }, that totally prevents task-configuration avoidance. Always do tasks.withType(...).configureEach { ... }.Jaya Surya Thotapalli
04/20/2023, 7:29 AMtasks out, from both a gradle task and an init script (as shown above).
And both of them printed a quite a different set of tasks. You can see the difference here https://www.diffchecker.com/LA7U9InU/
Am I’m missing something about the lifecycle of Gradle here?Jaya Surya Thotapalli
04/20/2023, 11:45 AMtasks.all{} to gain access to all of the tasks.
Accessing tasks like def tasks = tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile) (like I have mentioned above) might not return all of the tasks. Again, I think this has to do something with the lifecycle of Gradle
(which I’m not completely sure about), but tasks.all{} seems to do the trick for me.
And thanks for the hint about comparing tasks with their class name, it is working perfectly.Vampire
04/20/2023, 12:53 PMtasks.all though, except for trying things out / debugging.
It completely circumvents task configuration avoidance, causing each and every task to be realized and configured eagerly and thus wasting your time.Jaya Surya Thotapalli
04/20/2023, 5:36 PMallprojects {
afterEvaluate {
tasks.all {
if (it.class.name.contains("org.jetbrains.kotlin.gradle.tasks.KotlinCompile")) {
kotlinOptions {
freeCompilerArgs += ["-P", "...."]
}
}
}
}
}Vampire
04/20/2023, 5:50 PMtasks.configureEach, not tasks.allVampire
04/20/2023, 5:51 PMJaya Surya Thotapalli
04/21/2023, 5:29 AM