How check for a condition in ant and depending on

2020-02-15 08:37发布

问题:

This is a small piece of code please give a look at it then follow the description....

    <condition property="${param1}">
            <or>
                <istrue value="win-x86"/>
                <istrue value= "win-x86-client"/>
                <istrue value= "win-x64"/>
            </or>
     </condition>
    <target name="Mytarget" if="${param1}">
        <echo message="executing windows family build:::${param1}"/>
    </target>
<target name="print.name" >
    <antcall target="win-x86-build">
       <param name="param1" value="${platform.id}"/>
    </antcall>
</target>

I want that when ever platform.id contains any of the windows family name it should print the message EXECUTING WINDOWS FAMILY BUILD but the problem is that it is printing this message even when the family is unix.

I think either I am not checking the condition properly or else i am doing some other mistake.
Can someone help me out with this please?

回答1:

Peter is trying to explain that you must explicitly specify the property name. Try the following to make your code work:

<project name="demo" default="Mytarget">

    <condition property="windoze">
        <or>
            <equals arg1="${param1}" arg2="win-x86"/>
            <equals arg1="${param1}" arg2="win-x86-client"/>
            <equals arg1="${param1}" arg2="win-x64"/>
        </or>
    </condition>

    <target name="Mytarget" if="windoze">
        <echo message="executing windows family build:::${param1}"/>
    </target>

</project>

A better solution would be to make use of operating system tests built into ANT's condition task.

<project name="demo" default="Mytarget">

    <condition property="windoze">
        <os family="windows"/>
    </condition>

    <target name="Mytarget" if="windoze">
        <echo message="executing windows family build:::${os.name}-${os.arch}-${os.version}"/>
    </target>

</project>


回答2:

Looks like you misunderstood the Condition Task:

property: The name of the property to set.

Try using the Conditionos:

Test whether the current operating system is of a given type.



回答3:

Since ant 1.9.1 you can do this:

<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>


标签: ant