8/06/2013

Generate client object classes based on XML Schema ( .xsd files) (JAXB, Maven, Web Service)

To consume some xml based web services, sometimes we need to be able to generate java objects based on the schema that the service provider provides.

The JAXB is one tool that could help us do this and also JAXB is fully integrated with Maven.

Official Manual:
http://mojo.codehaus.org/jaxb2-maven-plugin/xjc-mojo.html

======================================================
======================================================

Example:

pom.xml:
<dependencies>
 <dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
   <version>4.10</version>
   <scope>test</scope>
 </dependency>
 <dependency>
   <groupId>javax.xml.bind</groupId>
   <artifactId>jaxb-api</artifactId>
   <version>2.1</version>
 </dependency>
 <dependency>
   <groupId>com.sun.xml.bind</groupId>
   <artifactId>jaxb-impl</artifactId>
   <version>2.1</version>
 </dependency>
</dependencies>

<build>
 <pluginManagement>
   <plugins>
     <plugin>
       <groupId>org.apache.maven.plugins</groupId>
       <artifactId>maven-compiler-plugin</artifactId>
       <version>2.3.1</version>
       <configuration>
         <source>1.6</source>
         <target>1.6</target>
       </configuration>
     </plugin>
   </plugins>
 </pluginManagement>
 <plugins>
   <plugin>
     <groupId>org.codehaus.mojo</groupId>
     <artifactId>jaxb2-maven-plugin</artifactId>
     <version>1.5</version>
     <configuration>
       <outputDirectory>${project.basedir}/src/main/java</outputDirectory>
       <schemaDirectory>${project.basedir}/src/main/schemas/</schemaDirectory>
       <bindingDirectory>${project.basedir}/src/main/schemas/bindings/</bindingDirectory>
       <extension>true</extension>
       <forceRegenerate>true</forceRegenerate>
     </configuration>
     <executions>
       <execution>
         <id>xjc-xxx</id>
         <goals>
           <goal>xjc</goal>
         </goals>
         <configuration>
           <schemaFiles>XXXXXXX.xsd</schemaFiles>
         </configuration>
       </execution>
     </executions>
   </plugin>
 </plugins>
</build>

No comments:

Post a Comment