Configuring Eclipse for Apache Ant FTP Buildfiles

This example uses Microsoft Windows XP Professional, Eclipse 3.2.1 and Ant 1.6.5 with the commons-net-1.4.1 and jakarta-oro-2.0.8 libararies

Overview

Eclipse is an open-source development tool for web and client applications. If you're developing a web application locally, you will eventually need to publish your application to a web server. Enter Apache Ant.

"Apache ant is a Java-based build tool." The standard installation of Eclipse (at the time of this writing) includes Ant 1.6.5. Using Ant, you can write a buildfile to perform tasks such as compiling your application or sending your files directly to the webserver using FTP. The FTP feature of Ant is not part of the standard Ant plugin that comes with Eclipse. Two extra libraries are needed; the Commons Net library and the ORO library.

The Commons Net library provides the necessary client side protocol access to FTP. The ORO library provides regular expression processing such as filename matching, substitution and filtering. For downloads and documentation on both libraries, see their respective sections on the Apache Jakarta Project website at http://jakarta.apache.org/.

Configuring Eclipse

Follow the steps below to configure Eclipse to use Ant FTP

  1. Download Eclipse
    3.2.1 http://www.eclipse.org/downloads/
  2. Unpack ("Install") Eclipse
    Extract the contents of the downloaded Zip file to your preferred destination directory, such as:
    C:\Eclipse
  3. Download the Commons Net library
    commons-net-1.4.1.zip
    http://jakarta.apache.org/site/downloads/downloads_commons-net.cgi
  4. Install the Commons Net library for the Ant plugin
    The important part of the zip file is the commons-net-1.4.1.jar file. Extract it to the Ant plugin lib directory under your Eclipse installation. For the above mentioned Eclipse installation, the Ant plugin lib directory is at:
    C:\Eclipse\plugins\org.apache.ant_1.6.5\lib
  5. Download the ORO library
    jakarta-oro-2.0.8.zip
    http://jakarta.apache.org/site/downloads/downloads_oro.cgi
  6. Install the ORO library for the Ant plugin
    The important part of the zip file is the jakarta-oro-2.0.8.jar file. Extract it to the Ant plugin lib directory under your Eclipse installation. For the above mentioned Eclipse installation, the Ant plugin lib directory is at:
    C:\Eclipse\plugins\org.apache.ant_1.6.5\lib
  7. Configure Eclipse for the new Ant libraries
    1. Open Eclipse
    2. Open the Preferences dialog by clicking Window -> Preferences...
    3. Traverse the tree on the left to expand Ant and select Runtime
    4. In the main dialog section on the Classpath tab, select Ant Home Entries
    5. Click the Add External JARs... button on the right
    6. Navigate to select the new commons-net-1.4.1.jar file
      (C:\Eclipse\plugins\org.apache.ant_1.6.5\lib\commons-net-1.4.1.jar)
    7. Repeat the last two steps to add the jakarta-oro-2.0.8.jar file
    8. Click the OK button to finish

 

Eclipse should now be properly configured to leverage the FTP functionality of Ant. That's only half the battle, though. Now you need to create an Ant buildfile for your project.

Creating an Ant FTP buildfile

An Ant buildfile is just a simple XML file. Add a new XML file and name it accordingly; something such as build.xml will do nicely. The contents of this XML file will tell Eclipse what actions to perform when building the application. Below is a sample buildfile.

<?xml version="1.0" encoding="UTF-8"?>
<project name="TestProject1" default="doFTP" basedir=".">
    <target name="doFTP" description="ftp files to mysite.com">   
    <echo>do the ftp...</echo>
    <input message="FTP to production?" validargs="y,n" addproperty="doFTP" defaultvalue="n" />
    <condition property="doAbort">
        <equals arg1="n" arg2="${doFTP}" />
    </condition>
    <fail if="doAbort">FTP cancelled</fail>
    <input addproperty="ftppass" message="FTP Password for admin:" />
    <ftp server="www.genuinejd.com"
            remotedir="/web/TestProject1"
            userid="admin"
            password="${ftppass}"
            depends="yes"
            binary="yes">
        <fileset dir="C:\eclipse\ide1\workspace\TestProject1">
            <include name="**"/>
        </fileset>
   
    </ftp>   
    <echo>Build completed</echo>
    </target>     
</project>

 

This buildfile asks the user to confirm the transfer of files. If y is entered, the user is prompted to enter the password for the user account admin on the ftp server at www.genuinejd.com The password is stored in a variable and passed to the FTP command in the Ant script. All the files in the project directory are then trasnferred to the web server.

Now that the buildfile has been created, we must use it to create a builder for the project. To do this, follow the steps below:

  1. Open the project properties
    Project -> Properties
  2. Click Builders on the left side of the project properties dialog
  3. Click the New... button on the right side of the dialog
  4. Select Ant Build as the configuration type and click OK to open the new builder configuration dialog
  5. Give the builder a meaningful name like Project Builder
  6. On the Main tab, in the Buildfile section, click the Browse Workspace... button to locate your new buildfile
  7. Select it from the current project and click OK
  8. Click the OK button to apply the new builder settings
  9. Click the OK button to apply the new builder to the project

 

That's it! You can also set the targets (on the Targets tab) for Auto Build and During a "Clean" should you want to FTP files EVERY time you make a change and save a file in your project. Should you want to disable the builder at any time, open the project properties dialog and un-check the builder in the Builders section

Review

Here are the basic steps to install, configure, and create an Ant FTP buildfile for Eclipse:

  1. Download and install Eclipse
  2. Download and install the additional libraries needed for Ant
  3. Configure the Eclipse Ant plugin to use the two new libraries
  4. Create an Ant buildfile
  5. Configure a new builder for the project using the new Ant buildfile

 

Resources

BlogCFC was created by Raymond Camden. This blog is running version 5.9.002. genuinejd.com