28. Juni 2011

Java: JAXB: Extend generated Classes

We use the same maven2 configuration as mentioned in this post.

Now we add the following line to the JAXB-plugin configuration:

<bindingDirectory>${basedir}/xml/binding</bindingDirectory>

Our complete pom.xml now looks like here:

<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.my.package</groupId>
  <artifactId>my-jaxb-artifact</artifactId>
  <version>0.1</version>
  <dependencies>
<dependency>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics-testing</artifactId>
<version>0.5.2</version>
<scope>test</scope>
</dependency>
<dependency>
   <groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics</artifactId>
<version>0.5.2</version>
<type>jar</type>
   <scope>compile</scope>
</dependency>
  </dependencies>
<repositories>
<repository>
<id>maven2-repository.dev.java.net</id>
<url>http://download.java.net/maven/2</url>
</repository>
  </repositories>
<pluginRepositories>
<pluginRepository>
<id>maven2-repository.dev.java.net</id>
<url>http://download.java.net/maven/2</url>
</pluginRepository>
<pluginRepository>
<id>central</id>
<url>http://repo1.maven.org/maven2</url>
</pluginRepository>
</pluginRepositories>
<build>
     <plugins>
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.7.1</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>

<configuration>
<extension>true</extension>
<args>
<arg>-XtoString</arg>
<arg>-Xequals</arg>
<arg>-XhashCode</arg>
<arg>-Xcopyable</arg>
<arg>-Xmergeable</arg>
<arg>-Xinheritance</arg>
</args>

<bindingDirectory>${basedir}/xml/binding</bindingDirectory>
<schemaDirectory>${basedir}/src/main/resources/xsd</schemaDirectory>
<packagename>com.my.package</packagename>
<generateDirectory>${basedir}/target/generated-sources/xjc</generateDirectory>
<plugins>
<plugin>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics</artifactId>
<version>0.5.2</version>
</plugin>
</plugins>
</configuration>
</plugin>
<plugin>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.3.1</version>
        <executions>
          <execution>
            <id>default-jar</id>
            <phase>package</phase>
            <goals>
              <goal>jar</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
         <outputDirectory>${basedir}/build/libs</outputDirectory>
        </configuration>
      </plugin>
     </plugins>
</build>
<reporting>
  
  </reporting>
</project>


After that we create a file called "extend.binding.xml" in the folder "xml/binding/".
The file contains the following information:


<jxb:bindings
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
    xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
    xmlns:pd="http://chubb.com/cpi/polsvc/xmlobj"
    xmlns:inheritance="http://jaxb2-commons.dev.java.net/basic/inheritance"
    jxb:extensionBindingPrefixes="inheritance"
    version="2.1">
     <jxb:bindings schemaLocation="../../src/main/resources/xsd/schema.xsd">
        <jxb:bindings node="//xs:element[@name='main.object']/xs:complexType">
            <inheritance:extends>com.my.package.BaseClass</inheritance:extends>
        </jxb:bindings>
    </jxb:bindings>
</jxb:bindings>


The previous XML tells the xjc compiler that he should extend the class created for the xsd-object main.object from the class called com.my.package.BaseClass.

When running mvn generate-sources the BaseClass is subclassed by the generated class for main.object.

Java: JAXB Classes with maven2

With existing XSD-Files JAXB gives a very nice opportunity to autogenerate the fitting Java classes to use the xml-Data based on the mentioned XSD-files.

Based on the xsd objects JAXB (xjc) generates the Java Beans.

The following pom.xml enables JAXB Java source file generation via maven's "generate-sources" goal. The files are created at the directory specified by the "generateDirectory"-tag.


<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.my.package</groupId>
  <artifactId>my-jaxb-artifact</artifactId>
  <version>0.1</version>
  <dependencies>
<dependency>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics-testing</artifactId>
<version>0.5.2</version>
<scope>test</scope>
</dependency>
<dependency>
   <groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics</artifactId>
<version>0.5.2</version>
<type>jar</type>
   <scope>compile</scope>
</dependency>
  </dependencies>
<repositories>
<repository>
<id>maven2-repository.dev.java.net</id>
<url>http://download.java.net/maven/2</url>
</repository>
  </repositories>
<pluginRepositories>
<pluginRepository>
<id>maven2-repository.dev.java.net</id>
<url>http://download.java.net/maven/2</url>
</pluginRepository>
<pluginRepository>
<id>central</id>
<url>http://repo1.maven.org/maven2</url>
</pluginRepository>
</pluginRepositories>
<build>
     <plugins>
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.7.1</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>

<configuration>
<extension>true</extension>
<args>
<arg>-XtoString</arg>
<arg>-Xequals</arg>
<arg>-XhashCode</arg>
<arg>-Xcopyable</arg>
<arg>-Xmergeable</arg>
<arg>-Xinheritance</arg>
</args>

<schemaDirectory>${basedir}/src/main/resources/xsd</schemaDirectory>
<packagename>com.my.package</packagename>
<generateDirectory>${basedir}/target/generated-sources/xjc</generateDirectory>
<plugins>
<plugin>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics</artifactId>
<version>0.5.2</version>
</plugin>
</plugins>
</configuration>
</plugin>
<plugin>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.3.1</version>
        <executions>
          <execution>
            <id>default-jar</id>
            <phase>package</phase>
            <goals>
              <goal>jar</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
         <outputDirectory>${basedir}/build/libs</outputDirectory>
        </configuration>
      </plugin>
     </plugins>
</build>
</project>