Search code examples
javamavengradlejaxb

Generate classes from XSD without namespace


I'm currently converting a large build from Maven to Gradle. We have a code generation step that uses JAXB to generate classes from a set of XSDs. In the Maven build, the classes are generated without a namespace, which is correct, as the same classes will be used in two different namespaces. In the Gradle build, the generated classes have a namespace added in their XMLRootElement annotation, which prevents them working properly. How can I change the Gradle build step so the generated classes don't have a namespace?

The Maven set up looks like this:

<plugin>
    <groupId>org.jvnet.jaxb2.maven2</groupId>
    <artifactId>maven-jaxb2-plugin</artifactId>
    <version>0.14.0</version>
    <configuration>
        <schemaDirectory>${project.build.directory}/classes/xmlbuild</schemaDirectory>
        <bindingDirectory>${project.build.directory}/classes/xmlbuild</bindingDirectory>
        <strict>false</strict>
        <schemaIncludes>
            <include>**/*.xsd</include>
        </schemaIncludes>
        <bindingIncludes>
            <include>**/*.xjb</include>
        </bindingIncludes>
        <args>
            <arg>-Xannotate</arg>
            <arg>-Xvalue-constructor</arg>
            <arg>-XtoString</arg>
            <arg>-Xequals</arg>
            <arg>-XhashCode</arg>
            <arg>-Xcopyable</arg>
            <arg>-Xmergeable</arg>
            <arg>-Xdefault-value</arg>
            <arg>-Xfluent-api</arg>
        </args>
        <extension>true</extension>
    </configuration>

In Gradle I'm using the com.intershop.gradle.jaxb plugin. To me, the config looks the same:

jaxb {
  javaGen {
    apiModelClasses {
        schemas = fileTree("$buildDir/path/to/xsds") {
            include '*.xsd'
        }
        bindings = fileTree("$buildDir/path/to/xsds") {
            include '*.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'
        ]
    }
  }
}

Solution

  • Simply remove the -npa flag from the JAXB config. This means "no package annotations". After it is removed, the namespace info will be added to a package-info.java file in each package, not inside the generated java classes.