How to associate a JVM argument with an Ant task? - ant

Currently, I have an Ant task that I invoke like this:
ant -f build-direct.xml email -Dmail.smtp.ssl.protocols=TLSv1.2
This is the email task:
<target name="email" depends="init" unless="email.skip">
<condition property="build.version.stableStatus" value="stable" else="non-stable">
<or>
<matches string="${build.version.tag}" pattern="^GA$"/>
<matches string="${build.version.tag}" pattern="^GA-E[0-9]{2}$"/>
</or>
</condition>
<echo message="is stable: ${build.version.stableStatus}"/>
<mail mailhost="${email.host}" mailport="${email.port}" ssl="${email.ssl}" user="${email.smtp.user}" password="${email.smtp.password}"
replyto="${email.from}" from="${email.from}" tolist="${email.to}" cclist="${email.cc}"
subject="The ${build.version} build has completed">
<message>email body here</message>
</mail>
</target>
I would like to simplify this command by permanently associating the argument -Dmail.smtp.ssl.protocols=TLSv1.2 with this email task. I read about the <jvmarg> tag but I am not sure how to apply it. Can someone guide me on how to do this? Thanks!

Related

Getting condition doesn't support the nested "then" element in <condition>

I'm new to Ant/Apache. When I tried to use <condition> tag in XML it's throwing an error. condition doesn't support the nested "then" element. Here is my code
<target name="determine-ae-build">
<condition property="ApplicationName">
<equals arg1="${ApplicationName}" arg2="new"/>
<then>
<echo>3.9 Robots Config Copied</echo>
</then>
<else>
<condition property="ApplicationName">
<equals arg1="${ApplicationName}" arg2="old"/>
<then>
<echo>3.8 Robots Config Copied</echo>
</then>
<else>
<echo>3.9 Robots Config Copied</echo>
</else>
</condition>
</else>
</condition>
</target>
I've tried with IF also but since my Ant version is not supporting to do this. Can someone help to resolve this issue. Thanks! in advance
The condition task simply sets a property; it doesn't contain nested build logic. The property that it sets can later be used to control which targets are executed.
While you can use antcontrib's extra if, then, and else tasks to accomplish something like what you showed in your example, I'd recommend sticking to the native Ant approach, which relies on target dependencies and uses separate targets to control build logic:
<project name="build" basedir="." default="build">
<target name="build" depends="copy-3.8,copy-3.9" />
<target name="copy-3.8" depends="determine-ae-build" if="copy.old">
<echo>3.8 Robots Config Copied</echo>
</target>
<target name="copy-3.9" depends="determine-ae-build" unless="copy.old">
<echo>3.9 Robots Config Copied</echo>
</target>
<target name="determine-ae-build">
<condition property="copy.old">
<equals arg1="${ApplicationName}" arg2="old"/>
</condition>
</target>
</project>
With the above script, you would run ant build (possibly with -DApplicationName=old). The build target depends on both copy targets, both of which depend on determine-ae-build. The determine-ae-build target will therefore run first. If ApplicationName is set to "old" (either from a properties file that has been loaded, or from being provided in command line with -DApplicationName=old) then the property copy.old will be set to true. Otherwise it will remain unset.
Then copy-3.8 and copy-3.9 will be called. If copy.old is is true, copy-3.8 will run. Otherwise, it will be skipped. copy-3.9 has no condition so it will run no matter what.
Lastly, the build target will execute because it was the original target called from the command line, but it contains no actual steps so the build will finish.
<target name="prepare-copy" description="copy file based on condition" depends="determine-ae-build, prepare-copy-old, prepare-copy-new, prepare-copy-default">
<sleep seconds="10"/> --To read the results
</target>
<target name="prepare-copy-old" description="copy file based on condition" if="copy.old">
<echo>Old File Copied </echo>
</target>
<target name="prepare-copy-new" description="copy file based on condition" if="copy.new">
<echo>New File Copied</echo>
</target>
<target name="prepare-copy-default" description="copy file based on false condition" if="copy.default">
<echo>Default File Coping</echo>
</target>
<target name="determine-ae-build">
<condition property="copy.old">
<equals arg1="${ApplicationName}" arg2="old"/>
</condition>
<condition property="copy.new">
<equals arg1="${ApplicationName}" arg2="new"/>
</condition>
<condition property="copy.default">
<not>
<or>
<equals arg1="${ApplicationName}" arg2="new"/>
<equals arg1="${ApplicationName}" arg2="old"/>
</or>
</not>
</condition>
</target>
Explanation: Calling way "ant -Dcopy.old = true prepare-copy". Here we are passing to copy old file hence, "Old File Copied" will copied. If you call it like "ant prepare-copy" it'll call "Default File Coping".
Kindly Accept my answer if it is answered your question.Thankyou!

Pass variables to build.xml from VSTS build

I'm setting up the build pipeline in VSTS (Visual Studio Team Services), and I'm not able to pass a variable to the build. I don't know what the syntax should I use actually to get the variable in the build I guess. I created the variable in the VSTS:
I use standard build.xml file:
<project name="Sample usage of Salesforce Ant tasks" default="deployCodeAndRunTests" basedir="." xmlns:sf="antlib:com.salesforce">
<property file="build.properties"/>
<property environment="env"/>
<!-- Setting default value for username, password and session id properties to empty string
so unset values are treated as empty. Without this, ant expressions such as ${sf.username}
will be treated literally.
-->
<condition property="sf.username" value=""> <not> <isset property="sf.username"/> </not> </condition>
<condition property="sf.password" value=""> <not> <isset property="sf.password"/> </not> </condition>
<condition property="sf.sessionId" value=""> <not> <isset property="sf.sessionId"/> </not> </condition>
<taskdef resource="com/salesforce/antlib.xml" uri="antlib:com.salesforce">
<classpath>
<pathelement location="ant-salesforce.jar" />
</classpath>
</taskdef>
<!-- Deploy code and run tests. If test fails, rollback deploy. -->
<target name="deployCodeAndRunTests">
<sf:deploy
username="${sf.username}"
password="${sf.password}"
sessionId="${sf.sessionId}"
serverurl="${sf.serverurl}"
maxPoll="${sf.maxPoll}"
deployRoot="..\src"
testLevel="NoTestRun"
rollbackOnError="true"
logType="Detail"/>
</target>
Any suggestion on how to get the variables to the build?
To run the ant build with the parametrs from VSTS you need set them with the property task in build.xml
<property name="sf.username" value="${sfUsername}"/>
<property name="sf.password" value="${sfPassword}"/>
<property name="sf.serverurl" value="${sfServerurl}"/>
<property name="sf.testLevel" value="${sfTestLevel}"/>
Then, when setting up the build in VSTS, you set it to run with parameters, where you assign the variables defined in VSTS. To run the ant build with parameters you use the Options:

How to fail an ant build if a property is not empty

I have an ANT build file which has the line-
<java classname="arq.sparql" fork="true" outputproperty="javaresult" errorproperty="javaerror">
Now I want add the condition to fail the build of the property 'javaerror' is not empty.
So I have the condition written like this :
<fail message="${javaerror}">
<condition>
<not>
<equals javaerror=""/>
</not>
</condition>
</fail>
But this did not work, can you please help.
Kind regards
Som
Your equals condition has the wrong syntax, it will work like that :
<fail message="${javaerror}">
<condition>
<not>
<equals arg1="${javaerror}" arg2=""/>
</not>
</condition>
</fail>
see Ant manual conditions for details
-- EDIT --
Alternatively you could use the new if/unless feature introduced with Ant 1.9.1 but you should use Ant 1.9.3 because of bugs in Ant 1.9.1 see this answer for details
<project xmlns:if="ant:if" xmlns:unless="ant:unless">
<property name="javaerror" value="whatever"/>
<fail message="${javaerror}" unless:blank="${javaerror}"/>
</project>
You're looking for
<fail message="failed" if="javaerror"/>
Fail ant task doc

How to stop ant script execution or skip targets without fail?

I want to terminate an ant script, or skip some targets, but without the fail task. I wanted to do it like this:
`
<target name="init">
<condition property="canContinue">
<not>
<equals arg1="${isOffline}" arg2="1" />
</not>
</condition>
</target>
<target name="init-setup" depends="init" if="canContinue">
<echo message="CanContinue: ${canContinue}"/>
</target>
<target name="doIt" depends="init-setup">
</target>
`
I would like to skip all the targets which depend on init-setup, not just init-setup.
How can I do this?
Thank you!
If you're not using explicit fail tasks, you'll need to add your boolean flag to each target you want to skip.
Using your example:
<target name="doIt" depends="init-setup" if="canContinue">
</target>

Fail Ant build in case of no command line parameters

I am sending a file path as a parameter to ant at command line. I want the build to fail if parameter doesnot exist. What is the way to do that?
Thanks!
Use the if attribute on a target e.g. :
<project name="test" default="init">
<target name="init" if="${path}">
<!--This will only execute if ${path} is defined from the command line-->
</target>
</project>
Second option : more verbose
<project name="test" default="init">
<target name="init">
<fail message="Path is not set! Exiting ant script!">
<condition>
<not>
<isset property="${path}"/>
</not>
</condition>
</fail>
</target>
</project>

Resources