Saturday, December 26, 2015

Using Ant to Deploy Salesforce Code

Use following steps to deploy Salesforce code using Ant

1. Install Java on your local machine
Go to http://www.oracle.com/technetwork/java/javase/downloads/index.html. And get the latest version of the Java JDK and install it on your machine.

 2. Install ANT in your local machine.
Go to http://ant.apache.org/bindownload.cgi and get these files to your local. Once these files are on your computer, there is no further installation required.

3. After installation of java and ANT, create following environment variable to set the path.
ANT_HOME -- give Ant location on your machine. For example in my machine I have stored ant in “C:\apache-ant-1.9.6” location
Add following at the end of path variable:
C:\Program Files\Java\jdk1.6.0\bin;%ANT_HOME%\bin

 4. Download Salesforce.jar file and add that file to your ant installation’s lib directory.
To download this Go to setup->Develop-> click on Tools -> and click on Force.com migration tool to download
After downloading extract the downloaded ZIP file and copy the ant-salesforce.jar file and paste it in Ant lib directory in your ant installation file.
5. Now Force.com migration tool installation is completed.

Use Migration tool to deploy components in Salesforce
To deploy metadata by using this migration tool we need prepare following 2 important files

Build.xml

<project name="Sample usage of Salesforce Ant tasks" default="test" basedir="." xmlns:sf="antlib:com.salesforce">
<property file="buildbatch.properties"/>
<property environment="env"/>
<target name="Deploymetadata">
<sf:deploy username="TDeployUsername" password="ToDeployPasswordalonwithSecuirtyToken" serverurl="https://test.salesforce.com" deployroot="CodeFolder"
 runAllTests="false" trace="true" pollWaitMillis="10000" maxPoll="100"/>
</target>

<target name="retrieveCode">
<sf:deploy username="SourceUsername" password="SourcePasswordalonwithSecuirtyToken" serverurl="https://test.salesforce.com" retrieveTarget="CodeFolder"/>
</target>

<target name="DeployCheckmetadata">
<sf:deploy username="TDeployUsername" password="ToDeployPasswordalonwithSecuirtyToken" serverurl="https://test.salesforce.com" deployroot="CodeFolder"
 checkOnly="true"/>
</target>
</project>

Package.xml: This file is a project manifest that lists all the components you want to retrieve or deploy in a single request.

<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
    <types>
        <members>*</members>
        <name>ApexClass</name>
    </types>
    <types>
        <members>*</members>
        <name>ApexComponent</name>
    </types>
    <types>
        <members>*</members>
        <name>ApexPage</name>
    </types>
    <types>
        <members>*</members>
        <name>ApexTrigger</name>
    </types>
    <types>
        <members>*</members>
        <name>StaticResource</name>
    </types>
    <version>30.0</version>
</Package>

How to run this script in command prompt
1. Open command prompt and enter file path of your build.xml file.
For example if my filed file in  “C:\Build”. Enter this command in command prompt “cd C:\Build”.
2. Now retrieve the code your local from source organization. To retrieve the components mentioned in your package.xml run the command “ant retrieveCode” which is mentioned in your build.xml
3. Once you have done with retrieving components to your local, now you can deploy those components to your target organization. To deploy your components run the command “ant deployCode”, which is mentioned in your build.xml file.


No comments:

Post a Comment