2009-01-17 17 views
36

我想通过os类型以不同的方式在ant任务中设置属性。使用ant来检测os并设置属性

该属性是一个目录,在Windows中,我希望它是“c:\ flag”在unix/linux“/ opt/flag”中。

我当前的脚本只适用于当我用默认目标运行它,但为什么?

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

在我已经添加了我所有的Ant目标 “取决于= checksw_path”。

如果我在Windows中运行默认目标,我已经正确地“c:\ flag”但如果我运行一个非默认目标,我已经调试进入if_windows,但指令“”没有设置该属性仍然是/ opt/flag。我正在使用ant 1.7.1。

回答

-3

我解决与用于使用-Dsw.root = C sw.root值执行ant任务:\标志(对于Windows)或-Dsw.root = /选择/ superwaba(对于Linux)。

反正感谢

20

将您的条件移出<target />,因为您的目标可能未被调用。

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

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

试着在你的java任务设置<sysproperty key="foobar" value="fowl"/>。 然后,在你的应用程序中,使用System.getProperty(“foobar”);

12

您需要将值设置为“true”以使if条件起作用。请参见下面的代码:

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

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

HTH, 哈日

2

我用这样的剧本,工作很适合我:

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

如果想设置基于OS是单一的财产,你可以设置它直接和,而不需要创建任务:

<condition property="sw.root" value="c:\flag"> 
    <os family="windows" /> 
</condition> 

<condition property="sw.root" value="/opt/flag"> 
     <os family="unix" /> 
</condition> 

<property name="sw.root" value="/os/unknown/"/> 
0

通过使用Ant Contrib你可以通过减少的Elemen量简化您的构建文件您需要声明以添加这些条件。

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