Friday 8 February 2013

How to use ANT for metadata backup & deployments?



We generally take metadata backups from Salesforce as a part of our regular deployments. And what I have noticed is that using ANT is faster that that of Eclipse.

Following are the manual things that we generally do while taking the backup using ANT.
1) Change the username and password to point to that particular org in the build.properties.
2) Change the server url to point to either sandbox or prod.
3) Change the name of the backup folder once the backup is taken – add a prefix or suffix of current date.
The following steps help you to prevent the above manual changes every time you use ANT to take a backup.
1) Open the build.properties and create your own variables (for storing the credentials) for different Orgs as shown below.



2) Create a date variable to store the current date in the same build.properties as shown below


#Date variable
sf.date = 01-22-2013


3) Now open the build.xml from the same folder as that of build.properties and create the targets as shown below.
Note that each target is using the org specific variables that we created in build.xml
Note that the dir attribute (in mkdir tag) and retrieveTarget attribute (in sf:retrieve tag) are using the date variable as suffix.

 


4)You are Done!
Now every time you take a back you don’t have to change the credentials to point to that org. All you have to do is change the date variable value to the current date.
Now open ANT in command prompt and type in ant as shown below.


Some points to consider into:


While installing ANT, I found three problems.

So, I thought to make you know this earlier to installing on your system.

If you get an error like these, do the prescribed solution. It would work! 
          D:\Software\apache-ant-1.8.4-bin\apache-ant-1.8.4\bin>ANT -version
          Unable to locate tools.jar. Expected to find it in C:\Program Files\Java\jre7\lib\tools.jar
          Apache Ant(TM) version 1.8.4 compiled on May 22 2012
    • Solution: Make sure you have tools.jar file in the above mentioned path.
   2.
          D:\Software\apache-ant-1.8.4-bin\apache-ant-1.8.4\bin>ANT Dev1Backup
          Buildfile: D:\Software\apache-ant-1.8.4-bin\apache-ant-1.8.4\bin\build.xml

          Dev1Backup:
              [mkdir] Created dir: D:\Software\apache-ant-1.8.4-bin\apache-ant-1.8.4\bin\Dev1Backup_01-22-2013

          BUILD FAILED
          D:\Software\apache-ant-1.8.4-bin\apache-ant-1.8.4\bin\build.xml:23: Problem: failed to create task or type antlib:com.salesforce:retrieve
          Cause: The name is undefined.
          Action: Check the spelling.
          Action: Check that any custom tasks/types have been declared.
          Action: Check that any / declarations have taken place.
          No types or tasks have been defined in this namespace yet

          This appears to be an antlib declaration.
          Action: Check that the implementing library exists in one of:
                  -D:\Software\apache-ant-1.8.4-bin\apache-ant-1.8.4\bin\..\lib
                  -C:\Documents and Settings\sudhir.kumarjaiswal\.ant\lib
                  -a directory added on the command line with the -lib argument

    • Solution: Make sure to add ant-salesforce.jar file inside lib folder (here for example put it inside D:\Software\apache-ant-1.8.4-bin\apache-ant-1.8.4\lib
    3.
          D:\Software\apache-ant-1.8.4-bin\apache-ant-1.8.4\bin>ANT Dev1Backup
          Buildfile: D:\Software\apache-ant-1.8.4-bin\apache-ant-1.8.4\bin\build.xml

          Dev1Backup:

          BUILD FAILED
          D:\Software\apache-ant-1.8.4-bin\apache-ant-1.8.4\bin\build.xml:23: Invalid username, password, security token; or user locked out. Are you at a new location?
          When accessing Salesforce--either via a desktop client or the API--from outside of your companyÆs trusted networks,
          you must add a security token to your password to log in.
          To receive a new security token, log in to salesforce.com at http://login.salesforce.com and click      Setup | My Personal Information | Reset Security Token.

    • Solution: Make sure to append Security token at the end of password in build.properties file.
      • P.S: You may need this security token when you are not connected to the VPN.

4 comments:

  1. Very useful.. thanks for sharing.

    ReplyDelete
  2. I am running in to issue listed above ( point 2) still facing

    BUILD FAILED
    /Users/sureshkotari/Desktop/Ant/Antdev/build.xml:14: Problem: failed to create task or type antlib:com.salesforce:retrieve
    Cause: The name is undefined.
    Action: Check the spelling.
    Action: Check that any custom tasks/types have been declared.
    Action: Check that any / declarations have taken place.
    No types or tasks have been defined in this namespace yet

    This appears to be an antlib declaration.
    Action: Check that the implementing library exists in one of:
    -/usr/local/Cellar/ant/1.9.4/libexec/lib
    -/Users/sureshkotari/.ant/lib
    -a directory added on the command line with the -lib argument

    i have copied ant-salesforce.jar file inside lib folder

    ReplyDelete
  3. BUILD FAILED
    /Users/sureshkotari/Desktop/Ant/Antdev/build.xml:14: Problem: failed to create task or type antlib:com.salesforce:retrieve
    Cause: The name is undefined.
    Action: Check the spelling.
    Action: Check that any custom tasks/types have been declared.
    Action: Check that any / declarations have taken place.
    No types or tasks have been defined in this namespace yet

    This appears to be an antlib declaration.
    Action: Check that the implementing library exists in one of:
    -/usr/local/Cellar/ant/1.9.4/libexec/lib
    -/Users/sureshkotari/.ant/lib
    -a directory added on the command line with the -lib argument

    Still i am facing same issue

    ReplyDelete
  4. How can we retrieve the newly created fields on the managed package objects in our metadata backup.

    We are unable to retrieve with below command.


    *
    CustomObject

    ReplyDelete

Thank you for visiting. Your comments are highly appreciated.