0

enter image description hereI 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

Vicky
  • 104
  • 1
  • 9
  • The best way would be to start giving things a try @Vivek. – Naman Aug 29 '18 at 17:56
  • @nullpointer thanks, I did try and was not able to do achieve the desired outcome. Attaching my dummy project, that I used with just 1 schema but the generated classes don't seem to have implemented the serializable and element interface. – Vicky Aug 30 '18 at 04:04
  • You state that _"I need to do this with Java 10.. and its not so easy as compared to java 8"_, but you don't mention what specific problems are caused by using Java 10. You need to update your post to clarify that core issue, and post the code and the outcome when using Java 10. As it stands your question is too broad, and incomplete. – skomisa Aug 30 '18 at 17:20
  • @skomisa: I did mention my problem. But its not well highlighted. So, the problem is the auto generated classes don't seem to have implemented the serializable and element interface.
    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

0 Answers0