Home > Software engineering >  Gradle jaxb code generation failing when annotation in xsd
Gradle jaxb code generation failing when annotation in xsd

Time:11-06

I'm trying to convert a Maven build over to Gradle. Two modules use JAXB code generation to generate code from XSDs. The first module is working but the second is failing. I'm using the intershop Gradle jaxb plugin:

https://github.com/IntershopCommunicationsAG/jaxb-gradle-plugin

The setup in my Gradle build file looks like this:

jaxbext 'org.jvnet.jaxb2_commons:jaxb2-basics-annotate:1.0.2'
jaxbext 'org.jvnet.jaxb2_commons:jaxb2-basics-ant:0.10.0'
jaxbext 'org.jvnet.jaxb2_commons:jaxb2-basics:0.10.0'
jaxbext 'org.jvnet.jaxb2_commons:jaxb2-default-value:1.1'
jaxbext 'org.jvnet.jaxb2_commons:jaxb2-fluent-api:3.0'
jaxbext 'org.jvnet.jaxb2_commons:jaxb2-value-constructor:3.0'
jaxbext 'org.jvnet.annox:annox:1.0.1'
jaxbext 'org.slf4j:slf4j-simple:1.7.25'

jaxb {
  javaGen {
    apiModelClasses {
        schemas = fileTree("$buildDir/xsds") {
            include ('file1.xsd','file2.xsd')
        }
        bindings = fileTree("$buildDir/xsds") {
            include 'jaxbbindings.xjb'
        }
        extension = true
        antTaskClassName = 'org.jvnet.jaxb2_commons.xjc.XJC2Task'
        args = [
                '-npa',                     
                '-Xannotate',
                '-Xvalue-constructor',
                '-XtoString',
                '-Xequals',
                '-XhashCode',
                '-Xcopyable',
                '-Xmergeable',
                '-Xdefault-value',
                '-Xfluent-api',
                '-verbose'
        ]
    }
  }
}

The build fails with multiple errors on annotations:

[ant:jaxb] [ERROR] Error parsing annotation.
[ant:jaxb] unknown location
[ant:jaxb]
[ant:jaxb] [ERROR] Error parsing annotation.
[ant:jaxb] unknown location

Our XSD contains the annox namespace for annotation generation:

xmlns:annox="http://annox.dev.java.net"
jaxb:extensionBindingPrefixes="xjc annox"

And we have annotations like the following:

        <xsd:element name="startDate" type="xsd:date" minOccurs="0">
            <xsd:annotation>
                <xsd:appinfo>
                    <annox:annotate target="getter">@com.fasterxml.jackson.annotation.JsonIgnore</annox:annotate>
                </xsd:appinfo>
            </xsd:annotation>
        </xsd:element>

Presumably I am missing some setup to allow JAXB to process these annotations, but what is it? Config in the Gradle task? Or another library on the classpath? I am aware that there a different XJC tasks available - I have tried it with both XJCTask and XJC2Task, they both fail with same error.

CodePudding user response:

The jar containing the annotation must be present on the jaxbext configuration. In this case the annotation is the Jackson @JsonIgnore annotation, so I've added a dependency:

jaxbext "com.fasterxml.jackson.core:jackson-annotations:$versions.jackson"

We are also using the Jackson @JsonSerialize annotation, so for that, add:

jaxbext "com.fasterxml.jackson.core:jackson-databind:$versions.jackson"

In our case we have a multi module build so I'm referencing the property in the top level build file that defines the version. In a smaller build you may define the version explicitly.

  • Related