如何在java 11中使用JAXB Maven插件创建POJO类

问题描述 投票:0回答:1

需要在java 11中使用JAXB maven插件创建Pojo类,我在java 8中使用这个插件并且工作正常:

<plugin>
            <groupId>org.jvnet.jaxb2.maven2</groupId>
            <artifactId>maven-jaxb2-plugin</artifactId>             
            <executions>
                <execution>
                    <id>process-xsd1</id>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                    <phase>generate-sources</phase>
                    <configuration>
                        <schemaIncludes>
                            <include>*.xsd</include>
                        </schemaIncludes>
                        <generateDirectory>generated-sources</generateDirectory>
                        <generatePackage>com.bill.entities</generatePackage>
                        <extension>true</extension>
                        <args>
                            <arg>-Xannotate</arg>
                            <arg>-npa</arg>
                        </args>
                        <plugins>
                            <plugin>
                                <groupId>org.jvnet.jaxb2_commons</groupId>
                                <artifactId>jaxb2-basics-annotate</artifactId>
                                <version>0.6.3</version>
                            </plugin>
                            <plugin>
                                <groupId>org.jvnet.jaxb2_commons</groupId>
                                <artifactId>jaxb2-basics</artifactId>
                                <version>0.6.3</version>
                            </plugin>
                            <plugin>
                                <groupId>org.jboss.resteasy</groupId>
                                <artifactId>resteasy-jettison-provider</artifactId>
                                <version>2.2.0.GA</version>
                            </plugin>
                            <plugin>
                                <groupId>com.fasterxml.jackson.core</groupId>
                                <artifactId>jackson-annotations</artifactId>
                                <version>2.1.1</version>
                            </plugin>
                        </plugins>
                    </configuration>
                </execution>
                <execution>
                    <id>process-xsd2</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                    <configuration>
                        <forceRegenerate>true</forceRegenerate>
                        <schemaDirectory>src/main/resources/FDP/acquire/v1</schemaDirectory>
                        <bindingDirectory>src/main/resources/FDP</bindingDirectory>
                        <generateDirectory>generated-sources</generateDirectory>                            
                        <strict>false</strict>
                        <extension>true</extension>
                        <args>
                            <arg>-no-header</arg>
                            <arg>-Xxew</arg>
                            <arg>-Xxew:instantiate lazy</arg>
                            <arg>-Xxew:delete</arg>
                        </args>
                        <plugins>
                            <plugin>
                                <groupId>com.github.jaxb-xew-plugin</groupId>
                                <artifactId>jaxb-xew-plugin</artifactId>
                                <version>1.0</version>
                            </plugin>
                        </plugins>
                    </configuration>
                </execution>                    
            </executions>
        </plugin>

这是我的 *.xsd 架构:


    <?xml version="1.0" encoding="UTF-8"?>
<xs:schema targetNamespace="http://www.bill.com/NextStam"
    elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:tns="http://www.bill.com/NextStam" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
    jaxb:version="2.1" xmlns:annox="http://annox.dev.java.net"
    jaxb:extensionBindingPrefixes="annox" xmlns:jl="http://annox.dev.java.net/java.lang"
    xmlns:jns="http://annox.dev.java.net/com.fasterxml.jackson.annotation">

    <xs:include schemaLocation=""></xs:include>
    <xs:complexType name="FIProfile">
        <xs:sequence>
            <xs:element name="allowedConnections" type="xs:int"
                nillable="true">
                <xs:annotation>
                    <xs:appinfo>
                        <annox:annotate target="field">
                            <jns:JsonProperty value="AllowedConnections" />
                        </annox:annotate>
                    </xs:appinfo>
                </xs:annotation>
            </xs:element>
            <xs:element name="activeConnections" type="xs:int"
                nillable="true">
                <xs:annotation>
                    <xs:appinfo>
                        <annox:annotate target="field">
                            <jns:JsonProperty value="ActiveConnections" />
                        </annox:annotate>
                    </xs:appinfo>
                </xs:annotation>
            </xs:element>
            <xs:element name="supportsCustomer" type="xs:boolean"
                nillable="true" default="false">
                <xs:annotation>
                    <xs:appinfo>
                        <annox:annotate target="field">
                            <jns:JsonProperty value="SupportsCustomer" />
                        </annox:annotate>
                    </xs:appinfo>
                </xs:annotation>
            </xs:element>
            <xs:element name="supportsAccounts" type="xs:boolean"
                nillable="true">
                <xs:annotation>
                    <xs:appinfo>
                        <annox:annotate target="field">
                            <jns:JsonProperty value="SupportsAccounts" />
                        </annox:annotate>
                    </xs:appinfo>
                </xs:annotation>
            </xs:element>
            <xs:element name="supportsTransactions" type="xs:boolean"
                nillable="true">
                <xs:annotation>
                    <xs:appinfo>
                        <annox:annotate target="field">
                            <jns:JsonProperty value="SupportsTransactions" />
                        </annox:annotate>
                    </xs:appinfo>
                </xs:annotation>
            </xs:element>
            <xs:element name="supportsStatements" type="xs:boolean"
                nillable="true">
                <xs:annotation>
                    <xs:appinfo>
                        <annox:annotate target="field">
                            <jns:JsonProperty value="SupportsStatements" />
                        </annox:annotate>
                    </xs:appinfo>
                </xs:annotation>
            </xs:element>
            <xs:element name="supportsImage" type="xs:boolean"
                nillable="true">
                <xs:annotation>
                    <xs:appinfo>
                        <annox:annotate target="field">
                            <jns:JsonProperty value="SupportsImage" />
                        </annox:annotate>
                    </xs:appinfo>
                </xs:annotation>
            </xs:element>
            <xs:element name="supportsTransfer" type="xs:boolean"
                nillable="true">
                <xs:annotation>
                    <xs:appinfo>
                        <annox:annotate target="field">
                            <jns:JsonProperty value="SupportsTransfer" />
                        </annox:annotate>
                    </xs:appinfo>
                </xs:annotation>
            </xs:element>
        </xs:sequence>
    </xs:complexType>
</xs:schema>
java spring-boot maven xsd jaxb
1个回答
0
投票

使用示例如下:

请注意内部插件依赖关系。

              <plugin>
               <groupId>org.jvnet.jaxb2.maven2</groupId>
               <artifactId>maven-jaxb2-plugin</artifactId>
               <version>${maven-jaxb2-plugin.version}</version>
               <executions>
                   <execution>
                       <id>example-xmls</id>
                       <goals>
                           <goal>generate</goal>
                       </goals>
                       <configuration>
                           <generateDirectory>target/generated-sources/xjc/</generateDirectory>
                           <schemaDirectory>${basedir}/src/main/resources/schemas/xsd</schemaDirectory>
                           <schemaIncludes>
                               <include>**/*.xsd</include>
                           </schemaIncludes>
                           <bindingDirectory>${basedir}/src/main/resources/schemas/xjb</bindingDirectory>
                           <bindingIncludes>
                               <include>**/*.xjb</include>
                           </bindingIncludes>
                       </configuration>
                   </execution>
               </executions>
               <dependencies>
                   <dependency>
                       <groupId>org.glassfish.jaxb</groupId>
                       <artifactId>jaxb-runtime</artifactId>
                       <version>2.3.3</version>
                   </dependency>
               </dependencies>
           </plugin>

参考:https://jcompetence.se/2022/03/07/jdk-17-and-maven-jaxb2-plugin/

© www.soinside.com 2019 - 2024. All rights reserved.