Cannot scan non-builtin tasks by class-type from a Gradle script plugin?


(Chris Rankin) #1

Hi,

I have some “boilerplate” configuration that I would like to push into a script plugin so that it can be reused. For example:

tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all {
    kotlinOptions {
        jvmTarget = '1.8'
    }
}

However, Gradle 4.9 doesn’t recognise the KotlinCompile class from within a script plugin, and I am forced to write something like this instead:

tasks.withType(AbstractCompile).all {
    if (it.class.name.startsWith("org.jetbrains.kotlin.gradle.tasks.KotlinCompile")) {
        kotlinOptions {
            jvmTarget = '1.8'
        }
    }
}

This works, but is ugly. Is there a better way to do this please?

Obviously script plugins have no difficulty with built-in task classes such as JavaCompile.


(Chris Doré) #2

By script plugin you mean apply from: something.gradle right?

If so, that file needs to declare the required dependencies in a buildscript{} block.


(Chris Rankin) #3

Hi, thanks for replying. And yes, by “script plugin” I do mean

apply from: something.gradle

But while adding

buildscript {
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

to the script plugin would be acceptable to Gradle, I have discovered that the KotlinCompile tasks in the Gradle module that applied the script plugin would remain unconfigured. Hence my

it.class.name.startsWith("...")

workaround.

Could this be a bug in Gradle?