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

发布于 2025-01-19 16:37:27 字数 9085 浏览 0 评论 0 原文

需要在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 schema:


    <?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>

In need to create Pojo classes using JAXB maven plugin in java 11, i am using this plugin in java 8 and working fine:

<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>

and here is my *.xsd schema:


    <?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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

脸赞 2025-01-26 16:37:27

用法的一个示例如下:

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

              <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>

参考:

An example of usage is below:

Kindly take note of the inner plugin dependency.

              <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>

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文