<?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>Simon Whatley &#187; Apache Ant</title>
	<atom:link href="http://www.simonwhatley.co.uk/tag/apache-ant/feed" rel="self" type="application/rss+xml" />
	<link>http://www.simonwhatley.co.uk</link>
	<description>The opposite of every great idea is another great idea</description>
	<lastBuildDate>Wed, 02 Nov 2011 09:28:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Apache Ant Best Practices</title>
		<link>http://www.simonwhatley.co.uk/apache-ant-best-practices</link>
		<comments>http://www.simonwhatley.co.uk/apache-ant-best-practices#comments</comments>
		<pubDate>Mon, 10 Aug 2009 09:12:40 +0000</pubDate>
		<dc:creator>Simon</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Apache Ant]]></category>
		<category><![CDATA[automated build]]></category>
		<category><![CDATA[best practices]]></category>
		<category><![CDATA[build scripts]]></category>
		<category><![CDATA[immediate processing]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[local development server]]></category>
		<category><![CDATA[similar build tool]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[The Definitive Guide]]></category>
		<category><![CDATA[user acceptance testing server]]></category>
		<category><![CDATA[web applications]]></category>
		<category><![CDATA[web development]]></category>
		<category><![CDATA[XML]]></category>
		<category><![CDATA[Yahoo]]></category>

		<guid isPermaLink="false">http://www.simonwhatley.co.uk/?p=1831</guid>
		<description><![CDATA[Before Ant, building and deploying web applications required a series of scripts or manual processes, which often led to mistakes. Apache Ant is a software tool for automating software build processes. It is similar to Make but is implemented using the Java language, requires the Java platform, and is best suited to building Java projects. However, that doesn’t mean it is restricted to Java projects. I use Ant increasingly for all my web development projects as it is an integral part of Eclipse, my IDE of choice. It makes building applications and releasing them across different servers far more efficient and less problematic.]]></description>
			<content:encoded><![CDATA[<p>Before Ant, building and deploying web applications required a series of scripts or manual processes, which often led to mistakes.</p>
<p>Apache <a href="http://ant.apache.org" title="Apache Ant" target="_blank" rel="nofollow">Ant</a> is a software tool for automating software build processes. It is similar to Make but is implemented using the Java language, requires the Java platform, and is best suited to building Java projects. However, that doesn&#8217;t mean it is restricted to Java projects. I use Ant increasingly for all my web development projects as it is an integral part of <a href="http://eclipse.org" title="Eclipse" target="_blank" rel="nofollow">Eclipse</a>, my <abbr title="Integrated Development Environment">IDE</abbr> of choice. It makes building applications and releasing them across different servers far more efficient and less problematic.</p>
<p>When I start a new project, after creating the folder structure, I create the Ant build file. Ant defines the build process and must be used by every developer working on the project. All of the tips in this article assume that the Ant build file is extremely important, must be written with care, and maintained in version control and re-factored periodically, when the project dependencies change.</p>
<p>Ant uses <abbr title="eXtensible Markup Language">XML</abbr> to describe the build process and its dependencies. By default the <abbr title="eXtensible Markup Language">XML</abbr> file is named <code>build.xml</code>. This makes it really simple for you setup and edit different build processes. However, since Ant is relatively simple to set up, you need to follow a common set of best practices. Some of these best practices were inspired by mistakes made on previous projects, or ideas passed on to me by other developers.</p>
<h3>Automate absolutely everything</h3>
<p>Ant is a powerful tool. If you choose to use it, get it to do absolutely everything. Ant can integrate with your source control databases such as Subversion (<abbr title="Subversion">SVN</abbr>), run <abbr title="Structured Query Language">SQL</abbr> scripts, change file permissions, send files across <abbr title="File Transfer Protocol">FTP</abbr>, zip and un-zip files, and many more tasks besides these.</p>
<h3>If you use Ant, only use Ant</h3>
<p>Use Ant as a common baseline. Regardless of what <abbr title="Integrated Development Environment">IDE</abbr> you use to develop your applications, set up an Ant build file that <strong>all</strong> team members use. The <code>build.xml</code> file is the one true build script. Make a rule that everyone should perform a successful Ant build before code is checked into version control. This will ensure that code will always build from the same build file.</p>
<h3>Follow consistent conventions</h3>
<p>Ant expects your build file to be called <code>build.xml</code> and the build&#8217;s properties file <code>build.properties</code>. These files should also be in the root directory of your project. Other than an insane desire to change convention and confuse other team members, don&#8217;t change this convention. Another less-well-known convention is to prefix all internal commands with a hyphen (-). It has the advantage that it is not possible to invoke targets that follow this naming convention, from the command line.</p>
<p>Spend time formatting the build file. Since <abbr title="eXtensible Markup Language">XML</abbr> is quite verbose, use tabs and line breaks to make the file readable to the human eye. Ant itself doesn&#8217;t care if the file looks pretty, but you can bet that you and your team will.</p>
<p>Pick meaningful, human-readable names for targets and properties. For example <em>dir.reports</em> is better than simple <em>rpt</em>. The specific naming convention is not important, beyond being meaningful to your organisation. For example, I use the following:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">deploy.local
deploy.dev
deploy.test
deploy.live
-build
-clean
-concatenate
-minify
-prepare</pre></div></div>

<p>The <em>-concatenate</em> and <em>-minify</em> commands are specific to web projects, the later using the Yahoo compressor library to minify <abbr ttile="Cascading Stylesheets">CSS</abbr> and JavaScript files.</p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-6475233631580417";
/* 468x60 Basic */
google_ad_slot = "7117418273";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
<h3>Provide a clean target</h3>
<p>Every build file should include a target that removes all generated files and directories, bringing everything back to its original pristine state. All files remaining after the clean should be those found in version control.</p>
<p>An example clean command could be as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;target</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;clean&quot;</span> <span style="color: #000066;">description</span>=<span style="color: #ff0000;">&quot;Deletes all generated files and directories&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;delete</span> <span style="color: #000066;">dir</span>=<span style="color: #ff0000;">&quot;${dir.build}&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;delete</span> <span style="color: #000066;">dir</span>=<span style="color: #ff0000;">&quot;${dir.dist}&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/target<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<h3>Use build files for all stages of the development process (development, staging and production)</h3>
<p>Ant standardises your build and release cycle. Therefore use it for all stages of the development process. Use it for releasing code to your local development server, your staging or user acceptance testing server and your production server. The different deployments can obviously differ slightly based upon the needs, i.e. your production build need not include the creation of dummy data, but may include a call to source control.</p>
<h3>Make build files self-contained</h3>
<p>A build file that relies on external dependencies is one that will be difficult to configure or has the potential to cause problems. If your build depends on additional tools, put them in your source control repository. I generally include a <em>lib</em> for all dependencies in my project root. This folder includes such files as the <a href="http://developer.yahoo.com/yui/compressor/" title="Yahoo Compressor" target="_blank" rel="nofollow">Yahoo Compressor</a> or <a href="http://www.jslint.com" title="JSLint" target="_blank" rel="nofollow">JSLint</a>.</p>
<h3>Prefer a single build file</h3>
<p>It is possible to split the build up into several small build files, each of which is responsible for a small proportion of the overall build. This isn&#8217;t always the best idea! Breaking the build often makes it harder to comprehend the whole process. It is better not to over-engineer the project and keep to a single, well formatted build file.</p>
<p>If your project is split into different build files, there should always be a master build file, found in the root directory of the project, even if it only delegates actual work to subordinate builds.</p>
<h3>Put the build.xml file in the project&#8217;s root directory</h3>
<p>The Ant build file can reside anywhere in the project tree, but conceptually it makes sense to put the file in the project root. This keeps things clean, simple and somewhat obvious to new members to the project. Having a build file in the top-level directory also makes it conceptually easy to see how relative paths point to different directories in the project tree.</p>
<p>When the <code>build.xml</code> file is in the top-level directory, you can compile code from the command line without the need to change the working directory.</p>
<h3>Modularise your project</h3>
<p>Arrange your project into coherent self-contain modules.</p>
<p>The following shows how I typically arrange my project files.</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">[root directory]
	/docs
	/lib
	/src
build.properties
build.xml
task.properties</pre></div></div>

<p> The <em>docs</em> folder contains project documentation, the <em>lib</em> folder contains libraries specific to the project and required by Ant (generally this is a link to repository containing a number of utility JAR files, such as those for <abbr title="Subversion">SVN</abbr> and <abbr title="File Transfer Protocol">FTP</abbr>). Finally, the <em>src</em> folder contains the actual project files.</p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-6475233631580417";
/* 468x60 Basic */
google_ad_slot = "7117418273";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
<h3>Use version control</h3>
<p>It is important to use version control for your entire project. The build file is an important artefact that needs to be versioned along with the source code of the project. When you tag a build for release, the build file should also be included. If you then need to roll back to a previous release, you will be able to build the code based upon the build file used for that particular release (after all, build files develop along with changes to the source code).</p>
<p>Third-party <abbr title="Java Archive">JAR</abbr> and executable files should also be maintained in your local version control. This makes it possible to recreate previous releases, after all third-party libraries are likely to develop and change as frequently as your own code. If you want or need to take advantage of these changes, it is necessary to version control the older versions.</p>
<p>Avoid including build output in version control. Provided that you use version control correctly, you will be able to recreate these files at a later date.</p>
<h3>Include comments in the build file (self-documenting)</h3>
<p>Make the build file self-documenting. Adding target descriptions is one way to achieve this. For example:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;target</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;clean&quot;</span> <span style="color: #000066;">description</span>=<span style="color: #ff0000;">&quot;Deletes all generated files and directories&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span></pre></div></div>

<p>Internal targets should not include description attributes. Internal targets may include targets that perform immediate processing, such as file concatenation or minifying.</p>
<p>Another way to include help in the build file is the <code>echo</code> tag. You can put any amount of comment between these tags and the information is printed to screen. For example:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;target</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;clean&quot;</span> <span style="color: #000066;">description</span>=<span style="color: #ff0000;">&quot;Deletes all generated files and directories&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;echo<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Cleaning project...<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/echo<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;echo<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Deleting build directory...<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/echo<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;delete</span> <span style="color: #000066;">dir</span>=<span style="color: #ff0000;">&quot;${dir.build}&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;echo<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Deleting dist directory...<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/echo<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;delete</span> <span style="color: #000066;">dir</span>=<span style="color: #ff0000;">&quot;${dir.dist}&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/target<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<h3>Summary</h3>
<p>By using Ant and creating and maintaining build scripts for a project, the reliance on ad-hoc manual procedures for compiling and creating websites is removed. Using a defined process with Ant, or similar build tool, removes errors across the entire project, whilst allowing teams to develop code more efficiently.</p>
<h3>References</h3>
<p>Holzner, Steve (2005). Ant: The Definitive Guide, 2nd Edition, O&#8217;Reilly, ISBN 978-0-596-00609-9.</p>
<p>Apache Ant &#8211; <a href="http://ant.apache.org" title="Apache Ant" target="_blank" rel="nofollow">http://ant.apache.org</a></p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-6475233631580417";
/* 468x60 Basic */
google_ad_slot = "7117418273";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
]]></content:encoded>
			<wfw:commentRss>http://www.simonwhatley.co.uk/apache-ant-best-practices/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using Ant with Eclipse</title>
		<link>http://www.simonwhatley.co.uk/using-ant-with-eclipse</link>
		<comments>http://www.simonwhatley.co.uk/using-ant-with-eclipse#comments</comments>
		<pubDate>Tue, 06 Jan 2009 11:16:44 +0000</pubDate>
		<dc:creator>Simon</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Adobe]]></category>
		<category><![CDATA[Ant]]></category>
		<category><![CDATA[Apache]]></category>
		<category><![CDATA[Apache Ant]]></category>
		<category><![CDATA[build]]></category>
		<category><![CDATA[build.xml]]></category>
		<category><![CDATA[CFEclipse]]></category>
		<category><![CDATA[Click Import]]></category>
		<category><![CDATA[ColdFusion]]></category>
		<category><![CDATA[dev server]]></category>
		<category><![CDATA[Eclipse]]></category>
		<category><![CDATA[extreme programming]]></category>
		<category><![CDATA[ide]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[lifecycle]]></category>
		<category><![CDATA[Martin Laine]]></category>
		<category><![CDATA[platform-independent tool]]></category>
		<category><![CDATA[software automation]]></category>
		<category><![CDATA[test-driven development]]></category>
		<category><![CDATA[unit testing frameworks]]></category>
		<category><![CDATA[web developers]]></category>
		<category><![CDATA[Web Server]]></category>
		<category><![CDATA[web server folder]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://www.simonwhatley.co.uk/?p=1552</guid>
		<description><![CDATA[If you're currently not using Eclipse as your development tool of choice, you certainly should be! Eclipse is an open source community whose projects are focused on building an open development platform comprised of extensible frameworks, tools and runtimes for building, deploying and managing software across the lifecycle.]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re currently not using Eclipse as your development tool of choice, you certainly should be! Eclipse is an open source community whose projects are focused on building an open development platform comprised of extensible frameworks, tools and runtimes for building, deploying and managing software across the lifecycle.</p>
<p>Eclipse started out as a Java <abbr title="Integrated Development Environment">IDE</abbr> and has continually grown from there. Plugins now include, amongst a plethora of others, the venerable Aptana, CFEclipse, SQL Explorer, Subclipse and, more recently, the Adobe-developed Flex Builder and ColdFusion-equivalent codenamed <q>Bolt</q>.</p>
<p>Apache Ant is a software tool for automating software build processes. It is implemented using the Java language, requires the Java platform, and was originally developed to automate the build of Java projects. However, since Ant was created as a simple, platform-independent tool, it can really be used to automate the build of anything you choose.</p>
<p>Ant uses <abbr title="eXtensible Markup Language">XML</abbr> to describe the build process and its dependencies, using a file commonly called <code>build.xml</code>. Using this file, Ant makes it trivial to integrate unit testing frameworks with the build process and has made it easy for web developers to adopt test-driven development, and even Extreme Programming.</p>
<p>Fortunately for us, if you download and run Eclipse, you already have Ant installed and so do not have any complex configuration to concern yourself with; well, at least initially.</p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-6475233631580417";
/* 468x60 Basic */
google_ad_slot = "7117418273";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
<h3>Setting Up Eclipse</h3>
<p>For the most part, Eclipse has all you need to get up and running with Ant pre-installed. However, most projects will include a release target which uses <abbr title="File Transfer Protocol">FTP</abbr> to upload the files to a live server. The <abbr title="File Transfer Protocol">FTP</abbr> ant task requires some extra libraries (.jar files):</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">commons-net-*.jar
jakarta-oro-*.jar</pre></div></div>

<p>(The library <a href="http://www.simonwhatley.co.uk/examples/ant/">files can be downloaded from here</a>).</p>
<p>Copy the files into the ant lib folder of your Eclipse install. The folder is commonly located here:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">C:\Program Files\Eclipse\plugins\org.apache.ant_*\lib</pre></div></div>

<p>(The asterix * refers to the Ant version).</p>
<p>Next, go to Window > Preferences and select Ant > Runtime. In the Classpath tab, select Ant Home Entries, click Add External JARs&#8230; and select the 2 files you copied to the ant lib folder.</p>
<h3>A Typical Ant Project Setup</h3>
<p>A project with ant build scripts should have the following within the repository:</p>
<ul>
<li>build.xml (the actual ant build script)</li>
<li>build.properties.template (a template for individual build.properties files)</li>
<li>an optional top level lib folder (containing jar files for external ant tasks)</li>
</ul>
<p>Examples of the <code>build.xml</code> and <code>build.properties.template</code> <a href="http://www.simonwhatley.co.uk/examples/ant/">files can be downloaded from here</a>.</p>
<h3>Create Your Own build.properties File</h3>
<p>Make a copy of the <code>build.properties.template</code> file (name it build.properties) and set the values to fit your local setup (in most cases, the only property you may want to change is <code>deploy.dir.local</code> which is the path to the site on your local machine).</p>
<p>This file should never be committed to the repository (the repository should be set to ignore it anyway).</p>
<h3>Basic Build Tasks</h3>
<p>The build file should have a <code>deploy.local</code> task. This task builds the project and copies it to your local webserver (the path to the web server folder will be defined in the <code>build.properties</code> file).</p>
<p>Most projects should also have a <code>deploy.dev</code> task to copy the latest work to the dev server so that other people can view it.</p>
<h3>Setting Up an Automatic Build</h3>
<p>To make things easy, you can set deploy.local to run every time you save a file. This is called an <q>automatic build</q>.</p>
<p>The process for setting up automatic build is quite convoluted. I&#8217;m not sure why but this is the best way I&#8217;ve found to make it work.</p>
<p><strong>Step 1: Add the build file to your ant view</strong></p>
<p>Click the add button and select the build.xml file from the project. You can also drag the build.xml file onto the ant view (To add the ant view to your perpective, go to Window > Show View > Other…)</p>
<p><strong>Step 2: Run as Ant Build</strong></p>
<p>In your ant view, right click the newly added build file and select Run As > Ant Build… In the dialog window, select the &#8216;Hide internal targets not selected for execution&#8217; option in the Targets tab. You can rename the builder in the box at the top of the window if you wish (I usually remove the trailing &#8216;build.xml&#8217;. Click Apply and Close.</p>
<p><strong>Step 3: Setup automatic build</strong></p>
<p>Right click on the project, select Properties, then Builders. Click Import. Before closing the screen, highlight the imported build and select edit. Go to the Targets tab select clean for &#8216;After a clean&#8217; and deploy.local for &#8216;Auto build&#8217;. Apply the changes and close.</p>
<p><strong>Step 4: Enable Auto Build</strong></p>
<p>Go to Project > Build Automatically</p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-6475233631580417";
/* 468x60 Basic */
google_ad_slot = "7117418273";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
<p><strong> Acknowledgment:</strong> Thanks should go to <a href="http://www.1pixelout.net/" title="1PixelOut: Martin Laine's online musings" target="_blank" rel="nofollow">Martin Laine</a> for his help and guidance which resulted in this article.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.simonwhatley.co.uk/using-ant-with-eclipse/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

