Alfred’s Computing Weblog

Alfred Java-cored Computing Weblog

Ant | FTP built file

with one comment

I was asked to do synchronization for binaries files between different machine.
The build tool used in the projects is Apache Ant.

The architecture is look like this :
development machine ---> deployment machine (host) --->  development machine (mirror)

Developer(s) is coding in development machine, so that the latest code is keep in development machine.
(the code has to be committed to svn, but its out of today’s scope)
Currently, developer is using Apache Ant to build the project (JAR/WAR) files.
Due to development machine and deployment machine (host) is the same physical machine, developer use the Ant Copy task to copy the latest built JAR file from build path to deployment path.

My main task for today is to ‘copy’ the latest version in deployment host to deployment mirror.
Keep in mind, the deployment host and deployment mirror is not in same physical machine. Therefore, we can not use Ant-Copy in this case.
In normal linux environment, we have several options to send file from 1 host to another, such as FTP, SFTP, SCP and etc.
For this case, I choose Ant-Ftp to FTP the files from development machine to deployment mirror.
The code that I’m going to DISCLOSE below can perfectly do the mentioned task.

	<!-- Synchronise Deployment Mirror -->
	<target name="sync-deploy-mirror">
	    <antcall target="copyJarToTempDir"/>
		  <antcall target="ftpToMirror"/>
	    <antcall target="deleteTempDir"/>

	<!-- Copy required JARs to temp directory -->
	<target name="copyJarToTempDir" depends="main">
	    <copy file="${jar.dir}/my_proj_a.jar" tofile="${temp.dir}/1my_proj_a.jar.jar"/>
		  <copy file="${jar.dir}/my_proj_z.jar" tofile="${temp.dir}/0my_proj_z.jar.jar"/>

	<!-- FTP JARs from temp directory to remote host-->
	<target name="ftpToMirror">
      <ftp server="${deploy.mirror.ip}"
         <fileset dir="${temp.dir}">
             <include name="**/*.jar"/>

	<!-- Delete temp directory -->
	<target name="deleteTempDir">
	    <delete dir="${temp.dir}"/>

The main target of our case is sync-deploy-mirror.
Its like the main method in normal programming, it calls 3 sub-targets: copyJarToTempDir, ftpToMirror and deleteTempDir.

Target copyJarToTempDir, as named is to copy the latest built JAR file to a temporary folder.
Pls take a closer look at this line . It means this target is depends on another target called main.
Why it need to depends on another target?
Basically, the main target is defined to build the project. As defined in this way, Ant will runs the main target before it executes copyJarToTempDir.
Therefore I can ensure that I can get the latest built JAR file to copy to temporary folder.
Another question rises, why do I need to copy latest JAR to another folder? As I just want to FTP latest JAR to another machine.
This question will be answer shortly as we proceed to the target that doing FTP.

Target ftpToMirror is FTP files from local to remote host.
The main attributes like IP, username, password, etc are defined in ftp tag.
In FTP tag, it only supports fileset directory, which means it can FTP every files (including subfolder) under the defined fileset.
Due to some business requirement, the original jar name (in Ant build path) may not be same (name) with its copy in deployment machine.
Therefore, I need to do copyJarToTempDir target to copy all built (required in deployment) JAR file to a temporary file.
Then, FTP every file in temporary folder (those file should be named as in deployment machine) to deployment mirror.

Target deleteTempDir is like a helper target as its used to remove temporary folder as the folder is no longer useful after FTP is done.

Written by Alfred

February 19, 2009 at 12:10

Posted in ant

Tagged with , , , , ,

One Response

Subscribe to comments with RSS.

  1. I really loved that post, I am a little puzzled, and have a quick question. Can I send you an email?


    January 17, 2010 at 18:16

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: