How to check if a property has value in Ant - ant

I have an Ant XML file which I use for build.
I have 3 properties. I want to break the build if these properties does not contain any value. Also I want to break the build if the value is empty.
How can I do this in Ant?
I a using Ant and not Ant-contrib.

You can use conditions using the <fail> task:
<fail message="Property "foo" needs to be set to a value">
<condition>
<or>
<equals arg1="${foo}" arg2=""/>
<not>
<isset property="foo"/>
</not>
</or>
</condition>
This is equivalent to saying if (not set ${foo} or ${foo} = "") is pseudocode. You have to read the XML conditions from the inside out.
You could have used the <unless> clause on the <fail> task if you only cared whether or not the variable was set, and not whether it has an actual value.
<fail message="Property "foo" needs to be set"
unless="foo"/>
However, this won't fail if the property is set, but has no value.
There's a trick that can make this simpler
<!-- Won't change the value of `${foo}` if it's already defined -->
<property name="foo" value=""/>
<fail message="Property "foo" has no value">
<condition>
<equals arg1="${foo}" arg2=""/>
</condition>
</fail>
Remember that I can't reset a property! If ${foo} already has a value, the <property> task above won't do anything. This way, I can eliminate the <isset> condition. It might be nice since you have three properties:
<property name="foo" value=""/>
<property name="bar" value=""/>
<property name="fubar" value=""/>
<fail message="You broke the build, you dufus">
<condition>
<or>
<equals arg1="${foo}" arg2=""/>
<equals arg1="${bar}" arg2=""/>
<equals arg1="${fubar}" arg2=""/>
</or>
</condition>
</fail>

Building on the other answers, this is my preferred form, as a Macro:
<!-- Macro to require a property is not blank -->
<macrodef name="prop-require">
<attribute name="prop"/>
<sequential>
<fail message="Property "#{prop}" must be set">
<condition>
<not>
<isset property="#{prop}"/>
</not>
</condition>
</fail>
<fail message="Property "#{prop}" must not be empty">
<condition>
<equals arg1="${#{prop}}" arg2=""/>
</condition>
</fail>
</sequential>
</macrodef>
To Be used as:
<target name="deploy.war" description="Do the war deployment ;)">
<prop-require prop="target.vm" />
<prop-require prop="target.vip" />
<!-- ... -->
For brevity you can collapse the two fail elements into one by using an <or>, but I prefer my error messages to treat me like I cannot think for myself ;)

You could try using conditions... or creating a target with unless

With Ant addon Flaka you may use patterns like :
<property name="foo" value="bar"/>
...
<fl:unless test="has.property.foo">
...
</fl:unless>
...
<fl:when test="has.property.foo">
...
</fl:when>
Concrete check for emptyness :
<fl:when test=" empty '${foo}' ">
<fail message="Houston we have a problem!!"/>
</fl:when>
A complete example, also using some equals check with 'eq' (opposite would be 'neq'):
<project xmlns:fl="antlib:it.haefelinger.flaka">
<!-- some if/then/else construct -->
<fl:choose>
<!-- if -->
<when test=" '${buildtype}' eq 'prod' ">
<!-- then -->
<echo>..starting ProductionBuild</echo>
</when>
<when test=" '${buildtype}' eq 'test' ">
<!-- then -->
<echo>..starting TestBuild</echo>
</when>
<!-- else -->
<otherwise>
<fl:unless test="has.property.dummybuild">
<fail message="No valid buildtype !, found => '${buildtype}'"/>
</fl:unless>
<echo>.. is DummyBuild</echo>
</otherwise>
</fl:choose>
</project>
output with ant -f build.xml -Dbuildtype=prod or
ant -f build.xml -Dbuildtype=prod -Ddummybuild=whatever
[echo] ..starting ProductionBuild
output with typo => ant - build.xml -Dbuildtype=testt
BUILD FAILED
/home/rosebud/workspace/AntTest/build.xml:21: No valid buildtype !, found => 'testt'
output with ant -f build.xml -Ddummybuild=whatever
[echo] .. is DummyBuild

I'm on an older version of Ant, so isset wasn't available. Instead I used the following notation with the double $ in the equals.
<target name="xxx">
<echo message="${contextRoot}" />
<if>
<!-- check if the contextRoot property is defined. -->
<equals arg1="${contextRoot}" arg2="$${contextRoot}" />
<then>
<!-- it isn't set, set a default -->
<property name="contextRoot" value="/WebAppCtx" />
</then>
</if>
<echo message="${contextRoot}" />
</target>

Try this.
<condition property="isPropertySet" value="true" else="false">
<and>
<isset property="my_property"/>
<length string="${my_property}" trim="true" when="greater" length="0"/>
</and>
</condition>
<fail unless="isPropertySet" message="The property my_property is not set."/>

Since Ant 1.9.1, it is possible to add if and unless attributes on all
tasks and nested elements using special namespaces.
In order to use this feature you need to add the following namespace declarations
xmlns:if="ant:if"
xmlns:unless="ant:unless"
The if and unless namespaces support the following conditions:
true - true if the value of the attribute evaluates to true
blank - true if the value of the attribute is null or empty
set - true if the specified property is set
Sample:
<project name="tryit"
xmlns:if="ant:if"
xmlns:unless="ant:unless">
<exec executable="java">
<arg line="-X" if:true="${showextendedparams}"/>
<arg line="-version" unless:true="${showextendedparams}"/>
</exec>
<condition property="onmac">
<os family="mac"/>
</condition>
<echo if:set="onmac">running on MacOS</echo>
<echo unless:set="onmac">not running on MacOS</echo>
</project>
From Ant manual "If And Unless"

Related

Use pure Ant to implement if else condition (check command line input)

I am a newbie in Ant. My ant script receives a user input variable named "env" from command line:
e.g. ant doIt -Denv=test
The user input value could be "test","dev" or "prod".
I also have the "doIt" target:
<target name="doIt">
//What to do here?
</target>
In my target, I would like to create the following if else condition for my ant script:
if(env == "test")
echo "test"
else if(env == "prod")
echo "prod"
else if(env == "dev")
echo "dev"
else
echo "You have to input env"
That's to check which value user has inputted from command line, then, print a message accordingly.
I know with ant-Contrib, I can write ant script with <if> <else> . But for my project, I would like to use pure Ant to implement if else condition. Probably, I should use <condition> ?? But I am not sure how to use <condition> for my logic. Could some one help me please?
You can create few targets and use if/unless tags.
<project name="if.test" default="doIt">
<target name="doIt" depends="-doIt.init, -test, -prod, -dev, -else"></target>
<target name="-doIt.init">
<condition property="do.test">
<equals arg1="${env}" arg2="test" />
</condition>
<condition property="do.prod">
<equals arg1="${env}" arg2="prod" />
</condition>
<condition property="do.dev">
<equals arg1="${env}" arg2="dev" />
</condition>
<condition property="do.else">
<not>
<or>
<equals arg1="${env}" arg2="test" />
<equals arg1="${env}" arg2="prod" />
<equals arg1="${env}" arg2="dev" />
</or>
</not>
</condition>
</target>
<target name="-test" if="do.test">
<echo>this target will be called only when property $${do.test} is set</echo>
</target>
<target name="-prod" if="do.prod">
<echo>this target will be called only when property $${do.prod} is set</echo>
</target>
<target name="-dev" if="do.dev">
<echo>this target will be called only when property $${do.dev} is set</echo>
</target>
<target name="-else" if="do.else">
<echo>this target will be called only when property $${env} does not equal test/prod/dev</echo>
</target>
</project>
Targets with - prefix are private so user won't be able to run them from command line.
If any of you require a plain if/else condition (without elseif); then use the below:
Here I am dependent on a env variable DMAPM_BUILD_VER, but it may happen that this variable is not set in the env. So I need to have mechanism to default to a local value.
<!-- Read build.version value from env variable DMAPM_BUILD_VER. If it is not set, take default.build.version. -->
<property name="default.build.version" value="0.1.0.0" />
<condition property="build.version" value="${env.DMAPM_BUILD_VER}" else="${default.build.version}">
<isset property="env.DMAPM_BUILD_VER"/>
</condition>

Check multiple properties are set and not blank (ant)

I'm in a situation that involves running an ant build with optional parameters that are always specified but but not always defined, like so
ant -DBUILD_ENVIRONMENT=test -Dusername_ext= -Dconf.dir_ext= -Dcgi-dir_ext=
If the parameters are not given values on the command line they will be by loading a .properties file. I have the following code that will check if the property isset and is not blank.
<if>
<bool>
<and>
<isset property="username_ext"/>
<not>
<equals arg1="${username_ext}" arg2="" />
</not>
</and>
</bool>
<then>
<property name="username" value="${username_ext}" />
</then>
</if>
<property file="${BUILD_ENVIRONMENT}.properties" />
Since there are multiple properties it seems like I should write a target that will do the same actions for each property rather than repeat that code every time.
<antcall target="checkexists">
<property name="propname" value="username"/>
<property name="paramname" value="username_ext"/>
</antcall>
<antcall target="checkexists">
<property name="propname" value="conf.dir"/>
<property name="paramname" value="conf.dir_ext"/>
</antcall>
But AFAIK an antcall will not set a global property. How then can I write a target that will accept the name of a parameter it needs to check is set and is not blank, and then copy that in to a parameter that other targets can use?
Rather than using a target you could use a macro to conditionally set properties based on whether or not another property is set to a non-empty value.
<macrodef name="set-property">
<attribute name="name" />
<attribute name="if-property-isset" />
<attribute name="value" default="${#{if-property-isset}}" />
<sequential>
<condition property="#{name}" value="#{value}">
<and>
<isset property="#{if-property-isset}" />
<not>
<equals arg1="${#{if-property-isset}}" arg2="" />
</not>
</and>
</condition>
</sequential>
</macrodef>
<target name="test-macro">
<set-property name="username" if-property-isset="username_ext" />
<set-property name="conf.dir" if-property-isset="conf.dir_ext" />
<property name="conf.dir" value="default conf directory" />
<echo message="username = ${username}" />
<echo message="conf.dir = ${conf.dir}" />
</target>
Output
$ ant test-macro -Dusername_ext=jsmith -Dconf.dir_ext=
Buildfile: /your/project/build.xml
test-macro:
[echo] username = jsmith
[echo] conf.dir = default conf directory
BUILD SUCCESSFUL
Total time: 1 second
Alternate Property Value
This macro also allows you set the property to a different value than the one provided on the command line.
<target name="test-macro">
<set-property name="username" if-property-isset="username_ext"
value="It worked!" />
<set-property name="conf.dir" if-property-isset="conf.dir_ext" />
<property name="conf.dir" value="default conf directory" />
<echo message="username = ${username}" />
<echo message="conf.dir = ${conf.dir}" />
</target>
Output
$ ant test-macro -Dusername_ext=jsmith -Dconf.dir_ext=
Buildfile: /your/project/build.xml
test-macro:
[echo] username = It worked!
[echo] conf.dir = default conf directory
BUILD SUCCESSFUL
Total time: 1 second

Ant check existence for a set of files

In ant, how can i check if a set of files (comma-separated list of paths) exist or not?
For example I need to check if all paths listed in myprop exist and if so i want to set a property pathExist:
<property name="myprop" value="path1,path2,path3"/>
So in the example all of path1 path2 path3 must exist to set pathExist to true, otherwise false.
I discovered that for a single resource I can use the resourceexist task, but i can't figure out how to use that with a comma-separated list of paths.
How can I check the existence for a set of paths? Thanks!
You can use a combination of a filelist, restrict and condition task for this.
In the below example a filelist is created from the property with the comma-separated list of files. Using restrict a list of the files that don't exist is found. This is placed in a property which will be empty if all the files are found.
<property name="myprop" value="path1,path2,path3"/>
<filelist id="my.files" dir="." files="${myprop}" />
<restrict id="missing.files">
<filelist refid="my.files"/>
<not>
<exists/>
</not>
</restrict>
<property name="missing.files" refid="missing.files" />
<condition property="pathExist" value="true" else="false">
<length string="${missing.files}" length="0" />
</condition>
<echo message="Files all found: ${pathExist}" />
You could use something like this to generate a failure message listing the missing files:
<fail message="Missing files: ${missing.files}">
<condition>
<length string="${missing.files}" when="greater" length="0" />
</condition>
</fail>
Bundled conditions are the shortest solution to check for existence of multiple dirs or files :
<condition property="pathExist">
<and>
<available file="/foo/bar" type="dir"/>
<available file="/foo/baz" type="dir"/>
<available file="path/to/foobar.txt"/>
...
</and>
</condition>
To check for a commaseparated list of path use Ant addon Flaka , f.e. :
<project xmlns:fl="antlib:it.haefelinger.flaka">
<!-- when you have a cvs property use split function
to get your list to iterate over -->
<property name="checkpath" value="/foo/bar,/foo/baz,/foo/bazz"/>
<fl:for var="file" in="split('${checkpath}', ',')">
<fl:fail message="#{file} does not exist !!" test="!file.tofile.exists"/>
</fl:for>
</project>
Another possibility is the use of scriptcondition task with a jvm scripting language like groovy,beanshell .. etc.
This can be solved with the set operations of Ant´s resource collections. If you calculate the intersection of the list of required files with the list of existing files it must not differ from the list of required files. This shows how to do it:
<property name="files" value="a.jar,b.jar,c.jar"/>
<target name="missing">
<condition property="missing">
<resourcecount when="ne" count="0">
<difference id="missing">
<intersect>
<filelist id="required" dir="." files="${files}"/>
<fileset id="existing" dir="." includes="*.jar"/>
</intersect>
<filelist refid="required"/>
</difference>
</resourcecount>
</condition>
</target>
<target name="check" depends="missing" if="missing">
<echo message="missing: ${toString:missing}"/>
</target>
The check target reports the list of missing files only if a file is missing.

How can I check if file exists, and if not kill the build?

How can I stop a build, and notify the user if a file does not exist? I know I can use the available task to set a property if a file exists, but I'm not sure how I would stop a build and echo something.
I would like to stick with core tasks if possible.
You can use the fail task for all your failing needs. The last example on that page is actually pretty much what you need
<fail message="Files are missing.">
<condition>
<not>
<resourcecount count="2">
<fileset id="fs" dir="." includes="one.txt,two.txt"/>
</resourcecount>
</not>
</condition>
</fail>
A little simpler (I wish it could be made simpler)
<fail message="file ${myfile} not set or missing">
<condition>
<not>
<available file="${myfile}" />
</not>
</condition>
</fail>
Set your property and use the Fail task with the if attribute.
This can be done more compactly (as indicated by Jason Punyon). Specifically, assuming the file you want is in the property file, do:
<available file="${file}" property="file.exists" />
<fail message="File missing: ${file}" unless="file.exists" />
These kind of checks are common, so it might pay off to use a macro.
Here is a macro based on the solution by leonbloy:
<macrodef name="require">
<attribute name="file"/>
<attribute name="message" default="file #{file} not set or missing"/>
<sequential>
<fail message="#{message}">
<condition>
<not>
<available file="#{file}" />
</not>
</condition>
</fail>
</sequential>
</macrodef>
Use like this:
<require file="${myfile}" />
or
<require file="${myfile}" message="my custom message" />

How to set an Ant property only if it is unset

I can't figure out how to set an Ant property on the condition that it has not been set (i.e it is not defined in the properties file and should automatically default).
So far, I only have the following code:
<condition property="core.bin" value="../bin">
<isset property="core.bin"/>
</condition>
But this only seems to work if the value is defined in a <property> tag.
Does anyone know how to conditionally set a property for the first time if it currently unset?
You simply can set the property with the property-task. If the property is already set, the value is unchanged, because properties are immutable.
But you can also include 'not' in your condition:
<condition property="core.bin" value="../bin">
<not>
<isset property="core.bin"/>
</not>
</condition>
Ant does this by default; if the property is already set; setting it again has no effect:
<project name="demo" default="demo">
<target name="demo" >
<property name="aProperty" value="foo" />
<property name="aProperty" value="bar" /> <!-- already defined; no effect -->
<echo message="Property value is '${aProperty}'" /> <!-- Displays 'foo' -->
</target>
</project>
Gives
/c/scratch> ant -f build.xml
Buildfile: build.xml
demo:
[echo] Property value is '${aProperty}'
BUILD SUCCESSFUL
Total time: 0 seconds
/c/scratch> ant -f build.xml
Buildfile: build.xml
demo:
[echo] Property value is 'foo'
BUILD SUCCESSFUL
Properties cannot be redefined; to do this you need to use something like the variable task from ant-contrib.
The easiest way to do what you want:
<if>
<not>
<isset property="your.property"/>
</not>
<then>
<property name="your.property" value="your.value"/>
</then>
</if>
There is support of using 'else' within : https://ant.apache.org/manual/Tasks/condition.html to serve your exact purpose.
else
The value to set the property to if the condition evaluates to false. By default the property will remain unset. Since Apache Ant 1.6.3
So change to :
<condition property="core.bin" else="../bin">
<isset property="core.bin"/>
</condition>
Properties in Ant are immutable. After defined they cannot be changed.
But the Ant Contrib package offers the variable task. It works like a property but the values can be modified and unset.
Exmaple from the variable task documentation:
<var name="x" value="6"/>
<if>
<equals arg1="${x}" arg2="6" />
<then>
<var name="x" value="12"/>
</then>
</if>
<echo>${x}</echo> <!-- will print 12 -->

Resources