Salesforce Migration Tool – ANT

Get articles everyday as a email directly to your inbox:
Click to publicize, if you like this article :

Following are the three ways to Migrate code, objects, schema etc… from organization to another :

  1. Change sets (From Salesforce site)
  2. Eclipse (Using “Deploy to force.com server” option in Eclipse)
  3. ANT (Java based tool)

We are going to discuss the ANT based migration, step by step:

Prerequisite:
JDK 1.5 or above

Step 1:
Download ANT distribution from – “http://ant.apache.org/bindownload.cgi

Step 2:
Set Environment variable “ANT_HOME”. The path should be of parent folder of “bin”. Also add the “bin” folder to your path.

Step 3:
Check whether ANT is installed or not properly by running command “ant -version”. It might be possible that you receive message something like “unable to find tools.jar”. You can copy this jar from “JDK_HOME/lib/tools.jar” to “JRE/lib” folder.

Salesforce Get ANT version

Salesforce Get ANT version

In above screen you can see that before copying “tools.jar” I was getting warning.

Step 4:
Login to salesforce and navigate to “Your Name |Setup | Develop | Tools” and download “Force.com Migration tool”.

Unzip the downloaded file to the directory of your choice. Copy the “ant-salesforce.jar” file from the unzipped file into the ant lib directory.

To start with deployment using ANT, we will need “build.xml” and “build.properties” file. As there is no need of “build.properties” however its good to have it so that the configuration related settings are in different file. You can copy both files from “sample” folder of unzipped content from salesforce. Following is the structure of “build.properties” file.

# build.properties
# Specify the login credentials for the desired Salesforce organization
sf.username = <SFDCUserName>
sf.password = <SFDCPasswrd>
#sf.pkgName = <Insert comma separated package names to be retrieved>
#sf.zipFile = <Insert path of the zipfile to be retrieved>
#sf.metadataType = <Insert metadata type name for which listMetadata or bulkRetrieve operations are to be performed>
# Use 'https://login.salesforce.com' for production or developer edition (the default if not specified).
# Use 'https://test.salesforce.com for sandbox.
sf.serverurl = https://test.salesforce.com

Step 5:
Copy all folders with source code from source organization using eclipse. Lets say the root folder name is “test1”.
Create “build.properties” from above code snippet or copy it from unzipped folder. Now create “build.xml” needed by ANT. Following is the example of build.xml file:

<project name="Shivasoft ANT Tutorial" default="deployCode" basedir="." xmlns:sf="antlib:com.salesforce">
    <property file="build.properties"/>
    <property environment="env"/>
    <!-- Shows deploying code & running tests for code in directory -->
    <target name="deployCode"  depends="proxy">
      <sf:deploy username="${sf.username}" password="${sf.password}" serverurl="${sf.serverurl}" deployRoot="test1">
        <runTest>TestClassName</runTest>
      </sf:deploy>
    </target>
    <!-- Shows removing code; only succeeds if done after deployCode -->
    <target name="undeployCode">
      <sf:deploy username="${sf.username}" password="${sf.password}" serverurl="${sf.serverurl}" deployRoot="removecodepkg"/>
    </target>
	<target name="proxy">
		<property name="proxy.host" value=" ProxyURL " />
		<property name="proxy.port" value="1234" />
		<property name="proxy.user" value="UserName" />
		<property name="proxy.pwd" value="Password" />
		<setproxy proxyhost="${proxy.host}" proxyport="${proxy.port}" proxyuser="${proxy.user}" proxypassword="${proxy.pwd}" />
	</target>
</project>

Attribute “deployRoot” means the root folder from which the code should be copied and tag <runTest> means the testclasses which should run after the deployment. At last, go to the folder “test1” from command line and run command “ant deployCode”.

Salesforce Migration using ANT

Salesforce Migration using ANT

Checking the status of the task:
To know the status of task you can run command:
Ant targetName -Dsf.asyncRequestId=requestID

Using Proxy in ANT migration tool:
If your organization uses the proxy then add below target in “build.xml” and specify this target as dependent.

<target name="proxy">
		<property name="proxy.host" value=" ProxyURL " />
		<property name="proxy.port" value="1234" />
		<property name="proxy.user" value="UserName" />
		<property name="proxy.pwd" value="Password" />
		<setproxy proxyhost="${proxy.host}" proxyport="${proxy.port}" proxyuser="${proxy.user}" proxypassword="${proxy.pwd}" />
	</target>

Retrieve content from Salesforce Organization:
create “package.xml” for the list of component to be retrieved and add following task in “build.xml”.

<!-- Retrieve an unpackaged set of metadata from your org -->
<!-- The file unpackaged/package.xml lists what is to be retrieved -->
<target name="test" depends="proxy">
  <mkdir dir="retrieveUnpackaged"/>
  <!-- Retrieve the contents into another directory -->
  <sf:retrieve username="${sf.username}" password="${sf.password}" serverurl="${sf.serverurl}" retrieveTarget="retrieveUnpackaged" unpackaged="unpackaged/package.xml" unzip="false" />
</target>

“Unzip” attribute specifies that the code retrieved should be in Zip format or not. By default the value is true means it will not in zip format.

I hope this article will help newbie to learn ant migration tool.

You can leave a response, or trackback from your own site.
  • Dedo

    Nice tutorial :-) can I run ANT to retrieve and deploy from/to salesforce from Eclipse?

    • JitendraZaa

      Eclipse already have inbuilt Migration tool.

  • http://www.facebook.com/aries.mishra Nitish Mishra

    How can I deploy a folder using ant tool:

    when I used the below codes to deploy the folder :

    EMEA_Pricing_Approval_Templates
    Folder

    It gives the error:
    Retrive Commands ::

    F:AntDemosample>ant retrieveUnpackaged
    Buildfile: F:AntDemosamplebuild.xml
    retrieveUnpackaged:
    [sf:retrieve] Request for a retrieve submitted successfully.
    [sf:retrieve] Request Id for the current retrieve task: 04s900000025E2EAAU
    [sf:retrieve] Waiting for server to finish processing the request…
    [sf:retrieve] Request Status: Completed
    [sf:retrieve] Retrieve warnings (1):
    [sf:retrieve] package.xml – Entity type: ‘Folder’ is unknown
    [sf:retrieve] Finished request 04s900000025E2EAAU successfully.

    Deploy command::

    F:AntDemosample>ant deployUnpackaged
    Buildfile: F:AntDemosamplebuild.xml
    deployUnpackaged:
    [sf:deploy] Request for a deploy submitted successfully.
    [sf:deploy] Request Id for the current deploy task: 04s90000002BZDxAAO
    [sf:deploy] Waiting for server to finish processing the request…
    [sf:deploy] Request Status: Completed
    BUILD FAILED
    F:AntDemosamplebuild.xml:37: FAILURES:
    Error: package.xml():Unknown type name ‘Folder’ specified in package.xml
    Total time: 16 seconds

    Any help will be appreciated..:)

  • sra

    Very good thanks shiva

  • Vinay

    Thanks a lot, very informative.

  • Sunu

    Thank you very much for the post