I am trying to auto generate classes using xsd via maven plugin.
There are different schemas and for each schema I need to have different package created.
Also the auto generated classes should implement serializable and java.xml.bind.Element interfaces.
Problem is I need to do this with Java 10.. and its not so easy as compared to java8.
Can someone guide me how best can we achieve this?
XSD:
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xsd:element name="empRequest" type="EmpRequest"></xsd:element>
<xsd:element name="empResponse" type="EmpResponse"></xsd:element>
<xsd:complexType name="EmpRequest">
<xsd:sequence>
<xsd:element name="id" type="xsd:int" minOccurs="0" maxOccurs="1" />
<xsd:element name="name" type="xsd:string" minOccurs="0" maxOccurs="1" />
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="EmpResponse">
<xsd:sequence>
<xsd:element name="id" type="xsd:int" minOccurs="1" maxOccurs="1" />
<xsd:element name="name" type="xsd:string" minOccurs="1" maxOccurs="1" />
<xsd:element name="role" type="xsd:string" minOccurs="1" maxOccurs="unbounded" />
<xsd:element name="gender" type="xsd:string" minOccurs="1" maxOccurs="1" />
<xsd:element name="salary" type="xsd:string" minOccurs="1" maxOccurs="1" />
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
global.jxb:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<jaxb:bindings version="2.0"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<jaxb:globalBindings>
<xjc:simple />
<xjc:superInterface name="javax.xml.bind.Element" />
<xjc:serializable uid="1" />
<jaxb:javaType name="java.util.Calendar" xmlType="xsd:dateTime" parseMethod="javax.xml.bind.DatatypeConverter.parseDateTime" printMethod="javax.xml.bind.DatatypeConverter.printDateTime"/>
<jaxb:javaType name="java.util.Calendar" xmlType="xsd:date" parseMethod="javax.xml.bind.DatatypeConverter.parseDate" printMethod="javax.xml.bind.DatatypeConverter.printDate"/>
<jaxb:javaType name="java.util.Calendar" xmlType="xsd:time" parseMethod="javax.xml.bind.DatatypeConverter.parseTime" printMethod="javax.xml.bind.DatatypeConverter.printTime"/>
</jaxb:globalBindings>
</jaxb:bindings>
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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>com.test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>XSD to Java</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<jaxb.generated.location>${project.basedir}/src/main/generated</jaxb.generated.location>
<xsd.location>${project.basedir}/src/main/resources/schemas</xsd.location>
</properties>
<build>
<plugins>
<!-- Plugin required to build java classes from XSD using XJC -->
<plugin>
<configuration>
<schemaDirectory>${xsd.location}</schemaDirectory>
<!-- The name of your generated source package -->
<packageName>com.example.myschema</packageName>
<outputDirectory>src/main/java/generated</outputDirectory>
<arguments>-extension -npa -b ${project.basedir}/src/main/xsd/global.xjb</arguments>
</configuration>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<goals>
<goal>xjc</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-osgi</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>javax.activation-api</artifactId>
<version>1.2.0</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
Regards, Vivek
With java8 when ever I run the mvn clean install, I get the java entities being generated like: @XmlRootElement(name = "EmployeeRequest") public class EmployeeRequest implements Serializable, Element {..}
But with Java10, its like: @XmlRootElement(name = "EmployeeRequest") public class EmployeeRequest {..}
So, I need the classes to implement Serializable and Element interface. – Vicky Aug 31 '18 at 07:10