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>
Related
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!
I have the following Ant targets in my project.xml:
<target name="to.run.under.conditions">
</target>
<target name="deploy1">
<antcall target="deploy2"/>
</target>
<target name="deploy2">
<antcall target="to.run.under.conditions"/>
</target>
<target name="another.target">
<antcall target="deploy1"/>
</target>
My intent is to be able to exclude the target to.run.under.conditions when running another.target. I am not very familiar with ANT and I am struggling to understand how to approach the issue. I have tried to employ unless="${target.running}" in and set the property as true in the condition task inside the target name ="target.running"
Can you help with this?
Thank you for your help,
I.
----EDIT UPDATED SOLUTION----
This is my current attempt (I am using ANT 1.8.2):
<target name="to.run.under.conditions" if="${target.running}">
</target>
<target name="another.target">
<property name="target.running" value="false"/>
</target>
If I am not mistaken, since the property is set to false inside another.target, then to.run.under.conditions should not be run (I might be wrong, though). Does it make sense? Any comment is much appreciated!
Try this:
<target name="build-module-A" if="module-A-present"/>
<target name="build-own-fake-module-A" unless="module-A-present"/>
In the first example, if the module-A-present property is set (to any value, e.g. false), the target will be run. In the second example, if the module-A-present property is set (again, to any value), the target will not be run.
Please see Any Targets for more information.
I have ended up with this solution that seems to be work as expected:
<target name="deploy2">
<if>
<equals arg1="${target.running}" arg2="true" />
<then>
<echo message="the target will not run" />
</then>
<else>
<echo message="the target will run" />
<antcall target="to.run.under.conditions"/>
</else>
</if>
</target>
<target name="to.run.under.conditions">
</target>
<target name="another.target">
<property name="target.running" value="true"/>
</target>
Hope this helps,
I.
I had ant build file for windows box and can I use the same build file for deploying on Unix also ?
Take all the file names and OS-dependent items and put them in properties files with the same keys. Use the <condition> task to load the correct properties file:
<target name="init-os">
<condition property="os.windows">
<os family="windows"/>
</condition>
<condition property="os.unix">
<os family="unix"/>
</condition>
</target>
<target name="init-windows-properties" depends="init-os" if="os.windows">
<property file="windows.properties"/>
</target>
<target name="init-unix-properties" depends="init-os" if="os.unix">
<property file="unix.properties"/>
</target>
<target name="init-properties" depends="init-windows-properties, init-unix-properties"/>
<target name="init-directories" depends="init-properties">
<mkdir .../>
<!-- More directories -->
</target>
<target name="init" depends="init-properties, init-directories"/>
Add more to the "init" target for the rest of the work you need to do.
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>
I m trying to do this with Ant:
<property name="test" value="123"/>
<target name="helloworld" depends="${test}"/>
But I'm getting error "Target ${test} does not exist in this project."
So I m guessing I can do this?
You can use the AntCall Task to call a Task inside another Task.
<project>
<target name="asdf">
<property name="prop" value="qwer" />
<antcall target="${prop}" />
</target>
<target name="qwer">
<echo message="in qwer" />
</target>
</project>
To make one depend on the other, you can set a parameter in the dependent task and check it in your calling task.
Rather than depends, you can check a property using the if attribute. See the manual for more details.
For example:
<target name="helloworld" if="test"/>
Note this only checks if the property is set (you can use unless to check if it is unset).
An alternative, more complex but powerful, approach is to use a nested condition on a depended target:
<target name="helloworld" depends="myTarget.check" if="myTarget.run">
...
</target>
<target name="myTarget.check">
<condition property="test">
<and>
<available file="foo.txt"/>
<available file="bar.txt"/>
</and>
</condition>