1

I got stuck trying to convert a XSD + XJB file to a POJO that has no JAXB annotations inside its code.

Project structure:

  • pom.xml
  • src/main/resources/schema.xsd

The file schema.xsd can be found here as an example.

I am using Maven and the plugin jaxb2-basics-annotate

Here is my maven file (pom.xml):

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.example</groupId>
    <artifactId>xsd-to-pojo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>XSD to Pojo</name>
    <properties>
        <plugin.version>1.1.0</plugin.version>
        <jaxb.version>2.3.1</jaxb.version>
        <maven-jaxb2-plugin.version>0.14.0</maven-jaxb2-plugin.version>
    </properties>
    <packaging>jar</packaging>
    <dependencies>
        <dependency>
            <groupId>org.glassfish.jaxb</groupId>
            <artifactId>jaxb-runtime</artifactId>
            <version>${jaxb.version}</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <build>
        <defaultGoal>compile</defaultGoal>
        <plugins>
            <plugin>
                <groupId>org.jvnet.jaxb2.maven2</groupId>
                <artifactId>maven-jaxb2-plugin</artifactId>
                <version>${maven-jaxb2-plugin.version}</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <encoding>UTF-8</encoding>
                    <extension>true</extension>
                    <args>
                        <arg>-Xannotate</arg>
                        <arg>-XremoveAnnotation</arg>
                    </args>
                    <plugins>
                        <plugin>
                            <groupId>org.jvnet.jaxb2_commons</groupId>
                            <artifactId>jaxb2-basics-annotate</artifactId>
                            <version>${plugin.version}</version>
                        </plugin>
                    </plugins>
                </configuration>
            </plugin>
            <plugin>
                <inherited>true</inherited>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

When I run the command mvn on command line, I get the following Java file, generated in the /target directory:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "gh16Type", propOrder = {
    "x"
})
public class Gh16Type {

    protected Object x;

    public Object getX() {
        return x;
    }

    public void setX(Object value) {
        this.x = value;
    }

    public boolean isSetX() {
        return (this.x!= null);
    }
}

@XmlAccessorType and @XmlType should be removed. But they are still there.

Thanks for any help !

If you get it to work, try to put the file binding.xjb under src/main/resources/ and try again.

joccafi
  • 78
  • 9

1 Answers1

0

To demonstrate (zip) a solution, this Maven project uses the hisrc-higherjaxb-maven-plugin to generate JAXB classes with annotations; then, it uses the XremoveAnnotation plugin from HiSrc HyperJAXB Annox to remove the annotations, followed by ann.xml resource files together with the HiSrc BasicJAXB Annox library to rebind a JAXB context, in memory.

Note: The HiSrc HyperJAXB Annox project is an updated fork of jaxb2-annotate-plugin. The demo schema and binding used here is a substitute for the OP's older schema reference. Disclaimer: I am the maintainer for the forked HiSrc projects.

The selective removal of JAXB annotations is configured in the XJB file, here's a snippet:

PurchaseOrder.xjb

...
<!-- Items -->
<jaxb:bindings node="xsd:complexType[@name='Items']">
    <annox:removeAnnotation target="class" class="jakarta.xml.bind.annotation.XmlAccessorType"/>
    <annox:removeAnnotation target="class" class="jakarta.xml.bind.annotation.XmlType"/>
</jaxb:bindings>
...

Next, the Main and PurchaseOrderTest classes each create a JAXBContext with an AnnoxAnnotationReader to read ann.xml resource files and rebind the annotations in memory. Thus, the generated JAXB classes become Plain Old Java Objects (POJOs) while the context is provided in memory.

JAXBContext plus AnnoxAnnotationReader

RuntimeAnnotationReader annotationReader = new AnnoxAnnotationReader();
Map<String, Object> properties = new HashMap<>();
properties.put(JAXBRIContext.ANNOTATION_READER, annotationReader);
Class<?>[] classes = { ObjectFactory.class };
setJaxbContext(JAXBContext.newInstance(classes, properties));
Rick O'Sullivan
  • 351
  • 3
  • 7