PaulH
PaulH

Reputation: 7873

How to conditionally execute batch scripts for different platforms using Ant?

I am attempting to use an Ant build script to build a project that already has nmake (Visual Studio) build scripts. Rather than redo the entire build script, I would like to have Ant reuse the existing scripts.

So, I have something like this which works for Windows Mobile 6 ARMV4I builds:

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

But I would also like it to work for other platforms like Win32 x86 and Windows CE6 x86.

How can I have the Ant script discriminate which batch file it should execute to perform the build?

Upvotes: 1

Views: 1174

Answers (1)

Christopher Peisert
Christopher Peisert

Reputation: 24194

The <os> condition may be used to set properties based on the operating system and the hardware architecture. Targets may be conditionally executed using the if and unless attributes.

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

The paths to the batch files are both single and double quoted so that file paths with spaces will not break the script. I have not tested this script on Windows Mobile 6 ARMV4I, so you will want to use the Ant target below to verify the name.

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

Related stack overflow questions:

Upvotes: 1

Related Questions