Salesforce Deployment Using ANT Migration Tool – Part 2

Below is an example. We will first retrieve the components from source org and then will deploy those components to the target org. Here is the folder that we have initially. We have build.xml and build.properties file here and one folder named “unpackaged” in which we have our package.xml.

This package.xml file contains the list of components to be retrieved from source org.

Build.properties : We provide the information about our instance on which we want to perform the operation (deploy or retrieve or any other possible operation). Below is the sample. Here in this file we have only enabled the username and password for source org as we will first retrieve the components from source org and build.xml file will use these credentials. But while deploying those components to target org , we’ll have to changes these credentials info to target org’s credentials, so that in the next target that we run from command prompt to deploy the components, the build.xml file will use the target org’s credentials for deployment of components.This is the file that is referred in the next file which is build.xml. All the lines in the below code that starts with # are considered as comments.

Build.xml : Here we provide the actions or we can say the operation that we want to perform ( either retrieve or deploy or any other possible operation). These tags are called targets also. So, user can run these targets one by one and complete the task in sequential manner as he/she wants. In the below example we have one retrieve target and one deploy target. Every target has some name which we’ll use when calling these targets from command prompt. This file refers build.properties file where we have mentioned all the info required in this file(like username, password, serverurl)

    <!– Setting default value for username, password and session id properties to empty string
         so unset values are treated as empty. Without this, ant expressions such as ${sf.username}
         will be treated literally.
    –>
    <condition property=”sf.username” value=””> <not> <isset property=”sf.username”/> </not> </condition>
    <condition property=”sf.password” value=””> <not> <isset property=”sf.password”/> </not> </condition>
    <condition property=”sf.sessionId” value=””> <not> <isset property=”sf.sessionId”/> </not> </condition>

    <taskdef resource=”com/salesforce/antlib.xml” uri=”antlib:com.salesforce”>
        <classpath>
            <pathelement location=”../ant-salesforce.jar” />           
        </classpath>
    </taskdef>

    <!– Shows deploying code & running tests for code in directory –>
    <target name=”deployCode”>
      <!– Upload the contents of the “codepkg” directory, running the tests for just 1 class –>
      <sf:deploy username=”${sf.myDevusername}” password=”${sf.myDevpassword}” sessionId=”${sf.sessionId}” serverurl=”${sf.serverurl}” maxPoll=”${sf.maxPoll}” deployRoot=”codepkg”>
       </sf:deploy>
    </target>

Package.xml : This is the last xml file that we need. We have to mention the API name of components that we want to retrieve from source org. And the same xml can be used while deploying those components to target org.

<?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>ApexTrigger</name>
    </types>
    <version>41.0</version>
</Package>

This is continued in next post

Permanent link to this article: https://salesforcebuddy.com/2019/04/salesforce-deployment-using-ant-migration-tool-part-2/

Leave a Reply

Your email address will not be published.