使用Maven自动生成的类

时间:2018-08-29 17:18:09

标签: java maven-3 maven-plugin java-10 maven-jaxb2-plugin

enter image description here我正在尝试通过Maven插件使用xsd自动生成类。 有不同的模式,对于每种模式,我需要创建不同的包。 自动生成的类还应实现可序列化和java.xml.bind.Element接口。

问题是我需要在Java 10中执行此操作。与Java8相比,它不那么容易。

有人可以指导我如何最好地实现这一目标吗?

 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>

关于, 维维克

0 个答案:

没有答案