ceving
ceving

Reputation: 23794

How to check two properties in an if condition of an Ant task?

It is possible to execute an Ant target conditionally by specifying an if or unless clause. As far as I can see this clause accepts only one property. How can I check for two properties?

This is an example:

<project default="test">
  <property name="a" value="true"/>
  <property name="b" value="true"/>
  <target name="test-a" if="a">
    <echo>a</echo>
  </target>
  <target name="test-b" if="b">
    <echo>b</echo>
  </target>
  <target name="test-ab" if="a,b">
    <echo>a and b</echo>
  </target>
  <target name="test" depends="test-a,test-b,test-ab"/>
</project>

If I run it, the test-ab target generates no output:

$ ant -f target-if.xml
Buildfile: target-if.xml

test-a:
     [echo] a

test-b:
     [echo] b

test-ab:

test:

BUILD SUCCESSFUL
Total time: 0 seconds

How to specify an and expression for the two properties?

Upvotes: 1

Views: 5742

Answers (2)

ceving
ceving

Reputation: 23794

This is my example with the use of the condition element:

<project default="test">
  <property name="a" value="true"/>
  <property name="b" value="true"/>
  <target name="test-a" if="a">
    <echo>a</echo>
  </target>
  <target name="test-b" if="b">
    <echo>b</echo>
  </target>
  <condition property="a-and-b">
    <and>
      <equals arg1="${a}" arg2="true"/>
      <equals arg1="${b}" arg2="true"/>
    </and>
  </condition>
  <target name="test-ab" if="a-and-b">
    <echo>a and b</echo>
  </target>
  <target name="test" depends="test-a,test-b,test-ab"/>
</project>

Upvotes: 2

iamchris
iamchris

Reputation: 2721

Unfortunately, no. From the ant Targets manual:

Only one propertyname can be specified in the if/unless clause. If you want to check multiple conditions, you can use a dependend target for computing the result for the check:

<target name="myTarget" depends="myTarget.check" if="myTarget.run">
    <echo>Files foo.txt and bar.txt are present.</echo>
</target>

<target name="myTarget.check">
    <condition property="myTarget.run">
        <and>
            <available file="foo.txt"/>
            <available file="bar.txt"/>
        </and>
    </condition>
</target>

Upvotes: 3

Related Questions