0

I'm trying to convert some task configuration in my Gradle plugin from Groovy to Kotlin. All of the examples I've found are about normal build scripts, and that approach doesn't seem to directly translate to plugin usage. My attempt below:

class JavaConventionsPlugin : Plugin<Project> {
    // ...
    fun configureBasicJavaOptions(project: Project) {
        project.tasks.withType<JavaCompile> {
            options.encoding = "cp1252"
            options.warning = false
        }
    }
}

produces these errors:

  • Type mismatch: inferred type is () -> Unit but Class<TypeVariable(S)!>! was expected
  • Unresolved reference: options
  • Variable expected

What the right way to do this?

Brad Mace
  • 27,194
  • 17
  • 102
  • 148

1 Answers1

1

I'm not sure if this is the way, but it seems to work:

    private fun configureBasicJavaOptions(project: Project) {
        project.tasks.withType(JavaCompile::class.java) { t ->
            t.options.encoding = "cp1252"
            t.options.isWarnings = false
        }
    }
Brad Mace
  • 27,194
  • 17
  • 102
  • 148
  • 1
    This works and uses the Java API as I mentioned in your other question. One thing to keep in mind with this approach is that `withType(Class clazz)` will configure ALL tasks of that type. That may or may not be what you want. You can use [`named(taskName, clazz) {}`](https://docs.gradle.org/current/javadoc/org/gradle/api/NamedDomainObjectCollection.html#named-java.lang.String-java.lang.Class-org.gradle.api.Action-) instead to configure a specific task. – Cisco Dec 07 '21 at 19:13