Salesforce Metadata Migration using ANT
Here, We will see how to migrate metadata using ANT tool, so that we can easily integrate it with CI/CD Pipeline without manual intervention.
What is ANT Migration Tool?
It is a command-line utility (Java/Ant-based) for moving metadata between a local directory and a Salesforce org.
Why we need it?
When you have to deal with multiple orgs and every time you have to set layouts, create custom objects or set values in standard field sets, you can automate the deployment using the solution. You can easily integrate this with CI/CD and run the job whenever you want.
Prerequisites
Java, Apache Ant, Salesforce ANT Jar
Download the compatible version, otherwise it will throw an error while retrieving or deploying metadata.
I have used following versions for sample example.
salesforce_ant_52.0
openjdk-11.0.2_windows-x64_bin → Set environment variable up to bin
apache-ant-1.10.11-bin → Set environment variable up to bin
Put the extracted ant-salesforce.jar from saleforce_ant_52.0 to the lib folder of apache-ant
Now all set to run the command.
Steps
Retrieve metadata
- Create folder named “unpackaged” and put your Package.xml inside it.
Package.xml
2. Create build.properties file as below.
3. Create build.xml as below.
4. Folder structure will be like.
5. Open the command prompt from this folder. To retrieve metadata , get the target name from build.xml which has sf:retrieve tag. In our example it is retrieveUnpackaged.
Basically this command will create retrieveUnpack directory. It will read the package.xml file from unpackaged folder and create the metadata in retrieveUnpack folder as specified in retrieveTarget parameter.
Command will be : Ant <name from the retrieve target>
Deploy metadata
Open the command prompt from same folder. To deploy metadata , get the target name from build.xml which has sf:deploy tag. In our example it is deployUnpackaged.
Basically this command will deploy the metadata from the folder mentioned in the deployRoot Parameter. In our case metadata will deploy from retrieveUnpack directory.
Command will be : Ant <name from the deploy target>
If the deployment is successful, it will display build successful.
You can integrate this command with CI/CD and use it anytime without manual intervention. At runtime you can replace the credentials of the org from the build.properties file. You can use Git repository to store the metadata for deployment. CI/CD can be configured with Git repository. You are all set to migrate data using automated way.