Juan Garcia
Juan Garcia

Reputation: 706

How to execute HibernateToolTask from Ant when Hibernate Tools is a Maven dependency

I am using Hibernate Tools to generate pojo and dao in my project. It is currently working in the Hibernate perspective using Run > Hibernate Code Generation... However, I want to automatize this as part of a more complex build where I need to do some pre-processing, running the hibernate code genaration and doing some post-processing. I have an Ant build file for doing this, but I don't know how to reference the Maven dependencies jar

<?xml version="1.0" ?>
<!DOCTYPE project>
<project name="Hibernate Tools hbm2java" default="gensrc">

    <path id="tools">
        <!--
            Here {
        -->
        <path location="lib/hibernate-tools-4.3.1.CR1.jar"/>
        <!-- more dependencies... -->
        <!--
            }
        -->
        ...
    </path>
    <taskdef name="gen-src" classname="org.hibernate.tool.ant.HibernateToolTask"
             classpathref="tools" />
    <target name="gensrc">
        ...
    </target>
</project>

I am getting this warning:

taskdef class org.hibernate.tool.ant.HibernateToolTask cannot be found using the classloader AntClassLoader[]

with the consequent build error:

BUILD FAILED
/.../hibernate-gen.xml:16: taskdef class org.hibernate.tool.ant.HibernateToolTask cannot be found using the classloader AntClassLoader[]

How can I reference the jar from Maven dependency to call the org.hibernate.tool.ant.HibernateToolTask?

Upvotes: 0

Views: 2756

Answers (1)

Sunil Kumar
Sunil Kumar

Reputation: 5657

To automatate pojo generation,you can 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 by you.

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

Or else you can automate by generating pojo classes using Hibernate tool task pragmatically. Refer this git project to generate pojos from hbm.

Upvotes: 1

Related Questions