Ant with SCP
WOW! That was not easy to figure out. For the most part it was easy, but there was one bug that just hung me up for a while.
I laid out what we would need to do a remote deployment:
- Change web.xml so that it will work on the server.
- Build project.
- Change web.xml back so it is ready to work locally again.
- Copy war file to remote server.
There are two things to do to prepare for completing this.
- Get the ant library for the scp task downloaded and in place.
- Confirm environment variables are set.
So, to complete #1, download this file and save it to your ant installation’s lib directory. On my machine that was in C:\ant\lib, but yours may be different.
To complete #2, make sure your JAVA_HOME and ANT_HOME environment variables are set to the proper location. On my system JAVA_HOME = C:\Program Files\java\jdk1.5.0_11 and ANT_HOME = C:\ant.
To accomplish the first and third requirements, I added the following to the build.xml:
<property name="remoteLog4J" value="/"/>
<property name="localLog4J" value="/Users/nate/Documents/school/java/euphemia/src/config/log4j.properties"/>
<target name="prep-remote">
<replaceregexp file="${src}/web.xml" match="(<param-name>log4jProperties</param-name>[^<]*<param-value>)[^<]+(</param-value>)"
replace="\1${remoteLog4J}\2"
flags="g"
/>
<echo>log4jProperties parameter value changed to ${remoteLog4J}</echo>
</target>
<target name="prep-local">
<replaceregexp file="${src}/web.xml" match="(<param-name>log4jProperties</param-name>[^<]*<param-value>)[^<]+(</param-value>)"
replace="\1${localLog4J}\2"
flags="g"
/>
<echo>log4jProperties parameter value changed to ${localLog4J}</echo>
</target>
I allowed build to function normally, without change. Deployment is as follows:
<property name="remoteUser" value="nsutton"/>
<property name="remoteURI" value="cisjava2.matcmadison.edu"/>
<property name="remoteDeploymentDir" value="/home/nsutton/tomcat/webapps"/>
<property name="remoteDeploymentPath" value="${remoteUser}:${remote.password}@${remoteURI}:${remoteDeploymentDir}"/>
<target name="deploy-remote" depends="prep-remote,dist,prep-local">
<scp trust="true" file="${dist}/euphemia.war" todir="${remoteDeploymentPath}"/>
</target>
To use the remote deployment:
ant deploy-remote -Dremote.password=MyPasswordHere
To customize this code for your use you will need to change the following properties:
- remoteLog4J
- localLog4J
- remoteUser
- remoteDeploymentDir
You will also need to change the name of your war file in the file attribute of the scp task in the deploy-remote target. Mine is euphemia.war, yours should be different. For that matter, I believe this should be in a property, but for the sake of keeping this post simple, I have kept it like this.
Here is my build.xml file.
I am COMPLETELY open to criticism/additions/fixes for this setup. I make no claim on it being a perfect solution and gladly welcome any and all input.