Home > Enterprise >  How to create POJO classes using JAXB maven plugin in java 11
How to create POJO classes using JAXB maven plugin in java 11

Time:04-09

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>

CodePudding user response:

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.com/2022/03/07/jdk-17-and-maven-jaxb2-plugin/

  • Related