How to deploy apex code using ANT ?

A developer want to push the components/metadata like Objects, Fields , Validation Rules, Workflows and Apex Code etc. We can use Change Set, IDE and ANT Migration tool. However there are some extra features provided in this ANT migration tool. In this blog, we are gonna demonstrate the most powerful tool ANT mostly used for our major deployments.

ANT migration tool (based on JAVA), is used to deploy the Metadata from one organization to other organization or we can use it to retrieve the metadata from one organization and then make some changes locally and then deploy that metadata again to the same organization.

Features of ANT Migration Tool

  • The main advantage of this tool is, that it gets the metadata in form of XML files from your server and downloads it locally on your computer. Thus you can make changes in those XML files locally and again deploy the changes to any server instance, any target org that you want.
  • It allows you to deploy the same metadata any number of times to any of your server, as you have downloaded the metadata in form of XML files, you can deploy them again and again.
    • Change set does not allow you to delete any metadata component from target org. But using ANT migration tool you can delete the components from target org. This is done using destructiveChanges.xml file.
  • Some components are not supported to be migrated using change sets but you can migrate them using ANT migration tool.
  • It can also be run from command prompt using some specific commands for calling APIs.
  • You can also automate your migration process leveraging the capabilities of command prompt .bat files and XML structure of source files.

Here are the few steps that we need to take in order to push our changes:-

Step 1 (Install ANT Migration Tool)

Please click here to install prerequisites and ANT Migration Tool. Save the .zip file locally, and extract the contents to the directory of your choice. If you are using Windows, please use a folder in C:\ for example ‘C:\Tools\apache-ant-1.10.3’ to avoid windows limitation for directory length.

In order to start deployment, we need to download the .zip file which contains set of configuration files. This helps developers to configure username and password, getting metadata and build scripts to run on command prompt. 

( The above download link doesn’t require authentication to Salesforce. If you’re logged in to Salesforce, we recommend you log out before accessing the link in your browser. )

Please extract the to ‘C:\Tools\salesforce_ant_41.0’. Will discuss about the each file later in this blog. Here is the screenshot of sample files.


Step 2 (Setup Environment Variables)

You need to set the environment path variables which will help you to access ant tools from any folder through the command line.

Go to Control Panel > System > Edit the System Environment Variables


  • Create a ANT_HOME and JAVA_HOME variables under ‘System Variables’
  • Create an ANT_HOME environment variable and set the value to where you have installed Ant. e.g. C:\Tools\apache-ant-1.10.3
  • Create JAVA_HOME environment variable and set the value to the location of your JDK. e.g. C:\Program Files\Java\jdk1.8.0_171
  • Add C:\Tools\apache-ant-1.10.3\bin to the list of existing PATH variablepath

Step 3 (Test Installation)

Once you done with the above steps now it’s time to check if everything is all set to go.

  • Open command prompt or terminal and type ant or ant –version and press enter


To get started  first, let’s understand the important files available inside the salesforce -ant folder (C:\Tools\salesforce_ant_41.0\sample):

We provide the login credentials in Open and enter your Dev Org credentials. Don’t forget to append the security token to the password.



Here we provide the scripts to retrive or deploy or any other possible operation. We use tags like “retrieveCode” or “deployCode” in build.xml. These tags are also called targets. We can run these targets one by one and complete the task in sequential manner as we want. is also referred in build.xml for required credential and login urls.

  • Search for “retrieveCode” in build.xml. You will see unpackaged=”codepkg/package.xml”


  • Open package.xml from ‘codepkg/package.xml’, and modify package.xml. You will enter the class and trigger you like to retrieve from Salesforce in 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.

Step 4 (Retrieve and Deploy Code)

You may navigate to Salesforce > Deployment Status to verify deployment
Deployment without running Test Class
In this case, we are not running the tests which can be configured in build.xml by setting testLevel=”NoTestRun”
Deployment with running Specified Tests
We can run specific test class by setting testLevel=”RunSpecifiedTests” with adding below runTest child tag in the above script.



For more details about build.xml scripts, please click on following link

How to delete a class or trigger, please click on the following link

To rename a component, you must delete the component, and then recreate it with a new name. It cannot be done directly because we don’t maintain the ID’s of components in the case of ANT. Please click on the following link for more information


Senior Salesforce Architect
In Connect me on LinkedIn
twitter Follow me on Twitter @apexcoder
blogger Subscribe to my blog
email Email me


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s