How do I build a versioned war file?
Author: Deron Eriksson
Description: This Ant tutorial shows how to build a versioned war file using a customized task
Tutorial created using: Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 1.5.1


Page: < 1 2 3 >

(Continued from page 1)

The 'tomcat-demo' project before executing our AntSW script is shown below.

'tomcat-demo' project in Eclipse Navigator

We can execute our 'war-version' target on 'tomcat-demo' by adding the 'tomcat-demo' build.xml file to our EclipseSW Ant view and double-clicking on it.

Eclipse Ant View

Running 'war-version' sends the following results to the console.

Buildfile: C:\projects\workspace\tomcat-demo\build.xml
      [var] Setting 'war-file-name' variable to 'tomcat-demo.war'
      [var] Setting 'build-directory' variable to 'build'
war-version:
      [var] Setting 'build-directory' variable to 'build-version'
    [mkdir] Created dir: C:\projects\workspace\tomcat-demo\build-version
      [var] Setting 'war-file-name' variable to 'tomcat-demo-1.0.war'
      [war] Building war: C:\projects\workspace\tomcat-demo\build-version\tomcat-demo-1.0.war
BUILD SUCCESSFUL
Total time: 843 milliseconds

If we examine our project structure now, we can see that the 'tomcat-demo-1.0.war' file has been created in the 'build-version' directory. A build.number file has also been created to keep track of the build number.

'tomcat-demo-1.0.war' file has been created

I should mention here that in my build-main.xml file, I created a new 'kitchen-sink (version)' task that would let me automatically build and deploy my versioned warW file.

	<target name="kitchen-sink" depends="compile,war,pscp,undeploy,deploy-from-server,mail-build-and-deploy" />
	<target name="kitchen-sink (version)" depends="compile,war-version,pscp,undeploy,deploy-from-server,mail-build-and-deploy" />

We can execute 'kitchen-sink (version)' by double-clicking on it in the Eclipse Ant view.

Eclipse Ant View

The output of this execution is displayed in the console. As you can see, it builds and deploys the 'tomcat-demo-1.1.war' file.

Buildfile: C:\projects\workspace\tomcat-demo\build.xml
      [var] Setting 'war-file-name' variable to 'tomcat-demo.war'
      [var] Setting 'build-directory' variable to 'build'
clean:
   [delete] Deleting directory C:\projects\workspace\tomcat-demo\bin
    [mkdir] Created dir: C:\projects\workspace\tomcat-demo\bin
copy-non-java-files:
     [copy] Copying 1 file to C:\projects\workspace\tomcat-demo\bin
compile:
    [javac] Compiling 1 source file to C:\projects\workspace\tomcat-demo\bin
war-version:
      [var] Setting 'build-directory' variable to 'build-version'
      [var] Setting 'war-file-name' variable to 'tomcat-demo-1.1.war'
      [war] Building war: C:\projects\workspace\tomcat-demo\build-version\tomcat-demo-1.1.war
pscp:
     [echo] uploading war file to server using pscp
     [exec] Server version: SSH-2.0-OpenSSH_3.9p1
     [exec] We claim version: SSH-2.0-PuTTY_Release_0.58
     [exec] Using SSH protocol version 2
     [exec] Doing Diffie-Hellman group exchange
     [exec] Doing Diffie-Hellman key exchange
     [exec] Host key fingerprint is:
     [exec] ssh-rsa 1024 --:--:--:--:--:--:--:--:--:--:--:--:--:--:--:--
     [exec] Initialised AES-256 client->server encryption
     [exec] Initialised HMAC-SHA1 client->server MAC algorithm
     [exec] Initialised AES-256 server->client encryption
     [exec] Initialised HMAC-SHA1 server->client MAC algorithm
     [exec] Using username "---".
     [exec] Sent password
     [exec] Access granted
     [exec] Opened channel for session
     [exec] Started a shell/command
     [exec] Using SFTP
     [exec] Connected to ---.---.---.---
     [exec] Sending file tomcat-demo-1.1.war, size=230510
     [exec] Sent EOF message
     [exec] Server sent command exit status 0
     [exec] All channels closed. Disconnecting
     [exec] Server closed network connection
     [exec] tomcat-demo-1.1.war (excess output removed here...)
     [echo] done uploading war file to server using pscp
undeploy:
 [undeploy] OK - Undeployed application at context path /tomcat-demo
deploy-from-server:
     [echo] deploying server war file
   [deploy] OK - Deployed application at context path /tomcat-demo
     [echo] done deploying
mail-build-and-deploy:
     [mail] Sending email: tomcat-demo-1.1.war was uploaded to the server and deployed
     [mail] Sent email with 0 attachments
kitchen-sink (version):
BUILD SUCCESSFUL
Total time: 12 seconds

In our Navigator view, we can see the 'tomcat-demo-1.1.war' file has been created in the 'build-version' directory.

'tomcat-demo-1.1.war' file has been created

That wasn't too bad, was it? In a few quick steps we were able to build a versioned war file and deploy it by modifying our ant-utilities build-main.xml file. As you have seen (if you read the other Ant tutorials), it's taken several hours to write all the targets in build-main.xml, but once we've written them, it's very easy to expand upon them and chain them together.

(Continued on page 3)

Page: < 1 2 3 >