GradleUp / jaxb2

This plugin makes it easy to generate Java source code from XML schema files (.xsd). Internally the plugin relies on the JAXB2
Apache License 2.0
7 stars 4 forks source link

plugin does not work for kotlin dsl #7

Open groupcard-ruben-timmermans opened 3 months ago

groupcard-ruben-timmermans commented 3 months ago
jaxb2 {
  xjc {
    'request-classes' {
      basePackage = 'com.any.app.model.request'
      schema = 'src/main/xsd/request.xsd'
    }
  }
}

I tried to make this work using kotlin dsl for gradle but ufortunately 'request-classes' can't be invoked as a function. Trying other things like create("request-classes") {....} or register("request-classes") {....} doesn't work either. Can you provide a working example for kotlin dsl (build.gradle.kts)? Thanks in advance.

deepy commented 3 months ago

From memory I think the kotlin equivalent is ` instead of ', but I'll add a kotlin example tomorrow

groupcard-ruben-timmermans commented 3 months ago

also tried with the backticks but they don't work either, thanks for adding an example.

groupcard-ruben-timmermans commented 3 months ago

interestingly when i use the above script in groovy dsl i get a smilar error. Here is the snippet of my build.gradle:

jaxb2 {
    xjc {
        'request-classes' {
            basePackage = 'nl.groupcard'
            schema = 'src/main/resources/stuf/somefile.xsd'
            encoding = 'UTF-8'
            extension = true
        }
        'generation-step-name' {
            generatedSourcesDir = 'build/generated'
        }
    }
}

and part of the stacktrace:

Caused by: java.lang.NullPointerException: Cannot invoke method replace() on null object
    at com.gradleup.jaxb.tasks.GenerateJaxb2Classes.antXjc(GenerateJaxb2Classes.groovy:46)
    at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103)
    at org.gradle.internal.reflect.JavaMethod.invoke(JavaMethod.java:125)

This and the fact that the generatedSourcesDir IS created but with the default value seems to hint that no fields such as basePackage or generatedSourcesDir are actually set. I forgot to mention, i use gradle 8.8 along with jdk21

deepy commented 3 months ago

A quick test shows that this works on 8.8 java 8

jaxb2.xjc.register("request-classes") {
    schema = "src/main/xsd/request.xsd"
    basePackage = "com.gradleup.generated.model"
    generatedSourcesDir  = "src/generated/java"
}

But throws an error on java 11 so presumably also on JDK 21, I should be able to get to that this weekend though

deepy commented 2 months ago

I can't get the error to reproduce, so can confirm this works fine on JDK 21+ You just need the jaxb-api dependency (like: jaxb2("javax.xml.bind:jaxb-api:2.3.1"))