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


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"/>

    <uptodate property="hibernateBuild.uptodate"
       <srcfiles refid="fileset.hbm.xml"/>

    <j:set var="buildHibernateFiles"

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

              <ant:taskdef name="hbm2java"

              <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:jar jarfile="${maven.src.dir}/generated/hbm.jar">
                  <fileset refid="fileset.hbm.xml"/>

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.

                  <taskdef name="hibernatetool"

                  <hbm2java output="src/generated">
                      <fileset dir="src/hibernate">
                          <include name="**/*.hbm.xml"/>

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

Next Pagearrow