Salesforce Migration Tool – ANT

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.
1# build.properties
2# Specify the login credentials for the desired Salesforce organization
3sf.username = <SFDCUserName>
4sf.password = <SFDCPasswrd><SecurityToken>
5#sf.pkgName = <Insert comma separated package names to be retrieved>
6#sf.zipFile = <Insert path of the zipfile to be retrieved>
7#sf.metadataType = <Insert metadata type name for which listMetadata or bulkRetrieve operations are to be performed>
8# Use 'https://login.salesforce.com' for production or developer edition (the default if not specified).
9# Use 'https://test.salesforce.com for sandbox.
10sf.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:
1<project name="Shivasoft ANT Tutorial" default="deployCode" basedir="."xmlns:sf="antlib:com.salesforce">
2    <property file="build.properties"/>
3    <property environment="env"/>
4    <!-- Shows deploying code & running tests for code in directory -->
5    <target name="deployCode"  depends="proxy">
6      <sf:deploy username="${sf.username}" password="${sf.password}"
        serverurl="${sf.serverurl}" deployRoot="test1">
7        <runTest>TestClassName</runTest>
8      </sf:deploy>
9    </target>
10    <!-- Shows removing code; only succeeds if done after deployCode -->
11    <target name="undeployCode">
12      <sf:deploy username="${sf.username}" password="${sf.password}"
      serverurl="${sf.serverurl}" deployRoot="removecodepkg"/>
13    </target>
14    <target name="proxy">
15        <property name="proxy.host" value=" ProxyURL " />
16        <property name="proxy.port" value="1234" />
17        <property name="proxy.user" value="UserName" />
18        <property name="proxy.pwd" value="Password" />
19        <setproxy proxyhost="${proxy.host}" proxyport="${proxy.port}"
         proxyuser="${proxy.user}" proxypassword="${proxy.pwd}" />
20    </target>
21</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.
1<target name="proxy">
2        <property name="proxy.host" value=" ProxyURL " />
3        <property name="proxy.port" value="1234" />
4        <property name="proxy.user" value="UserName" />
5        <property name="proxy.pwd" value="Password" />
6        <setproxy proxyhost="${proxy.host}" proxyport="${proxy.port}"
proxyuser="${proxy.user}" proxypassword="${proxy.pwd}" />
7    </target>
Retrieve content from Salesforce Organization:
create “package.xml” for the list of component to be retrieved and add following task in “build.xml”.
1<!-- Retrieve an unpackaged set of metadata from your org -->
2<!-- The file unpackaged/package.xml lists what is to be retrieved -->
3<target name="test" depends="proxy">
4  <mkdir dir="retrieveUnpackaged"/>
5  <!-- Retrieve the contents into another directory -->
6  <sf:retrieve username="${sf.username}" password="${sf.password}"
   serverurl="${sf.serverurl}" retrieveTarget="retrieveUnpackaged"
   unpackaged="unpackaged/package.xml" unzip="false" />
7</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.

Ref:

Comments

Popular posts from this blog

Approval Process

Spring Batch 2.0 -Basic Concepts

Sending Mass Emails From Salesforce