huehnerlady
11/23/2022, 4:14 PMallWarningsAsErrors
I do not want that in general, just on my local machine (as this would also fire when there are deprecations)
Currently my file includes:
allprojects {
afterEvaluate {
if (tasks.findByName("compileKotlin") == null) {
logger.lifecycle("[init.gradle.kts] no kotlin project")
}
else {
logger.lifecycle("[init.gradle.kts] adding 'allWarningsAsErrors = true'")
tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
kotlinOptions {
allWarningsAsErrors = true
}
}
}
}
}
When I run my project it is showing the [init.gradle.kts] adding 'allWarningsAsErrors = true'
log message, but the warnings do not make the build fail. if I add that flag to the build script, it does fail.
Any idea what I am doing wrong?Vampire
11/24/2022, 9:05 AMinitscript { ... }
block? I'd then expect that script to not even compile for not finding the KotlinCompile
class. If you have it, but just left it out, the problem most probably is, that the actual task class added by the Kotlin plugin and the task you check for using withType
are coming from different class loaders and thus are different classes, so your configuration block is not executed.huehnerlady
11/24/2022, 10:16 AMVampire
11/24/2022, 10:35 AMKotlinCompile
you check for is not the class the tasks are written in.
If you add to your init script
println(tasks.findByName("compileKotlin")!!::class.java)
println(tasks.findByName("compileKotlin")!!::class.java.superclass)
println(tasks.findByName("compileKotlin")!!::class.java.superclass.classLoader)
println(org.jetbrains.kotlin.gradle.tasks.KotlinCompile::class.java)
println(org.jetbrains.kotlin.gradle.tasks.KotlinCompile::class.java.classLoader)
you will see something like
class org.jetbrains.kotlin.gradle.tasks.KotlinCompile_Decorated
class org.jetbrains.kotlin.gradle.tasks.KotlinCompile
VisitableURLClassLoader(ClassLoaderScopeIdentifier.Id{coreAndPlugins:settings[:]:buildSrc[:]:root-project[:](export)})
class org.jetbrains.kotlin.gradle.tasks.KotlinCompile
VisitableURLClassLoader(ClassLoaderScopeIdentifier.Id{coreAndPlugins:init-file:/D:/Dateien/.gradle/init.d/test.init.gradle.kts(export)})
which shows that the two classes come from different class loaders and thus the withType
will not find the ones you like to configure.
Instead use the class of the task you found:
val compileKotlin = tasks.findByName("compileKotlin")
if (compileKotlin == null) {
...
else {
...
tasks.withType(compileKotlin::class) {
...
You also do not need the initscript dependency then.
But as the script does not really know the correct class of the task, you can also not use kotlinOptions
like that.
You would then probably need to use reflection to set it.
Again, I strongly recommend you use Groovy DSL for init scripts.
Because due to its ducktyping, kotlinOptions
could then be used without problem.
And Groovy DSL and Kotlin DSL should usually be similar enough to not confuse you.
I usually prefer Kotlin DSL for anything, but in case of init scripts, it is just better and more flexible to use Groovy instead.
With Groovy DSL the complete init script would be
allprojects {
afterEvaluate {
def compileKotlin = tasks.findByName("compileKotlin")
if (compileKotlin == null) {
logger.lifecycle("[init.gradle.kts] no kotlin project")
}
else {
logger.lifecycle("[init.gradle.kts] adding 'allWarningsAsErrors = true'")
tasks.withType(compileKotlin.getClass()) {
kotlinOptions {
allWarningsAsErrors = true
}
}
}
}
}
Vampire
11/24/2022, 10:36 AMmavenLocal()
to any build, let alone to every build.
It is broken by design, can easily result in flaky and strangely behaving builds and will also make the builds much slower, especially when added as first repository.Vampire
11/24/2022, 10:37 AMmavenLocal
.huehnerlady
11/24/2022, 10:59 AMVampire
11/24/2022, 11:12 AMVampire
11/24/2022, 11:13 AMhuehnerlady
11/24/2022, 11:14 AMhuehnerlady
11/24/2022, 11:14 AMVampire
11/24/2022, 11:15 AMhuehnerlady
11/24/2022, 11:27 AM