Alex Rivera | Logout

How to check if a property has value in Ant

Asked 2012-02-09T08:45:01.750
38

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.

ant
Edit
Report

1 Answer

0

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
answered 2012-02-09T22:10:26.613

Your Answer