ANT Tool Migration - Deployment Salesforce
Though there are lot of tools to develop in Salesforce available, but when it comes to deployment there are few options out there. Below are the ways deployment can be carried out
As seen in the above screenshot, following components are expected to be deployed:
ant deployCode - This will deploy the code with the unit test specified.
<target name="deployCode">
<!-- Upload the contents of the "codepkg" directory, running the tests for just 1 class -->
<sf:deploy username="${sf.username}" password="${sf.password}" sessionId="${sf.sessionId}" serverurl="${sf.serverurl}" maxPoll="${sf.maxPoll}" deployRoot="codepkg" testLevel="RunSpecifiedTests" rollbackOnError="true">
<runTest>SampleDeployClass</runTest>
</sf:deploy>
</target>
For deleting components in the org:
Delete components from Salesforce Organization using “destructiveChanges.xml” :
In some cases, we may want to delete some components like Object or fields from Salesforce Organization. In this case, Only “package.xml” will not work. We need to create “destructiveChanges.xml” file also. Syntax for this file is exactly same as of “package.xml”, except that here we cannot define wildcards. So, to undeploy anything from Salesforce org, we need two xml files – “package.xml” and “destructiveChanges.xml“.
ant undeployCode - This will delete the components specified in the destructiveChanges.xml
<!-- Shows removing code; only succeeds if done after deployCode -->
<target name="undeployCode">
<sf:deploy username="${sf.username}" password="${sf.password}" sessionId="${sf.sessionId}" serverurl="${sf.serverurl}" maxPoll="${sf.maxPoll}" deployRoot="removecodepkg"/>
</target>
Here is sample package.xml with types and names that needs to be used for deployment.
- Changesets from Salesforce Site - Almost easiest way to do Salesforce deployment, but certain components cannot be deployed through changeset since it is not handy in sometimes when we need to deploy certain components alone.
- Eclipse IDE - Most prefered deployment method preferred so far, but I find it really hard because of the slowness, takes time for the deployment.
- ANT Tool - A JAVA based tool, which is going to take sometime to setup, but its easiest way to perform the deployment.
Prerequisite:
JDK 1.5 or above
You can download from this link based on the operating system http://www.oracle.com/technetwork/java/javase/downloads/jdk10-downloads-4416644.html
ANT Distribution
You can download from this link http://ant.apache.org/bindownload.cgi
Setup the ANT environment variable performing the following steps:
- Unzip the zip file to a convenient location c:\folder. using Winzip, winRAR, 7-zip or similar tools.
- Create a new environment variable called ANT_HOME that points to the Ant installation folder, in this case c:\apache-ant-1.8.2-bin folder.
- Append the path to the Apache Ant batch file to the PATH environment variable. In our case this would be the c:\apache-ant-1.8.2-bin\bin folder.
Login to Salesforce -> Navigate to Search -> Develop -> Tools -> Lightning Platform tools & toolkits. or go to this link directly to download the Salesforce ANT migration jar.
Let us consider two environment from one sandbox to another you are going to deploy the code.
- Create two directory Source Org and Target org.
- Unzip the rar file and place in the directory.
For start the ANT deployment, we need the build.xml file. The build.xml file contains the properties to make connection to the org, type of deployment which you want to perform in commands which we are going to see later.
Build.properties file has the credentials of the organization that needs to be connected through ANT tool.
Here we are going to retrieve Code from the source Org.
- Open the command prompt.
- Use the change directory command and navigate to the target folder which has the build.xml.
- Enter the command - ant and if the build is successful you will be able to connect to the org.
- Now for retrieve the code use the command - ant retrieveCode
In the build.xml, you can find the
target name = "retreiveCode" which will get executed to retrieve your code from the org.
retrieveTarget = Directory in which the code has to be retrieved.
<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>
Now we are going to deploy the code retrieved from source org to target org.
- Copy and paste the components that needs to be deployed to target org.
- Make sure you give the credentials in the build.properties of the target org.
- Open command prompt.
- Use the change directory command and navigate to the target folder which has the build.xml.
- In the deployRoot folder, edit the package.xml and add the targets that needs to be deployed.
- deployRoot - is the directory in which components that needs to be deploy is placed and mentioned in the build.xml.
As seen in the above screenshot, following components are expected to be deployed:
- SampleDeployClass
- SampleAccountTrigger
- SampleFailingTestClass
ant deployCode - This will deploy the code with the unit test specified.
<target name="deployCode">
<!-- Upload the contents of the "codepkg" directory, running the tests for just 1 class -->
<sf:deploy username="${sf.username}" password="${sf.password}" sessionId="${sf.sessionId}" serverurl="${sf.serverurl}" maxPoll="${sf.maxPoll}" deployRoot="codepkg" testLevel="RunSpecifiedTests" rollbackOnError="true">
<runTest>SampleDeployClass</runTest>
</sf:deploy>
</target>
If the test is successful, this will deploy the code to the target org.
ant deployCodeNoTestLevelSpecified - this won't run any unit test and simply deploy the code to the org.
<!-- Shows deploying code with no TestLevel sepcified -->
<target name="deployCodeNoTestLevelSpecified">
<sf:deploy username="${sf.username}" password="${sf.password}" sessionId="${sf.sessionId}" serverurl="${sf.serverurl}" maxPoll="${sf.maxPoll}" deployRoot="codepkg" rollbackOnError="true"/>
</target>
ant deployCodeRunLocalTests - this will run only the local test and deploy the code to the org.
<!-- Shows deploying code and running tests only within the org namespace -->
<target name="deployCodeRunLocalTests">
<sf:deploy username="${sf.username}" password="${sf.password}" sessionId="${sf.sessionId}" serverurl="${sf.serverurl}" maxPoll="${sf.maxPoll}" deployRoot="codepkg" rollbackOnError="true" testlevel="RunLocalTests"/>
</target>
ant deployCodeNoTestLevelSpecified - this won't run any unit test and simply deploy the code to the org.
<!-- Shows deploying code with no TestLevel sepcified -->
<target name="deployCodeNoTestLevelSpecified">
<sf:deploy username="${sf.username}" password="${sf.password}" sessionId="${sf.sessionId}" serverurl="${sf.serverurl}" maxPoll="${sf.maxPoll}" deployRoot="codepkg" rollbackOnError="true"/>
</target>
ant deployCodeRunLocalTests - this will run only the local test and deploy the code to the org.
<!-- Shows deploying code and running tests only within the org namespace -->
<target name="deployCodeRunLocalTests">
<sf:deploy username="${sf.username}" password="${sf.password}" sessionId="${sf.sessionId}" serverurl="${sf.serverurl}" maxPoll="${sf.maxPoll}" deployRoot="codepkg" rollbackOnError="true" testlevel="RunLocalTests"/>
</target>
For deleting components in the org:
Delete components from Salesforce Organization using “destructiveChanges.xml” :
In some cases, we may want to delete some components like Object or fields from Salesforce Organization. In this case, Only “package.xml” will not work. We need to create “destructiveChanges.xml” file also. Syntax for this file is exactly same as of “package.xml”, except that here we cannot define wildcards. So, to undeploy anything from Salesforce org, we need two xml files – “package.xml” and “destructiveChanges.xml“.
ant undeployCode - This will delete the components specified in the destructiveChanges.xml
<!-- Shows removing code; only succeeds if done after deployCode -->
<target name="undeployCode">
<sf:deploy username="${sf.username}" password="${sf.password}" sessionId="${sf.sessionId}" serverurl="${sf.serverurl}" maxPoll="${sf.maxPoll}" deployRoot="removecodepkg"/>
</target>
Here is sample package.xml with types and names that needs to be used for deployment.
Nice Post.
ReplyDeleteCan you also provide steps to configure in mac.(Linux commands etc)
Thanks
veera