unbroken-dome/gradle-xjc-plugin

Use with Kotlin

nieldw opened this issue · 3 comments

This plugin doesn't work out of the box with Kotlin.

To make your build pass, add the following to your (groovy) build.gradle file:

xjc {
    includeInMainCompilation = false
}

sourceSets {
    main { java { srcDir xjcGenerate.outputDirectory } }
}

compileKotlin {
    dependsOn xjcGenerate
}

Here's a working, complete build.gradle file assuming an XSD file in the default src/main/schema location: https://gist.github.com/nieldw/db9b46bebbfcf8c704a3b21d7e92fa01

There is a simpler workaround:

sourceSets.main.java.srcDir(xjcGenerate)

It seems that generated sources are not visible from Kotlin side because of two issues:

  1. The xjcGenerate task (instead of it's output directory) should be added to Java sources dirs. In this case it will be considered a dependency of compileJava and compileKotlin automatically.
    It could be fixed here easily.
  2. Kotlin plugin does not pick up Java source directories which added in afterEvaluate { } after it was applied.
    I think it should be fixed in Kotlin plugin. As a workaround you can apply XJC plugin before Kotlin plugin.

I'm struggling to get this and the duplicate task in kotlin to work and keep getting NPE.

guide to duplicate a task

Version 2.0 is a complete rewrite in Kotlin, so Kotlin DSL support is now first class :-)
The plugin will also work on each source set by default, so this should no longer be an issue