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?

2
  • Where are you defining this plugin class? Commented Dec 7, 2021 at 3:00
  • I'm using a composite build where the gradle plugin is just an ordinary project rather than buildSrc. Commented Dec 7, 2021 at 16:47

1 Answer 1

2

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
        }
    }
Sign up to request clarification or add additional context in comments.

1 Comment

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<T> clazz) will configure ALL tasks of that type. That may or may not be what you want. You can use named(taskName, clazz) {} instead to configure a specific task.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.