<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>FactoryPattern.com &#187; Ant</title>
	<atom:link href="http://www.factorypattern.com/category/ant/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.factorypattern.com</link>
	<description>Just another Object Oriented Weblog</description>
	<lastBuildDate>Thu, 26 Jan 2012 20:21:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>How To Use Ant</title>
		<link>http://www.factorypattern.com/how-to-use-ant/</link>
		<comments>http://www.factorypattern.com/how-to-use-ant/#comments</comments>
		<pubDate>Tue, 18 Mar 2008 15:05:50 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Ant]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[How To Ant]]></category>
		<category><![CDATA[Tutorial]]></category>

		<guid isPermaLink="false">http://www.factorypattern.com/how-to-use-ant/</guid>
		<description><![CDATA[Ant is a build tool written in Java, intended to be used for Java build processes. Ant is used for building small and large projects as well. You can use ant not only for automation of building Java projects, but for automating any complicated and repetitive tasks. Ant is using xml files to define tasks [...]]]></description>
			<content:encoded><![CDATA[<p>Ant is a build tool written in Java, intended to be used for Java build processes. Ant is used for building small and large projects as well. You can use ant not only for automation of building Java projects, but for automating any complicated and repetitive tasks. Ant is using xml files to define tasks that should be run during a process.<span id="more-13"></span></p>
<p><H3>How To Install Ant</H3></p>
<p>Ant can be installed on almost any operating system in a few easy steps:</p>
<ol>
<li>Install Java Runtime Environment(JRE) or Java Development Kit(JDK). Ant need a JRE or JDK in order to run.</li>
<li>2. Download the ant binary distribution from this <a href="http://ant.apache.org/bindownload.cgi">link</a> and unzip it to a directory of your choice.</li>
<li>3. Add the location of the bin folder to the path environment variable. For example in windows: <em>PATH=%PATH%;c:\ant\bin</em></li>
<li>4. Set ANT_HOME environment variable to the location where ant is installed. For example: <em>ANT_HOME=c:\ant</em></li>
</ol>
<p><H3>How to create your first ANT project</H3></p>
<p>Any ant project consist of one or more xml files containing the ant tasks. Lets create a small file named &#8220;build.xml&#8221;, containing only one task to print a small &#8220;Hello World!!!&#8221;. In order to run you have to go to the directory containing the build.xml file and type and run &#8220;ant&#8221;. </p>
<pre><code>&lt;project name="how-to-use-ant" basedir="." default="hello-world"&gt;

    &lt;target name="hello-world"&gt;
        &lt;echo message="Hello World!!!"/&gt;
    &lt;/target&gt;
	
&lt;/project&gt;</code></pre>
<p>As you noticed we defined in this and project  one task named <em>hello-world</em> and this is the default task. If everything goes well, the ant will run (we added in the path the ant bin directory when we installed it) and will print the following text:</p>
<pre><code>Buildfile: build.xml

hello-world:
     [echo] Hello World!!!

BUILD SUCCESSFUL
Total time: 0 seconds</code></pre>
<p><H3>How To Compile and Build Using Ant</H3></p>
<p>In real life we use Ant as the build tool for our applications and Ant is used in 90 percent of the cases for that. I&#8217;ve created a separate post to serve as a tutorial. You can check <a href="http://www.factorypattern.com/how-to-create-jar-files-from-ant/">How to Create Jar Files from Ant</a>.</p>
<p>Lets take this time in consideration a real life project. Most of the time Ant is used to compile and build java projects. This means generating, compiling and building jar files. For this we are going to define severals tasks. Lets consider the we create a buid.xml file in the directory where sources are located. The sources are put in the subdirectory called <em>src</em>. We also need to include a jar in the classpath while compiling. We are going to create a separate property file to define the location of this file:</p>
<pre><code>#===================================
#	File Name: build.properties
#===================================

env.LIB=c://java//thirdparty//lib</code></pre>
<p>The ant task contains a few tasks for doing the following operations:<br />
1. Clean the build directory if exists.<br />
2. Create the build directory where the new classes will be built.<br />
3. Compile the java classes.<br />
4. Build a jar file from the java classes.</p>
<p>Here is the build.xml file. Note the dependencies between the tasks and how the properties are defined in the ant xml file or loaded from the properties file:</p>
<pre><code>&lt;project name="how-to-use-ant" basedir="." default="hello-world"&gt;

	&lt;property name="build" value="build"/&gt;
	&lt;property file="build.properties"/&gt;
	
	&lt;target name="clean"&gt;
		&lt;delete dir="${build}"/&gt;
	&lt;/target&gt;

	&lt;target name="init" depends="clean"&gt;
		&lt;mkdir dir="${build}"/&gt;
	&lt;/target&gt;	
	
	&lt;target name="compile" depends="init"&gt;
		&lt;!-- Compile the java code --&gt;
		&lt;javac srcdir="src" destdir="${build}"&gt;
			&lt;classpath&gt;
				&lt;pathelement location="${env.LIB}/required-jar1.jar"/&gt;
				&lt;pathelement location="${env.LIB}/required-jar2.jar"/&gt;
			&lt;/classpath&gt;
		&lt;/javac&gt;
	&lt;/target&gt;	
	
	&lt;target name="jar" depends="compile"&gt;
		&lt;!-- Build the jar file --&gt;
		&lt;jar basedir="${build}" destfile="${build}/new-jar.jar"/&gt;
	&lt;/target&gt;
	
&lt;/project&gt;</code></pre>
<p>Now that you&#8217;ve seen how to create an ant task you might ask why you need an ant build file for building a jar file. This can be done as well with a few batch commands which will take only a 4 lines bat file. There are certain advantages. The ant files can be easily parametrized and reused, sub projects cand be created, lots of tasks are implemented, covering most of the operations you want to do (junits, sql operations, zip file tasks, tasks for replacing and editing text and xml files, &#8230;) and new tasks can be implemented in java and used in any ant file.</p>
<div style="text-align:center;"><a href="http://api.tweetmeme.com/share?url=http://www.factorypattern.com/how-to-use-ant/"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http://www.factorypattern.com/how-to-use-ant/" height="61" width="51" /></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.factorypattern.com/how-to-use-ant/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to write an Ant xml build file to Generate JAXB Code and Compile and Build it to a Jar</title>
		<link>http://www.factorypattern.com/how-to-write-ant-xml-build-file-generate-jaxb-code-compile-build-jar/</link>
		<comments>http://www.factorypattern.com/how-to-write-ant-xml-build-file-generate-jaxb-code-compile-build-jar/#comments</comments>
		<pubDate>Mon, 17 Mar 2008 15:06:35 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Ant]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Build]]></category>
		<category><![CDATA[compile]]></category>
		<category><![CDATA[jar]]></category>
		<category><![CDATA[Jaxb]]></category>
		<category><![CDATA[packaging]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://www.factorypattern.com/how-to-write-an-ant-xml-build-file-to-generate-jaxb-code-and-build-jar/</guid>
		<description><![CDATA[There is a pretty common situation to use a java classes generator and to create classes based on a defined pattern. Hibernate, Castor, Jaxb are a few examples. I prefer to have those classes packed in a separate jar file. Since they are generated files it&#8217;s not a good practice to modify them, so there [...]]]></description>
			<content:encoded><![CDATA[<p>There is a pretty common situation to use a java classes generator and to create classes based on a defined pattern. Hibernate, Castor, Jaxb are a few examples. I prefer to have those classes packed in a separate jar file. Since they are generated files it&#8217;s not a good practice to modify them, so there is not need to have them in your project. Debugging is the case when you need the source of generated classes. In this case you can simply attach the jar containing sources in your IDE (eclipse supports it, I think other IDE support it either). <span id="more-14"></span></p>
<p>Mainly you have to use 2 ant tasks after the java code is generated, one for compiling the classes, the other one to package them. In the following example I&#8217;m using ant to generate some jaxb classes, then to compile them and build a jar file. In this task there are 2 ant tasks, named compile and jar, responsible for compiling and generating the ant file:</p>
<pre><code>&lt;?xml version="1.0"?&gt;
&lt;project name="bbindings.jaxb" default="jar" basedir="."&gt;

&lt;property name="home" value="."/&gt;
&lt;property name="build" value="build"/&gt;
&lt;property name="logs" value="logs"/&gt;
&lt;property name="src" value="src"/&gt;
&lt;property file="build.properties"/&gt; 

&lt;target name="clean"&gt;
	&lt;delete dir="${logs}"/&gt;
	&lt;delete dir="${build}"/&gt;
	&lt;delete dir="${src}"/&gt;
&lt;/target&gt;

&lt;target name="init" depends="clean"&gt;
	&lt;mkdir dir="${build}"/&gt;
	&lt;mkdir dir="${logs}"/&gt;
	&lt;mkdir dir="${src}"/&gt;
&lt;/target&gt;

&lt;target name="copy_xsd" depends="init"&gt;
	&lt;copy file="${env.XSD_FILE}" todir="${src}"/&gt;
&lt;/target&gt;       

&lt;target name="generate_jaxb" depends="copy_xsd"&gt;
	&lt;java jar="${env.JWSDP_HOME_LIB}\jaxb-xjc.jar" fork="true" dir="${src}"&gt;
		&lt;arg value="-p"/&gt;
		&lt;arg value="com.bbindings.jaxb"/&gt;
		&lt;arg value="${home}/${env.XSD_FILE}"/&gt;
	&lt;/java&gt;
&lt;/target&gt;  

&lt;target name="compile" depends="init,generate_jaxb"&gt;
	&lt;!-- Compile the java code --&gt;
	&lt;javac srcdir="${src}" destdir="${build}"&gt;
		&lt;classpath&gt;
			&lt;pathelement location="${env.JWSDP_HOME_LIB}/jaxb-api.jar"/&gt;
			&lt;pathelement location="${env.JWSDP_HOME_LIB}/jaxb-impl.jar"/&gt;
			&lt;pathelement location="${env.JWSDP_HOME_LIB}/jaxb-xjc.jar"/&gt;
			&lt;pathelement location="${env.JWSDP_HOME_LIB}/jsr173_api.jar"/&gt;
		&lt;/classpath&gt;
	&lt;/javac&gt;
&lt;/target&gt;
  
&lt;target name="jar" depends="compile"&gt;
	&lt;!-- Build the jar file --&gt;
	&lt;jar basedir="${build}" destfile="${build}/${env.JAR_FILE}"/&gt;
&lt;/target&gt;

&lt;/project&gt;</code></pre>
<div style="text-align:center;"><a href="http://api.tweetmeme.com/share?url=http://www.factorypattern.com/how-to-write-ant-xml-build-file-generate-jaxb-code-compile-build-jar/"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http://www.factorypattern.com/how-to-write-ant-xml-build-file-generate-jaxb-code-compile-build-jar/" height="61" width="51" /></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.factorypattern.com/how-to-write-ant-xml-build-file-generate-jaxb-code-compile-build-jar/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>How to Create Jar Files from Ant</title>
		<link>http://www.factorypattern.com/how-to-create-jar-files-from-ant/</link>
		<comments>http://www.factorypattern.com/how-to-create-jar-files-from-ant/#comments</comments>
		<pubDate>Fri, 29 Feb 2008 07:06:09 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Ant]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[compile]]></category>
		<category><![CDATA[jar]]></category>
		<category><![CDATA[packaging]]></category>

		<guid isPermaLink="false">http://www.factorypattern.com/how-to-create-jar-files-from-ant/</guid>
		<description><![CDATA[Lets take this time in consideration a real life project. Most of the time Ant is used to compile and build java projects. This means generating, compiling and building jar files. For this we are going to define severals tasks. Lets consider the we create a buid.xml file in the directory where sources are located. [...]]]></description>
			<content:encoded><![CDATA[<p>Lets take this time in consideration a real life project. Most of the time Ant is used to compile and build java projects. This means generating, compiling and building jar files. For this we are going to define severals tasks. Lets consider the we create a buid.xml file in the directory where sources are located. The sources are put in the subdirectory called <em>src</em>. We also need to include a jar in the classpath while compiling. We are going to create a separate property file to define the location of this file:<span id="more-10"></span></p>
<pre><code>#===================================
#	File Name: build.properties
#===================================

env.LIB=c://java//thirdparty//lib</code></pre>
<p>The ant task contains a few tasks for doing the following operations:<br />
1. Clean the build directory if exists.<br />
2. Create the build directory where the new classes will be built.<br />
3. Compile the java classes.<br />
4. Build a jar file from the java classes.</p>
<p>Here is the build.xml file. Note the dependencies between the tasks and how the properties are defined in the ant xml file or loaded from the properties file:</p>
<pre><code>&lt;project name="how-to-use-ant" basedir="." default="jar"&gt;

	&lt;property name="build" value="build"/&gt;
	&lt;property file="build.properties"/&gt;
	
	&lt;target name="clean"&gt;
		&lt;delete dir="${build}"/&gt;
	&lt;/target&gt;

	&lt;target name="init" depends="clean"&gt;
		&lt;mkdir dir="${build}"/&gt;
	&lt;/target&gt;	
	
	&lt;target name="compile" depends="init"&gt;
		&lt;!-- Compile the java code --&gt;
		&lt;javac srcdir="src" destdir="${build}"&gt;
			&lt;classpath&gt;
				&lt;pathelement location="${env.LIB}/required-jar1.jar"/&gt;
				&lt;pathelement location="${env.LIB}/required-jar2.jar"/&gt;
			&lt;/classpath&gt;
		&lt;/javac&gt;
	&lt;/target&gt;	
	
	&lt;target name="jar" depends="compile"&gt;
		&lt;!-- Build the jar file --&gt;
		&lt;jar basedir="${build}" destfile="${build}/new-jar.jar"/&gt;
	&lt;/target&gt;
	
&lt;/project&gt;</code></pre>
<p>Now that you&#8217;ve seen how to create an ant task you might ask why you need an ant build file for building a jar file. This can be done as well with a few batch commands which will take only a 4 lines bat file. There are certain advantages. The ant files can be easily parametrized and reused, sub projects cand be created, lots of tasks are implemented, covering most of the operations you want to do (junits, sql operations, zip file tasks, tasks for replacing and editing text and xml files, &#8230;) and new tasks can be implemented in java and used in any ant file.</p>
<div style="text-align:center;"><a href="http://api.tweetmeme.com/share?url=http://www.factorypattern.com/how-to-create-jar-files-from-ant/"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http://www.factorypattern.com/how-to-create-jar-files-from-ant/" height="61" width="51" /></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.factorypattern.com/how-to-create-jar-files-from-ant/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Howto generate Hibernate POJO and mapping files using ant from a db schema</title>
		<link>http://www.factorypattern.com/howto-generate-hibernate-pojo-and-mapping-files-using-ant-from-a-db-schema/</link>
		<comments>http://www.factorypattern.com/howto-generate-hibernate-pojo-and-mapping-files-using-ant-from-a-db-schema/#comments</comments>
		<pubDate>Thu, 07 Feb 2008 18:18:06 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Ant]]></category>
		<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://www.factorypattern.com/howto-generate-hibernate-pojo-and-mapping-files-using-ant-from-a-db-schema/</guid>
		<description><![CDATA[This is the ant task I&#8217;m using to generate POJO and mapping files from a DB Schema (mysql). &#60;project name="springapp" basedir="." default="gen_hibernate"&#62; &#60;taskdef name="hibernatetool" classname="org.hibernate.tool.ant.HibernateToolTask"&#62; &#60;classpath&#62; &#60;fileset dir="lib"&#62; &#60;include name="**/*.jar"/&#62; &#60;/fileset&#62; &#60;/classpath&#62; &#60;/taskdef&#62; &#60;target name="gen_hibernate" description="generate hibernate classes"&#62; &#60;hibernatetool&#62; &#60;jdbcconfiguration configurationfile="hibernate.cfg.xml" packagename="com.openversion.bus" detectmanytomany="true" /&#62; &#60;hbm2hbmxml destdir="src" /&#62; &#60;hbm2java destdir="src" /&#62; &#60;/hibernatetool&#62; &#60;/target&#62; &#60;/project&#62; The following [...]]]></description>
			<content:encoded><![CDATA[<p>This is the ant task I&#8217;m using to generate POJO and mapping files from a DB Schema (mysql).<span id="more-5"></span></p>
<pre><code>&lt;project name="springapp" basedir="." default="gen_hibernate"&gt;

	&lt;taskdef name="hibernatetool"
			classname="org.hibernate.tool.ant.HibernateToolTask"&gt; 
			&lt;classpath&gt;
				&lt;fileset dir="lib"&gt;
					&lt;include name="**/*.jar"/&gt;
				&lt;/fileset&gt;
			&lt;/classpath&gt;
	&lt;/taskdef&gt; 

	&lt;target name="gen_hibernate"
			description="generate hibernate classes"&gt;
		&lt;hibernatetool&gt;
		
			&lt;jdbcconfiguration
				configurationfile="hibernate.cfg.xml"
				packagename="com.openversion.bus"  
				detectmanytomany="true"
			/&gt;
			&lt;hbm2hbmxml destdir="src" /&gt; 
			&lt;hbm2java  destdir="src" /&gt;
		&lt;/hibernatetool&gt;
	&lt;/target&gt;

&lt;/project&gt;</code></pre>
<p>The following files should be in the lib directory added to classpath (the <strong>taskdef</strong> section in the above ant task file). Another way to use them is to copy directly in the ant lib folder.</p>
<pre><code>commons-collections.jar
commons-logging.jar
dom4j-1.6.1.jar
freemarker.jar
hibernate-annotations.jar
hibernate-tools.jar
hibernate3.jar
jtidy-r8-20060801.jar
log4j-1.2.14.jar
mysql-connector-java-5.1.5-bin.jar</code></pre>
<p>If you want to generate POJO files and Hibernate .hbm.xml mapping files not for all the tables, then a new hibernate reverse engineering file has to be created specifying the table that should be used and it should be used in the ant script:</p>
<pre><code>...
	&lt;jdbcconfiguration
		configurationfile="hibernate.cfg.xml"
		packagename="com.openversion.bus"
		revengfile="tables.reveng.xml"
		detectmanytomany="true"/&gt;
...</code></pre>
<p>And here is sample tables.reveng.xml file inlcuding all the tables using a matching pattern <strong>match-name=&#8221;.*&#8221;</strong>. If you want to select only a few tables you can filter by putting <strong>match-name=&#8221;table_name&#8221;</strong> and add as many <strong>table-filter</strong> tags as many tables you want to include. You also should take care what because the tables names are case sensitive, I lost some time figuring it out.</p>
<pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;!DOCTYPE hibernate-reverse-engineering PUBLIC 
	"-//Hibernate/Hibernate Reverse Engineering DTD 3.0//EN"
	"http://hibernate.sourceforge.net/hibernate-reverse-engineering-3.0.dtd" &gt;

&lt;hibernate-reverse-engineering&gt;
	&lt;type-mapping&gt;
		&lt;sql-type jdbc-type="BIGINT" hibernate-type="java.lang.Long" not-null="true"&gt;&lt;/sql-type&gt;
	&lt;/type-mapping&gt;
	&lt;table-filter match-name=".*" match-catalog="openversion"&gt;&lt;/table-filter&gt;
&lt;/hibernate-reverse-engineering&gt;</code></pre>
<p>For further reference you can check <a href="http://www.hibernate.org/hib_docs/tools/reference/en/html/ant.html">Chapter 4. Ant Tools</a> and if you need more details about reverse engineering file () you can check <a href="http://www.hibernate.org/hib_docs/tools/reference/en/html/reverseengineering.html">Chapter 5. Controlling reverse engineering</a> of the <a href="http://www.hibernate.org/hib_docs/tools/reference/en/html/index.html">Hibernate Tools Reference Guide</a>.</p>
<div style="text-align:center;"><a href="http://api.tweetmeme.com/share?url=http://www.factorypattern.com/howto-generate-hibernate-pojo-and-mapping-files-using-ant-from-a-db-schema/"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http://www.factorypattern.com/howto-generate-hibernate-pojo-and-mapping-files-using-ant-from-a-db-schema/" height="61" width="51" /></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.factorypattern.com/howto-generate-hibernate-pojo-and-mapping-files-using-ant-from-a-db-schema/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

