In previous sections,we have learnt the widely used components of Hibernate. Our development environment was eclipse and a set of Hibernate libraries. Usually ant is used to build and run the Hibernate applications. In this section,we will configure our project to use ant  and build without any help of eclipse even from command line.


To start with ant:



  • Download and install the latest available version Apache Ant from http://ant.apache.org


  • Set environment variable JAVA_HOME to point to the available JDK location like d:\java\j2sdk1.4.2_04




  • Set environment variable ANT_HOME to point to the ant installation directory like d:\java\apache-ant-1.6.2




  • Add %ANT_HOME%\bin to your PATH environment variable so that the ant binaries are accessible.




Our ant installation is ready. Let's configure our project to use ant.


Create a directory lib at the root of our project and place the libraries required by our project in this folder. The libraries include all the Hibernate jar files we added in eclipse project libraries and the JDBC driver. See the directory structure below to find where to place the libraries.


Lib directory


Create an xml file at the root of the project. i.e. where the src and lib resides; and add the following text in this file.


<?xml version="1.0"?> <project name="hibernate-tutorial" default="run" basedir=".">    <!-- set global properties for this build -->   <property name="srcdir" value="src"/>   <property name="jardir" value="jars"/>   <property name="builddir" value="build"/>   <property name="libdir" value="lib"/>   <property name="package" value="com/visualbuilder/hibernate"/>     <path id="cp">     <fileset dir="${libdir}">        <include name="*.jar"/>        <include name="hibernate/*.jar"/>        <include name="hibernate/lib/*.jar"/>     </fileset>   </path>     <target name="init">     <mkdir dir="${builddir}"/>     <mkdir dir="${jardir}"/>   </target>     <target name="clean">     <delete quiet="true" dir="${builddir}"/>     <delete quiet="true" dir="${jardir}"/>   </target>     <target name="compile" depends="init">     <javac srcdir="${srcdir}" destdir="${builddir}" classpathref="cp">       <include name="${package}/*.java"/>       <include name="${package}/client/*.java"/>     </javac>   </target>     <target name="jar" depends="compile">     <jar destfile="${jardir}/app.jar">       <fileset dir="${builddir}">         <include name="${package}/**/*.class"/>       </fileset>       <fileset dir="${srcdir}">         <include name="${package}/*.hbm.xml"/>         <include name="*.cfg.xml"/>       </fileset>     </jar>   </target>     <target name="run" depends="jar">     <java classname="com.visualbuilder.hibernate.client.TestClient" classpathref="cp">       <classpath>         <pathelement location="${jardir}/app.jar" />       </classpath>     </java>   </target>   </project> 

This is the main build script that can be used to automatically build the source,copy the configuration files(*.hbm.xml and *.cfg.xml),build the jar file and execute the program. Open the command prompt,change your current directory to the project root,and issue the following command:
ant
The out put of this command is shown below:


E:\eclipseprojects\hibernate-tutorial>ant Buildfile: build.xml init: [mkdir] Created dir: E:\eclipseprojects\hibernate-tutorial\build [mkdir] Created dir: E:\eclipseprojects\hibernate-tutorial\jars compile: [javac] Compiling 4 source files to E:\eclipseprojects\hibernate-tutorial\build jar: [jar] Building jar: E:\eclipseprojects\hibernate-tutorial\jars\app.jar run: [java] log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment). [java] log4j:WARN Please initialize the log4j system properly. [java] User found with ID=14 [java]     Name=Elison [java]     Email=john@visualbuilder.com [java]             Number Type:Home [java]             Phone Number:934757 [java]             Number Type:Office [java]             Phone Number:934757 [java] User found with ID=17 [java]     Name=Elison [java]     Email=john@visualbuilder.com [java]             Number Type:Home [java]             Phone Number:934757 [java]             Number Type:Office [java]             Phone Number:934757 BUILD SUCCESSFUL 

So,you can see that the ant did a magic for us by doing all the configuration,including the necessary libraries and automatically executing the client program for us.

                    

Copyright © 2014 VisualBuilder. All rights reserved