【问题标题】:Using ant to detect os and set property使用ant检测os并设置属性
【发布时间】:2009-01-17 11:29:30
【问题描述】:

我想根据操作系统类型在 ant 任务中设置不同的属性。

该属性是一个目录,在 windows 中我希望它是 unix/linux "/opt/flag" 中的 "c:\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 目标中,我添加了一个“depends=checksw_path”。

如果我在 windows 中运行默认目标,我得到了正确的“c:\flag”,但是如果我运行一个非默认目标,我得到了调试进入 if_windows 但指令“”没有设置保留 /opt/flag 的属性。我正在使用 ant 1.7.1。

【问题讨论】:

    标签: ant


    【解决方案1】:

    将您的条件移出&lt;target /&gt;,因为您的目标可能没有被调用。

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

    【讨论】:

      【解决方案2】:

      您需要为该属性设置值“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, 哈里

      【讨论】:

        【解决方案3】:

        我使用了这个脚本,对我来说效果很好:

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

        【讨论】:

          【解决方案4】:

          如果你想根据操作系统设置单个属性,你也可以直接设置,不需要创建任务:

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

          【讨论】:

            【解决方案5】:

            尝试在您的 java 任务中设置 &lt;sysproperty key="foobar" value="fowl"/&gt;。 然后,在您的应用中,使用 System.getProperty("foobar");

            【讨论】:

              【解决方案6】:

              通过使用Ant Contrib,您可以通过减少为添加这些条件而需要声明的元素数量来简化构建文件。

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

              【讨论】:

                【解决方案7】:

                首先您要根据操作系统 (OS) 将变量设置为 true 或 false:

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

                然后你想使用变量来触发你的逻辑。为此,您可以在这里找到答案:

                https://boulderappsco.postach.io/post/running-different-ant-tasks-depending-on-the-operating-system

                【讨论】:

                • 链接已损坏。这就是为什么你不应该在你的答案中只发布链接......总是引用源的相关部分。
                【解决方案8】:

                我使用 -Dsw.root=c:\flag(对于 Windows)或 -Dsw.root=/opt/superwaba(对于 linux)解决了使用 sw.root 的值执行 ant 任务。

                还是谢谢

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 2020-01-08
                  • 2014-02-04
                  • 1970-01-01
                  • 2012-10-16
                  • 2015-01-11
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多