18

We have inherited an ant build file but now need to deploy to both 32bit and 64bit systems.

The non-Java bits are done with GNUMakefiles where we just call "uname" to get the info. Is there a similar or even easier way to mimic this with ant?

7 Answers 7

12

Late to the party, but what the heck...

${os.arch} only tells you if the JVM is 32/64bit. You may be running the 32bit JVM on a 64bit OS. Try this:

<var name ="os.bitness" value ="unknown"/>
<if>
<os family="windows"/>
<then>
    <exec dir="." executable="cmd" outputproperty="command.ouput">
        <arg line="/c SET ProgramFiles(x86)"/>
    </exec>
    <if>
        <contains string="${command.ouput}" substring="Program Files (x86)"/>
        <then>
            <var name ="os.bitness" value ="64"/>
        </then>
        <else>
            <var name ="os.bitness" value ="32"/>
        </else>
    </if>
</then>
<elseif>
    <os family="unix"/>
    <then>
        <exec dir="." executable="/bin/sh" outputproperty="command.ouput">
        <arg line="/c uname -m"/>
        </exec>
        <if>
            <contains string="${command.ouput}" substring="_64"/>
            <then>
                <var name ="os.bitness" value ="64"/>
            </then>
            <else>
                <var name ="os.bitness" value ="32"/>
            </else>
        </if>
    </then>
</elseif>
</if>

<echo>OS bitness: ${os.bitness}</echo>

EDIT: As @GreenieMeanie pointed out, this requires the ant-contrib library from ant-contrib.sourceforge.net

5
  • I can't find an environment property that works across all versions of Windows. For instance, ProgramFiles(x86) doesn't exist under Windows 2000 or Windows XP... For Linux it worked great. Any other thoughts on that? Jan 23, 2012 at 13:43
  • Actually, if I'm reading his script correctly, he is relying on the fact that that the ProgramFiles(x86) environment variable does not exist on 32 bit Windows.
    – aDev
    Feb 14, 2012 at 17:13
  • @LuisSoeiro - the script worked for me on the OS's I was using at the time; I don't have instances of Windows 2000 or XP to test on for you now. If you are having problems with PROGRAMFILES(x86), does PROCESSOR_ARCHITECTURE exist on all the other MS OS's? You could always change the windows portion of the script so that it checks if the directory C:\Program Files (x86)\ exists or not -- if it exists you're running 64bit, if not you're running 32 bit.
    – phatypus
    Feb 28, 2012 at 5:41
  • 4
    Just a note that using the above syntax requires the ant-contrib library via ant-contrib.sourceforge.net Jun 25, 2013 at 18:54
  • thanks Greenie - because excuse me for being a pain - but this is utterly context-less and therefore useless - HOW COULD you run this? This isn't like anything I have ever seen looks like some sort of markup. If I need to know what ant architecture why do I need to have a grasp of "how to use ant" lots of down arrows please?
    – user26676
    Jan 2, 2014 at 12:52
10

you can get at the java system properties (http://java.sun.com/javase/6/docs/api/java/lang/System.html#getProperties()) from ant with ${os.arch}. other properties of interest might be os.name, os.version, sun.cpu.endian, and sun.arch.data.model.

2
  • Thanks, that sounds like the sanest approach. Will try that.
    – HD.
    Oct 21, 2008 at 12:15
  • 7
    Careful - ${os.arch} only tells you the bit-ness of the JVM, not the platform. See @phatypus's answer. Dec 18, 2011 at 14:24
7

Here is an answer that works (I tested on Kubuntu 64, Debian 32, Windows 2000 and Windows XP) without the need of external or optional ANT dependencies. It was based on @phatypus's answer.

<project name="FindArchitecture" default="check-architecture" basedir=".">

    <!-- Properties set: unix-like (if it is unix or linux), x64 (if it is 64-bits),
         register- size (32 or 64) -->
    <target name="check-architecture" depends="check-family,check-register" >
        <echo>Register size: ${register-size}</echo>
        <echo>OS Family: ${os-family}</echo>
    </target>

    <target name="check-family" >
        <condition property="os-family" value="unix" else="windows">
            <os family="unix" />
        </condition>

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

    <target name="check-register" depends="reg-unix,reg-windows">
    </target>

    <!-- Test under GNU/Linux -->
    <target name="reg-unix" if="unix">
        <exec dir="." executable="uname" outputproperty="result">
            <arg line="-m"/>
        </exec>

        <!-- String ends in 64 -->
        <condition property="x64">
            <matches string="${result}" pattern="^.*64$"/>
        </condition>

        <condition property="register-size" value="64" else="32">
            <isset property="x64"/>
        </condition>
    </target>

    <!-- Test under MS/Windows-->
    <target name="reg-windows" unless="unix">
        <!-- 64 bit Windows versions have the variable "ProgramFiles(x86)" -->
        <exec dir="." executable="cmd" outputproperty="result">
            <arg line="/c SET ProgramFiles(x86)"/>
        </exec>

    <!-- String ends in "Program Files (x86)" -->
        <condition property="x64">
            <matches string="${result}" pattern="^.*=.*Program Files \(x86\)"/>
        </condition>

        <condition property="register-size" value="64" else="32">
            <isset property="x64"/>
        </condition>
    </target> 
</project>
0
3

You can just pass a parameter into the build file with the value you want. For example, if your target is dist:

ant -Dbuild.target=32 dist

or

ant -Dbuild.target=64 dist

and then in your Ant build script, take different actions depending on the value of the ${build.target} property (you can also use conditions to set a default value for the property if it is not set).

Or, you can check the value of the built-in system properties, such as ${os.arch}.

1

os.arch does not work very well, another approach is asking the JVM, for example:

    ~$ java -d32 test
    Mon Jun 04 07:05:00 CEST 2007
    ~$ echo $?
    0
    ~$ java -d64 test
    Running a 64-bit JVM is not supported on this platform.
    ~$ echo $?
    1

That'd have to be in a script or a wrapper.

1
  • 1
    It worked for me under Linux but it doesn't seem to work under windows, though. Jan 23, 2012 at 13:41
1

BTW, the os.arch (arch property of the os tag) I got for 64-bit Linux was amd64.

0

Assuming you are using ANT for building Java Application, Why would you need to know if it is a 32 bit arch or 64-bit? We can always pass parameters to ant tasks. A cleaner way would be to programmaticaly emit the system properties file used by Ant before calling the actual build. There is this interesting post http://forums.sun.com/thread.jspa?threadID=5306174.

1
  • The thread link broke when Oracle took over Java forums. Can you find the thread and fix the link? Jun 24, 2011 at 17:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.