Saturday, July 6, 2013

JAXB: Using Maven Plugin XJC tool to generate POJO objects

Step 01: Family.xsd
<?xml version="1.0" encoding="UTF-8"?>
<!-- (c) 2010 DIRECTV, Inc. All rights reserved. -->
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    elementFormDefault="qualified" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb">

    <xsd:complexType name="father">
            <xsd:sequence>
                <xsd:element name="fatherName" type="xsd:string"
                    minOccurs="0" />
                <xsd:element name="dateOfBirth" type="xsd:date"
                    minOccurs="0" />
                <xsd:element name="job" type="xsd:string" minOccurs="0" />
                <xsd:element name="age" type="xsd:int" minOccurs="0" />
            </xsd:sequence>
    </xsd:complexType>
    
    <xsd:complexType name="mother">
        <xsd:sequence>
            <xsd:element name="motherName" type="xsd:string"/>
            <xsd:element name="dateOfBirth" type="xsd:date"/>
            <xsd:element name="job" type="xsd:string"/>
            <xsd:element name="age" type="xsd:int"/>
        </xsd:sequence>
    </xsd:complexType>
    
    <xsd:complexType name="children">
        <xsd:sequence>
            <xsd:element name="fullName" type="xsd:string"
                minOccurs="0" />
            <xsd:element name="dateOfBirth" type="xsd:date"
                minOccurs="0" />
            <xsd:element name="job" type="xsd:string" minOccurs="0" />
            <xsd:element name="age" type="xsd:int" minOccurs="0" />
        </xsd:sequence>
    </xsd:complexType>
    
    <xsd:element name="family">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="father" type="father"/>
                <xsd:element name="mother" type="mother"/>
                <xsd:element name="children" type="children" maxOccurs="unbounded"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
    
</xsd:schema>
Step 02: Pom.xsd
<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.sample.common</groupId>
    <artifactId>restfuljaxb</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>restfuljaxb Maven Webapp</name>
    <url>http://maven.apache.org</url>

    <repositories>
        <repository>
            <id>maven2-repository.java.net</id>
            <name>Java.net Repository for Maven</name>
            <url>http://download.java.net/maven/2/</url>
            <layout>default</layout>
        </repository>
    </repositories>

    <properties>
        <jersey.version>1.16</jersey.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.8.2</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <finalName>restfuljaxb</finalName>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
            <!-- Plugin to generate POJOs -->
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>jaxb2-maven-plugin</artifactId>
                <version>1.3</version>
                <executions>
                    <execution>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>xjc</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <packageName>com.jaxb.family</packageName>
                    <schemaDirectory>${basedir}/src/main/resources</schemaDirectory>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
Step 03: Maven Build maven install
Step 04: Output

No comments:

Post a Comment