Wednesday, 15 November 2017

Salesforce Deployment Using ANT Migration Tool | ANT Deployment Tool

Please follow below step to configure Migration Tool ANT

Step 1) Install the Java

Please use 1.7x or later version for better security and for TLS security.

Migration using ANT in Salesforce


Step 2) Download the ANT


        1) Download ANT from below URL :- https://ant.apache.org/bindownload.cgi


        2) Download the zip file of Force.com Migration Tool from https://developer.salesforce.com/docs/atlas.en-us.daas.meta/daas/forcemigrationtool_install.htm

       or you can click on STEP -> Tool -> Force.com Tools and Toolkits

salesforce ant migration jar

     Then Click on Force.com Migration Tool: and download the JAR file.

     Now We have below two zip files.

salesforce ant migration jar

     Unzip both the files.

       3) Now Open Force.com Migration Folder (salesforce_ant_41.0 in my case). and copy "ant-salesforce.zip" File and paste in apache-ant-1.10.1\lib folder "D:\ANTTool\apache-ant-1.10.1-bin\apache-ant-1.10.1\lib" .


 

Step 3) Set the path.


Now we need to set ANT_HOME and JAVA_HOME variable which we can set from Environment variable.

ANT_HOME   :- Set the address where you installed the ANT.
JAVA_HOME  :- set the value where your JDK is located.
Path  :- Set And and Jave path till bin folder


Now All Set. Check the version



Step 4) Configure "Build.xml" and "build.properties" Files


To setup connection with salesforce we need to configure the "build.properties".
  • Go to folder where you unzip the Force.com  Migration Tool Files. (in my case salesforce_ant_41.0 )
  • Open the "build.properties" and update like below 
  1. # build.properties
    #

    # Specify the login credentials for the desired Salesforce organization
    sf.username = amit.salesforce21@gmail.com.demoapp
    sf.password = Password

    sf.myDevusername = amit.salesforce21@gmail.com.kt
    sf.myDevpassword = Password

    #sf.sessionId = <Insert your Salesforce session id here.  Use this or username/password above.  Cannot use both>
    #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://login.salesforce.com

    sf.maxPoll = 20
    # If your network requires an HTTP proxy, see http://ant.apache.org/manual/proxy.html for configuration.
    #




  • Now configure "build.xml" to retrievePkg and Deploy.

<project name="Sample usage of Salesforce Ant tasks" default="test" basedir="." xmlns:sf="antlib:com.salesforce">

    <property file="build.properties"/>
    <property environment="env"/>

    <!-- 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>
   
    <!-- Shows retrieving code; only succeeds if done after deployCode -->
    <target name="retrieveCode">
      <!-- Retrieve the contents listed in the file codepkg/package.xml into the codepkg directory -->
      <sf:retrieve username="${sf.username}" password="${sf.password}" sessionId="${sf.sessionId}" serverurl="${sf.serverurl}" maxPoll="${sf.maxPoll}" retrieveTarget="codepkg" unpackaged="codepkg/package.xml"/>
    </target>

</project>



  • Now Setup your Package.xml file
<?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>


Step 5) Now execute the command


 retrieve the Code from Source Salesforce org and execute below command

ant retrieveCode



Now Deploy the code to target Salesforce org

ant deployeCode



Reference :-
https://developer.salesforce.com/docs/atlas.en-us.daas.meta/daas/meta_development.htm

Thanks,
Amit Chaudhary

12 comments:

  1. This comment has been removed by a blog administrator.

    ReplyDelete
    Replies
    1. what did you remove? Unless if its offensive you should have it to encourage a healthy collaboration

      Delete
  2. Very helpful for beginners to get started. - Thanks

    ReplyDelete
  3. how can we take backups from multiple sfdc org?

    ReplyDelete
    Replies
    1. Make a copy of the sample folder. Change direction (userid, password, if org is test or prod) and optionally build.properties etc.
      Go to the newly created folder in your cli and run ant

      Delete
  4. Thanks. Great instruction - it just worked for me first time.

    ReplyDelete
  5. Thanks Amit for the good Article but how to upload a static resource(zip file) using ANT migration tool?

    ReplyDelete
  6. how can we solve this error: Failed to send request to https://test.salesforce.com/services/Soap/u/47.0
    Please advise.

    ReplyDelete
  7. BUILD FAILED


    /home/vsts/work/1/s/lightning/build.xml:88: Can't assign value '${sf.maxPoll}' to attribute maxpoll, reason: class java.lang.NumberFormatException with message 'For input string: "${sf.maxPoll}"'


    Im using Ant for building the code in my Azure devops pipeline. i am getting the above error. can someone plz help me resolve this.

    ReplyDelete