如何用ant确定构建架构(32位/ 64位)?

HD.*_*HD. 18 java ant 64-bit build

我们继承了一个ant构建文件,但现在需要部署到32位和64位系统.

非Java位是用GNUMakefiles完成的,我们只需要调用"uname"来获取信息.是否有类似甚至更简单的方法来模仿蚂蚁?

pha*_*pus 12

晚到了派对,但到底是什么......

$ {os.arch}仅告诉您JVM是否为32/64位.您可能正在64位操作系统上运行32位JVM.试试这个:

<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>
Run Code Online (Sandbox Code Playgroud)

编辑: 正如@GreenieMeanie所指出的,这需要来自ant-contrib.sourceforge.net的ant-contrib库.

  • 请注意,使用上述语法需要通过http://ant-contrib.sourceforge.net/获取ant-contrib库. (4认同)

Ray*_*yek 10

你可以使用$ {os.arch}从ant 获取java系统属性(http://java.sun.com/javase/6/docs/api/java/lang/System.html#getProperties()).其他感兴趣的属性可能是os.name,os.version,sun.cpu.endian和sun.arch.data.model.

  • 小心 - $ {os.arch}只告诉你JVM的位,而不是平台.请参阅@ phatypus的回答. (7认同)

Lui*_*iro 7

这是一个有效的答案(我在Kubuntu 64,Debian 32,Windows 2000和Windows XP上测试过),而不需要外部或可选的ANT依赖.它基于@ phatypus的回答.

<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>
Run Code Online (Sandbox Code Playgroud)