Salesforce Ant Scripts

This brief post illustrates how Ant scripts can be used in a continuous integration scenario, i.e. where metadata is held in a source code control (SCC) repository such as Subversion. In a CI scenario developers would typically be working in isolated developer orgs with periodic commits of unit tested code to SCC following peer-review (hopefully). The act of committing changes to SCC will trigger a full deployment of the metadata state held in SCC to a dedicated integration org – with full execution of unit tests and hopefully automated acceptance tests (i.e. Selenium or similar). The whole point of this process is to introduce rigour into the development process around code commits and to ensure build errors are captured whilst the developer is in the moment and can remedy the problem quickly. CI is an agile practice related to technical excellence.

Ok, enough theory – the example (simplistic) script below shows a common case where the head revision is checked-out to a local folder and deployed to a Salesforce org – with unit tests running. In practice this script would be automated by a Hudson or Jenkins job that would be monitoring the SCC repository for commit operations.

Build Properties []

# Contains properties referenced by all deployment scripts
# May be replaced by configuration parameters when invoked from a Hudson/Jenkins Job.

# local root folder.

# Salesforce task configuration properties.

# SvnAnt task configuration properties.

Build File – Retrieve Metadata from SCC and Deploy to Org [build.xml]

<project name="Subversion to Org" default="deploy" basedir="." xmlns:sf="antlib:com.salesforce">	
  	<property file="" />
  	<property environment="env" />
	<!-- path to the svnant libraries. Usually they will be located in ANT_HOME/lib -->
	<path id="svnant.classpath">
		<fileset dir="${ant.home}\lib">
	  		<include name="**/svn*.jar"/>
	<!-- load the svn task -->
	<typedef resource="org/tigris/subversion/svnant/svnantlib.xml" classpathref="svnant.classpath" />
	<target name="checkoutLatest">
		<svn username="${svnant.repository.user}" password="${svnant.repository.passwd}">
	  		<checkout url="${svnant.latest.url}"
	  			destPath="${metadata.root}" />
	<target name="deploy" depends="checkoutLatest">
  		<echo message="deploying from ${metadata.root}" /> 
		<sf:deploy username="${}" 
			runAllTests="${}" />

The script above can be executed manually from a standard ( project within Eclipse. I typically run with an Eclipse workspace per-client and maintain a deployment project within the workspace for all the scripts I use.

Pre-requisites are the Migration Tool and svnAnt task being added to the Ant classpath. Install instructions linked below. Migration Tool Guide

SvnAnt Project Home

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: