Buscar este blog

sábado, 9 de enero de 2016

Maven jaxb2 plugin - Generate class from XSD Schema with custom bindings file

In a previous post (http://trabajosdesisifo.blogspot.com.es/2015/04/generate-classes-from-xsd-xmlrootelement.html) I talked about how to generate java classes from XSDs and WSDLs files. When I worked with XSD Schema files I was using org.jvnet.jaxb2.maven2 / maven-jaxb2-plugin, now I post the equivalent with org.codehaus.mojo / maven-jaxb2-plugin a newer version.

My XSD and XJB files are located under /src/main/resources/META-INF/xsd/consultaVersion directory.
File consultaVersion.xsd:
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="aplicacion">
    <xs:complexType>
      <xs:sequence>
        <xs:element type="xs:string" name="nombre"/>
        <xs:element type="xs:string" name="version"/>
        <xs:element type="xs:date" name="fecha"/>
        <xs:element type="xs:string" name="observaciones"/>
        <xs:element type="xs:anyURI" name="urlDescarga"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

File bindings.xjb:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<jaxb:bindings version="2.1" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
 xmlns:xs="http://www.w3.org/2001/XMLSchema">

 <jaxb:globalBindings generateIsSetMethod="true" localScoping="toplevel">
  <jaxb:javaType name="java.util.Calendar" xmlType="xs:dateTime" parseMethod="javax.xml.bind.DatatypeConverter.parseDateTime" printMethod="javax.xml.bind.DatatypeConverter.printDateTime" />
 </jaxb:globalBindings>


 <jaxb:bindings schemaLocation="consultaVersion.xsd" node="/xs:schema">
  <jaxb:schemaBindings>
   <jaxb:package name="es.sisifo.myproject.framework.service.versionado.dto" />
  </jaxb:schemaBindings>
 </jaxb:bindings>

</jaxb:bindings>

Note that with this binding file dateTime fields are converted to java Calendar type.

And this is my pom.xml:
<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>es.sisifo.myproject</groupId>
 <artifactId>desktop-framwork</artifactId>
 <version>0.0.1-SNAPSHOT</version>

 <properties>
  (...)
  <java.version>1.7</java.version>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 </properties>

 <build>
  <pluginManagement>
   <plugins>
    <!--This plugin's configuration is used to store Eclipse m2e settings 
     only. It has no influence on the Maven build itself. -->
    <plugin>
     <groupId>org.eclipse.m2e</groupId>
     <artifactId>lifecycle-mapping</artifactId>
     <version>1.0.0</version>
     <configuration>
      <lifecycleMappingMetadata>
       <pluginExecutions>
        <pluginExecution>
         <pluginExecutionFilter>
          <groupId>
           org.codehaus.mojo
          </groupId>
          <artifactId>
           jaxb2-maven-plugin
          </artifactId>
          <versionRange>
           [2.2,)
          </versionRange>
          <goals>
           <goal>xjc</goal>
          </goals>
         </pluginExecutionFilter>
         <action>
          <ignore></ignore>
         </action>
        </pluginExecution>
       </pluginExecutions>
      </lifecycleMappingMetadata>
     </configuration>
    </plugin>
   </plugins>
  </pluginManagement>

  <plugins>
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.3</version>
    <configuration>
     <target>${java.version}</target>
     <source>${java.version}</source>
    </configuration>
   </plugin>
  
  
   <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>jaxb2-maven-plugin</artifactId>
    <version>2.2</version>
    <executions>
     <execution>
      <id>Generate-clases-xsd</id>
      <phase>generate-sources</phase>
      <goals>
       <goal>xjc</goal>
      </goals>
     </execution>
    </executions>
    <configuration>
     <outputDirectory>${basedir}/target/xjc/generated</outputDirectory>
     <sources>
      <source>${basedir}/src/main/resources/META-INF/xsd/consultaVersion/consultaVersion.xsd</source>
     </sources>
     <xjbSources>
      <xjbSource>${basedir}/src/main/resources/META-INF/xsd/consultaVersion/bindings.xjb</xjbSource>
     </xjbSources>
     <target>2.1</target>
     <generateEpisode>false</generateEpisode>
     <encoding>${project.build.sourceEncoding}</encoding>      
    </configuration>
   </plugin>
  </plugins>

 </build>

 <dependencies>
  (... Nothing special ...)
 </dependencies>
</project>


Aditional info:

No hay comentarios:

Publicar un comentario