如何使用Ant有条件地为不同平台执行批处理脚本?

时间:2012-09-07 18:48:09

标签: ant

我正在尝试使用Ant构建脚本来构建已经具有nmake(Visual Studio)构建脚本的项目。我想让Ant重用现有的脚本,而不是重做整个构建脚本。

所以,我有类似的东西适用于Windows Mobile 6 ARMV4I版本:

<project ...>
  <target name="-BUILD.XML-COMPILE" depends="-init, -pre-compile">
    <exec executable="cmd">
      <arg value="/c"/>
      <arg line='"${g.path.project}\build-wm6-armv4i.bat"'/>
    </exec>
    <antcall target="-post-compile" inheritall="true" inheritrefs="true" />
  </target>
</project>

但我也希望它适用于其他平台,如Win32 x86和Windows CE6 x86。

如何让Ant脚本区分它应该执行哪个批处理文件来执行构建?

1 个答案:

答案 0 :(得分:1)

<os>条件可用于根据操作系统和硬件体系结构设置属性。可以使用ifunless属性有条件地执行目标。

<?xml version="1.0" encoding="UTF-8"?>
<project name="build" basedir="." default="BUILD.XML-COMPILE">

  <condition property="Win32-x86">
    <and>
      <os family="windows" />
      <or>
        <os arch="i386" />
        <os arch="x86" />
      </or>
    </and>
  </condition>

  <condition property="Win-ARMv4">
    <os family="windows" arch="armv4" />
  </condition>


  <target name="-BUILD.XML-COMPILE_Win-ARMv4" if="Win-ARMv4" 
      depends="-init, -pre-compile">
    <exec executable="cmd">
      <arg value="/c"/>
      <arg line='"${g.path.project}\build-wm6-armv4i.bat"'/>
    </exec>
    <antcall target="-post-compile" inheritall="true" inheritrefs="true" />
  </target>

  <target name="-BUILD.XML-COMPILE_Win32-x86" if="Win32-x86"
      depends="-init, -pre-compile">
    <exec executable="cmd">
      <arg value="/c"/>
      <arg line='"${g.path.project}\build-win32-x86.bat"'/>
    </exec>
    <antcall target="-post-compile" inheritall="true" inheritrefs="true" />
  </target>

  <!-- Execute the correct target automatically based on current platform. -->
  <target name="BUILD.XML-COMPILE"
      depends="-BUILD.XML-COMPILE_Win-ARMv4,
               -BUILD.XML-COMPILE_Win32-x86" />
</project>

批处理文件的路径是单引号和双引号,因此带空格的文件路径不会破坏脚本。我没有在Windows Mobile 6 ARMV4I上测试过这个脚本,因此您需要使用下面的Ant目标来验证名称。

<target name="echo-os-name-arch-version">
  <echo message="OS Name is:         ${os.name}" />
  <echo message="OS Architecture is: ${os.arch}" />
  <echo message="OS Version is:      ${os.version}" />
</target> 

相关的堆栈溢出问题: