Search

Friday, 26 October 2012

Using Ant To Deploy


In addition to the Force.com IDE, you can also use a script to deploy Apex.
Download the Force.com Migration Tool if you want to use a script for deploying Apex from a Developer Edition or sandbox organization to a Database.com production organization using Apache's Ant build tool.
http://www.salesforce.com/us/developer/docs/apexcode/Content/images/help/helpNote_icon.gifThe Force.com Migration Tool is a free resource provided by salesforce.com to support its users and partners but isn't considered part of our services for purposes of the salesforce.com Master Subscription Agreement.
To use the Force.com Migration Tool, do the following:
1.     Visit http://java.sun.com/javase/downloads/index.jsp and install Java JDK, Version 6.1 or greater on the deployment machine.
2.     Visit http://ant.apache.org/ and install Apache Ant, Version 1.6 or greater on the deployment machine. i found it here http://ant.apache.org/bindownload.cgi

3.     Set up the environment variables (such as ANT_HOMEJAVA_HOME, and PATH) as specified in the Ant Installation Guide at http://ant.apache.org/manual/install.html. Dont forget setup the same variables in User and System environment variables. In windows right click on Computer > Properties > Advanced Settings > environment variables

4.     Verify that the JDK and Ant are installed correctly by opening a command prompt, and entering ant –version. Your output should look something like this:
Apache Ant version 1.7.0 compiled on December 13 2006
5.     Log in to Salesforce on your deployment machine. Click Your Name | Setup | Develop | Tools, then click Force.com Migration Tool.
6.     Unzip the downloaded file to the directory of your choice. The Zip file contains the following:
o    Readme.html file that explains how to use the tools
o    A Jar file containing the ant task: ant-salesforce.jar
§  A sample folder containing:
§  codepkg\classes folder that contains SampleDeployClass.cls and SampleFailingTestClass.cls
§  codepkg\triggers folder that contains SampleAccountTrigger.trigger
o    mypkg\objects folder that contains the custom objects used in the examples
o    removecodepkg folder that contains XML files for removing the examples from your organization
o    A sample build.properties file that you must edit, specifying your credentials, in order to run the sample ant tasks in build.xml
o    A sample build.xml file, that exercises the deploy and retrieveAPI calls
7.     Copy the ant-salesforce.jar file from the unzipped file into the ant lib directory. The ant lib directory is located in the root folder of your Ant installation.
8.     Open the sample subdirectory in the unzipped file.
9.     Edit the build.properties file:
§  Enter your Salesforce production organization username and password for the sf.username and sf.password fields, respectively.Note
o    http://www.salesforce.com/us/developer/docs/apexcode/Content/images/help/helpNote_icon.gifThe username you specify should have the authority to edit Apex.
o    If you are using the Force.com Migration Tool from an untrusted network, append a security token to the password. To learn more about security tokens, see “Resetting Your Security Token” in the Salesforce online help.
§  If you are deploying to a sandbox organization, change the sf.serverurl field to https://test.salesforce.com.
10.   Open a command window in the sample directory.
11.   Enter ant deployCode. This runs the deployAPI call, using the sample class and Account trigger provided with the Force.com Migration Tool.
The ant deployCode calls the Ant target named deploy in the build.xml file.
<!-- Shows deploying code & running tests for package 'codepkg' -->
    <target name="deployCode">
      <!-- Upload the contents of the "codepkg" package, running the tests for just 1 class -->
      <sf:deploy username="${sf.username}" password="${sf.password}" serverurl="${sf.serverurl}" deployroot="codepkg">
        <runTest>SampleDeployClass</runTest>
      </sf:deploy>
    </target>
For more information on deploy, see Understanding deploy.
12.   To remove the test class and trigger added as part of the execution of ant deployCode, enter the following in the command window: ant undeployCode.
ant undeployCode calls the Ant target named undeployCode in the build.xml file.
<target name="undeployCode">
      <sf:deploy username="${sf.username}" password="${sf.password}" serverurl=
            "${sf.serverurl}" deployroot="removecodepkg"/>
</target>

4 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Hi, want to add that we can deploy not only APEX but anything we want to production or other org. Especially it's cool when we need to deploy objects with a lot of fields.

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
    2. If you change your package.xml and add other items such as

      Google doesnt like entering xml in these comments so Ive removed the angle brackets below

      types
      members * members
      name ApexPage name
      types
      types
      members * members
      name ApexTrigger name
      types
      types
      members * members
      name CustomObject name
      types


      So you can triggers and objects, of course you can add most things

      Delete

Note: only a member of this blog may post a comment.