ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

Hibernate Class Generation Using hbm2java
Pages: 1, 2, 3, 4, 5, 6

Customizing the Build Process with Maven 1

To integrate this into a Maven (1.0) build process, you need to modify your maven.xml file. The Maven code is presented here. The script basically checks whether the Hibernate mapping files have been changed since the last time the classes were generated (using the uptodate tag), and, if not, invokes the Ant hbm2java task described earlier. In this case, the following assumptions are made:



  • The hbm2java.xml configuration file is expected to be in the src/hibernate directory.
  • The Hibernate mapping files are expected to be in the src/hibernate directory.
  • The Java classes will be generated in the src/generated/src/java directory.
  
<goal name="generate-hibernate-classes">
    <ant:echo message="Hibernate class generation"/>

    <fileset dir="${basedir}/src" id="fileset.hbm.xml">
        <include name="**/*.hbm.xml"/>
    </fileset>

    <uptodate property="hibernateBuild.uptodate"
              targetfile="${maven.src.dir}/generated/hbm.jar">
       <srcfiles refid="fileset.hbm.xml"/>
    </uptodate>

    <j:set var="buildHibernateFiles"
           value="${hibernateBuild.uptodate}"/>

    <j:choose>
        <j:when test="${buildHibernateFiles != null}">
              <ant:echo message="Hibernate classes up to date"/>
        </j:when>
        <j:otherwise>
              <ant:echo message="Generating Hibernate classes to src/java"/>

              <ant:taskdef name="hbm2java"
                           classname="net.sf.hibernate.tool.hbm2java.Hbm2JavaTask"
                           classpathref="maven.dependency.classpath"/>

              <ant:hbm2java config="${maven.src.dir}/conf/hbm2java.xml"
                            output="${maven.src.dir}/generated/src/java" >
                  <ant:fileset dir="${maven.src.dir}/hibernate">
                      <ant:include name="**/*.hbm.xml"/>
                  </ant:fileset>
              </ant:hbm2java>

              <ant:jar jarfile="${maven.src.dir}/generated/hbm.jar">
                  <fileset refid="fileset.hbm.xml"/>
              </ant:jar>
        </j:otherwise>
    </j:choose>
</goal>
  
 

Customizing the Build Process with Maven 2

If you happen to be using Maven 2, things are a little simpler. Instead of using pre and post goals in the maven.xml file, you add a maven-antrun-plugin plugin to the pom.xml file. Within this plugin, in the tasks section, you can directly invoke the Ant tasks, described above.

  
<project...>
  <modelVersion>4.0.0</modelVersion>
  ...
  <build>
    ...
    <plugins>
       <plugin>
          <artifactId>maven-antrun-plugin</artifactId>
          <executions>
            <execution>
              <phase>generate-sources</phase>
              <configuration>
                <tasks>
                  <taskdef name="hibernatetool"
                           classname="org.hibernate.tool.ant.HibernateToolTask"
                           classpathref="maven.dependency.classpath"/>

                  <hbm2java output="src/generated">
                      <fileset dir="src/hibernate">
                          <include name="**/*.hbm.xml"/>
                      </fileset>
                  </hbm2java>
                </tasks>
              </configuration>
              <goals>
                <goal>run</goal>
              </goals>
            </execution>
          </executions>
        </plugin>
     </plugins>
  </build>
</project>
  

Pages: 1, 2, 3, 4, 5, 6

Next Pagearrow