Build.xml
Code:
<taskdef name="hibernatetool" classname="org.hibernate.tool.ant.HibernateToolTask" classpathref="toolslib" />
<target name = "hibernatetool" >
<echo message="Starting hbm generation ..."></echo>
<hibernatetool>
<jdbcconfiguration propertyfile="hibernate.properties" revengfile="revenge.xml" packagename="sample">
</jdbcconfiguration>
<classpath refid="toolslib"/>
<hbm2hbmxml destdir="new" templatepath="src">
</hbm2hbmxml>
</hibernatetool>
<echo message="Successfully generated hbm files"></echo>
<echo message="Java class generation starting..."></echo>
<hibernatetool>
<configuration propertyfile="hibernate.properties">
<fileset dir="sample" id="id">
<include name="*.hbm.xml"/>
</fileset>
</configuration>
<hbm2java destdir="new/java"/>
</hibernatetool>
<echo message="Successfully generated java files"></echo>
</target>
Java codeCode:
File buildFile = new File(file);
Project p = new Project();
String path = buildFile.getAbsolutePath();
path = path.replaceAll("%20", " ");
p.setUserProperty("ant.file", path);
DefaultLogger consoleLogger = new DefaultLogger();
consoleLogger.setErrorPrintStream(System.err);
consoleLogger.setOutputPrintStream(System.out);
consoleLogger.setMessageOutputLevel(Project.MSG_INFO);
p.addBuildListener(consoleLogger);
try {
p.fireBuildStarted();
p.init();
ProjectHelper helper = ProjectHelper.getProjectHelper();
p.addReference("ant.projectHelper", helper);
helper.parse(p, buildFile);
p.executeTarget(p.getDefaultTarget());
p.fireBuildFinished(null);
} catch (BuildException e) {
p.fireBuildFinished(e);
}
Don't forget to add all the dependency library files onto your classpath. Hope it helps!