Whatterz


Using Ant with Eclipse

by Simon. Average Reading Time: almost 4 minutes.

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.

Eclipse started out as a Java IDE 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 Bolt.

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.

Ant uses XML to describe the build process and its dependencies, using a file commonly called build.xml. 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.

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.

Setting Up Eclipse

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 FTP to upload the files to a live server. The FTP ant task requires some extra libraries (.jar files):

commons-net-*.jar
jakarta-oro-*.jar

(The library files can be downloaded from here).

Copy the files into the ant lib folder of your Eclipse install. The folder is commonly located here:

C:\Program Files\Eclipse\plugins\org.apache.ant_*\lib

(The asterix * refers to the Ant version).

Next, go to Window > Preferences and select Ant > Runtime. In the Classpath tab, select Ant Home Entries, click Add External JARs… and select the 2 files you copied to the ant lib folder.

A Typical Ant Project Setup

A project with ant build scripts should have the following within the repository:

  • build.xml (the actual ant build script)
  • build.properties.template (a template for individual build.properties files)
  • an optional top level lib folder (containing jar files for external ant tasks)

Examples of the build.xml and build.properties.template files can be downloaded from here.

Create Your Own build.properties File

Make a copy of the build.properties.template 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 deploy.dir.local which is the path to the site on your local machine).

This file should never be committed to the repository (the repository should be set to ignore it anyway).

Basic Build Tasks

The build file should have a deploy.local 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 build.properties file).

Most projects should also have a deploy.dev task to copy the latest work to the dev server so that other people can view it.

Setting Up an Automatic Build

To make things easy, you can set deploy.local to run every time you save a file. This is called an automatic build.

The process for setting up automatic build is quite convoluted. I’m not sure why but this is the best way I’ve found to make it work.

Step 1: Add the build file to your ant view

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

Step 2: Run as Ant Build

In your ant view, right click the newly added build file and select Run As > Ant Build… In the dialog window, select the ‘Hide internal targets not selected for execution’ 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 ‘build.xml’. Click Apply and Close.

Step 3: Setup automatic build

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 ‘After a clean’ and deploy.local for ‘Auto build’. Apply the changes and close.

Step 4: Enable Auto Build

Go to Project > Build Automatically

Acknowledgment: Thanks should go to Martin Laine for his help and guidance which resulted in this article.

This article has been tagged

, , , , , , , , , , , , , , , , , , , , , , ,

Other articles I recommend

Apache Ant Best Practices

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.

Configuring PHP with Apache on Windows Vista

Installing PHP with Apache on Windows Vista is a relatively simple task until you try an configure the settings in php.ini. Problems can occur and stem from the fact that when you install PHP and edit the php.ini file, you need to not only be logged in as Administrator, but run the installer and Notepad text editor as Administrator. The php.ini file also needs to be located in the Windows directory.

Installing PHP with Apache on Windows Vista

Following on from my posts about installing Apache and ColdFusion on Vista, it is now the turn of PHP.

  • http://priest@thecrumb.com Jim Priest

    Good stuff – will add to my Ant wiki: http://www.thecrumb.com/wiki/ant

  • http://blog.alanklement.com Alan

    Do you use Ant to compile SWFs? I’m a Flash dev and I use FDT FB with Eclipse, but I juse edit code in FDT and then compile it with Flash CS4. Mostly because of my use of Flash CS4′s library assets I create ( graphics ). Does it make sense for me to use Ant?

  • http://www.simonwhatley.co.uk Simon

    @Alan You can use Ant to compile SWFs. Take a look at this article on Adobe’s Developer Connection:

    http://www.adobe.com/cfusion/communityengine/index.cfm?event=showdetails&productId=2&postId=12175

    or the following from DopeLogik:

    http://www.dopelogik.com/documents/actionscript_ant/

    or even the following from gunua:Flex:

    http://flex.gunua.com/?p=79

  • http://www.allenweb.com Allen

    Is there anything that can’t be done with Ant? :)

  • http://www.greencollective.nl latcho

    @Alan
    When you publish, in Flash CSx You can use “export SWC”
    the generated swc file you can include as a library in your ant build, all the fla symbol library classes can be instanced that way.
    It is also possible to link assets from an swf to variables in an `Assetmanager` by using the flex [embed tag] class,
    but then you have to manually link them in (every swf symbol one by one) everytime you update your swf library.

  • Pingback: Technical Related Notes » Blog Archive » links for 2010-04-12