Skip to content

Instantly share code, notes, and snippets.

@vnehess
Last active August 29, 2015 14:21
Show Gist options
  • Select an option

  • Save vnehess/20566e51a0c89d5a8a86 to your computer and use it in GitHub Desktop.

Select an option

Save vnehess/20566e51a0c89d5a8a86 to your computer and use it in GitHub Desktop.
<target name="deploy" description="Deploys all package components to dev org">
<!-- stop scheduled job -->
<executeAnonymous username="${de.username}" password="${de.password}"><![CDATA[
for (SObject j : [SELECT Id FROM CronTrigger]) System.abortJob(j.Id);
]]></executeAnonymous>
<!-- upload code -->
<sf:deploy
username="${de.username}"
password="${de.password}"
serverurl="https://login.salesforce.com"
deployRoot="src"
/>
<!-- restart scheduled job -->
<executeAnonymous username="${de.username}" password="${de.password}"><![CDATA[
System.schedule('New Job', '0 0 * * * ?', new Job());
]]></executeAnonymous>
</target>
<!--
! The MIT License (MIT)
!
! Copyright (c) 2014 bigass.force.com
!
! Permission is hereby granted, free of charge, to any person obtaining a copy
! of this software and associated documentation files (the "Software"), to deal
! in the Software without restriction, including without limitation the rights
! to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
! copies of the Software, and to permit persons to whom the Software is
! furnished to do so, subject to the following conditions:
!
! The above copyright notice and this permission notice shall be included in
! all copies or substantial portions of the Software.
!
! THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
! IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
! FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
! AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
! LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
! OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
! THE SOFTWARE.
!-->
<project default="usage" basedir="." xmlns:sf="antlib:com.salesforce" xmlns:ml="org.missinglink.ant.task.http.HttpClientTask">
<!-- Download from https://login.salesforce.com/dwnld/SfdcAnt/salesforce_ant_32.0.zip -->
<taskdef uri="antlib:com.salesforce" classpath="lib/salesforce_ant_32.0/ant-salesforce.jar" />
<!-- Download from https://code.google.com/p/missing-link/ -->
<taskdef name="http" uri="org.missinglink.ant.task.http.HttpClientTask" classname="org.missinglink.ant.task.http.HttpClientTask" classpath="lib/ml-ant-http-1.1.3/ml-ant-http-1.1.3.jar"/>
<property file="build.properties"/>
<property environment="env"/>
<target name="deploy" description="Deploys all package components to dev org">
<!-- stop scheduled job -->
<executeAnonymous username="${de.username}" password="${de.password}"><![CDATA[
for (SObject j : [SELECT Id FROM CronTrigger]) System.abortJob(j.Id);
]]></executeAnonymous>
<!-- upload code -->
<sf:deploy
username="${de.username}"
password="${de.password}"
serverurl="https://login.salesforce.com"
deployRoot="src"
/>
<!-- restart scheduled job -->
<executeAnonymous username="${de.username}" password="${de.password}"><![CDATA[
System.schedule('New Job', '0 0 * * * ?', new Job());
]]></executeAnonymous>
</target>
<macrodef name="executeAnonymous" description="Invoke ExecuteAnonymous using Salesforce Tooling API">
<attribute name="username" description="Salesforce user name."/>
<attribute name="password" description="Salesforce password."/>
<text name="apexcode"/>
<sequential>
<!-- Login to populate serverUrl / sessionId / instance -->
<login username="@{username}" password="@{password}" />
<!-- Execute using HTTP GET -->
<ml:http url="${instance}/services/data/v32.0/tooling/executeAnonymous" method="GET" outfile="lib/executeAnonymousResult.json" statusProperty="loginResponseStatus">
<headers>
<header name="Authorization" value="Bearer ${sessionId}"/>
</headers>
<query>
<parameter name="anonymousBody" value="@{apexcode}"/>
</query>
</ml:http>
<!-- Fail on error -->
<loadfile property="executeAnonymousResult" srcFile="lib/executeAnonymousResult.json" />
<fail message="Execute Anonymous failed: ${executeAnonymousResult}">
<condition>
<resourcecontains resource="lib/executeAnonymousResult.json" substring="&quot;success&quot;:false" />
</condition>
</fail>
<!-- Show output -->
<echo message="${executeAnonymousResult}" />
</sequential>
</macrodef>
<macrodef name="login" description="Login to salesforce and populate serverUrl / sessionId / instance">
<attribute name="username" description="Salesforce user name."/>
<attribute name="password" description="Salesforce password."/>
<sequential>
<!-- Obtain Session Id via Login SOAP service -->
<ml:http url="https://login.salesforce.com/services/Soap/c/32.0" method="POST" failonunexpected="false" outfile="lib/loginResult.xml" statusProperty="loginResponseStatus">
<headers>
<header name="Content-Type" value="text/xml"/>
<header name="SOAPAction" value="login"/>
</headers>
<entity><![CDATA[
<env:Envelope xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:env='http://schemas.xmlsoap.org/soap/envelope/'>
<env:Body>
<sf:login xmlns:sf='urn:enterprise.soap.sforce.com'>
<sf:username>@{username}</sf:username>
<sf:password>@{password}</sf:password>
</sf:login>
</env:Body>
</env:Envelope>]]>
</entity>
</ml:http>
<!-- Fail on error -->
<loadfile property="loginResult" srcFile="lib/loginResult.xml" />
<fail message="Login failed: ${loginResult}">
<condition>
<resourcecontains resource="lib/loginResult.xml" substring="faultcode" />
</condition>
</fail>
<!-- Parse response into properties -->
<xmlproperty file="lib/loginResult.xml" />
<echo file="lib/serverUrl.txt" message="${soapenv:Envelope.soapenv:Body.loginResponse.result.serverUrl}" />
<echo file="lib/sessionId.txt" message="${soapenv:Envelope.soapenv:Body.loginResponse.result.sessionId}" />
<echo file="lib/instance.txt" message="${soapenv:Envelope.soapenv:Body.loginResponse.result.serverUrl}" />
<replaceregexp file="lib/instance.txt" match="(https://[^/]+)/.*" replace="\1" />
<loadfile property="sessionId" srcFile="lib/sessionId.txt" />
<loadfile property="instance" srcFile="lib/instance.txt" />
</sequential>
</macrodef>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment