Using ant to detect os and set property

2019-01-07 17:36发布

I want to set a property in an ant task differently by os type.

The property is a directory, in windows i want it to be "c:\flag" in unix/linux "/opt/flag".

My current script only works when i run it with the default target, but why ?

    <target name="checksw_path" depends="if_windows, if_unix"/>

<target name="checkos">
    <condition property="isWindows">
        <os family="windows" />
    </condition>

    <condition property="isLinux">
        <os family="unix" />
    </condition>
</target>

<target name="if_windows" depends="checkos" if="isWindows">
   <property name="sw.root" value="c:\flag" />
    <echo message="${sw.root}"/>
</target>

<target name="if_unix" depends="checkos" if="isLinux">
    <property name="sw.root" value="/opt/flag" />
    <echo message="${sw.root}"/>
</target>

In all my ant targets i've added a "depends=checksw_path".

If i run the default target in windows i've got correctly "c:\flag" but if i run a non default target i've got that the debug goes in the if_windows but the instruction " " does not set the property that remains /opt/flag. I'm using ant 1.7.1.

标签: ant
8条回答
戒情不戒烟
2楼-- · 2019-01-07 18:05

First you want to set a variable to true or false based on the operating system (OS):

<condition property="IS_WINDOWS" value="true" else="false">
   <os family="windows"/>
</condition> 

Then you want to trigger your logic using the variable. For that you can find the answer here:

http://boulderapps.co/running-different-ant-tasks-depending-on-the-operating-system

查看更多
乱世女痞
3楼-- · 2019-01-07 18:08

You need to set value "true" to the property for the if condition to work. See the code below:

<target name="checkos">
    <condition property="isWindows" value="true">
            <os family="windows" />
    </condition>

    <condition property="isLinux" value="true">
            <os family="unix" />
    </condition>
</target>

HTH, Hari

查看更多
smile是对你的礼貌
4楼-- · 2019-01-07 18:08

By using Ant Contrib you can simplify your build file by reducing the amount of elements you need to declare in order add these conditions.

<!--Tell Ant to define the Ant Contrib tasks from the jar-->
<taskdef resource="net/sf/antcontrib/antcontrib.properties">
    <classpath>
        <pathelement location="path/to/ant-contrib-0.6.jar"/>
    </classpath>
</taskdef>

<!--Do your OS specific stuff-->
<target name="checkos">
    <if>
        <os family="unix"/>
        <then>
            <!--Do your Unix stuff-->
        </then>
        <elseif>
            <os family="windows"/>
            <then>
                <!--Do your Windows stuff-->
            </then>
        </elseif>
    </if>
</target>
查看更多
孤傲高冷的网名
5楼-- · 2019-01-07 18:12

I solved executing the ant task with the value for sw.root using -Dsw.root=c:\flag (for windows) or -Dsw.root=/opt/superwaba (for linux).

Anyway thanks

查看更多
ら.Afraid
6楼-- · 2019-01-07 18:18

I used this script, and it worked well for me:

<project name="dir" basedir=".">

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

  <condition property="isUnix">
    <os family="unix" />
  </condition>

  <target name="setWindowsRoot" if="isWindows">
    <property name="root.dir" value="c:\tmp\" />
  </target>

  <target name="setUnixRoot" if="isUnix">
    <property name="root.dir" value="/i0/" />
  </target>

  <target name="test" depends="setWindowsRoot, setUnixRoot">
    <mkdir dir="${root.dir}" />
  </target>

</project>
查看更多
爷的心禁止访问
7楼-- · 2019-01-07 18:18

Try setting <sysproperty key="foobar" value="fowl"/> in your java task. Then, in your app, use System.getProperty("foobar");

查看更多
登录 后发表回答