18

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

非 Java 位使用 GNUMakefiles 完成,我们只需调用“uname”来获取信息。有没有类似甚至更简单的方法来用蚂蚁模仿这个?

4

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

于 2011-01-17T02:22:45.237 回答
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.

于 2008-10-20T16:35:40.450 回答
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>
于 2012-01-23T17:04:55.710 回答
3

您可以使用所需的值将参数传递到构建文件中。例如,如果您的目标是dist

ant -Dbuild.target=32 dist

或者

ant -Dbuild.target=64 dist

然后在您的 Ant 构建脚本中,根据${build.target}属性的值采取不同的操作(如果未设置,您也可以使用条件为属性设置默认值)。

或者,您可以检查内置 系统属性的值,例如${os.arch}.

于 2008-10-20T16:21:04.393 回答
1

os.arch 不太好用,另一种方法是询问JVM,例如:

    ~$ java -d32 测试
    2007 年 6 月 4 日星期一 07:05:00 CEST
    ~$ 回声 $?
    0
    ~$ java -d64 测试
    此平台不支持运行 64 位 JVM。
    ~$ 回声 $?
    1

那必须在脚本或包装器中。

于 2008-10-20T16:25:43.933 回答
1

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

于 2011-02-17T01:10:18.690 回答
0

假设您使用 ANT 构建 Java 应用程序,为什么需要知道它是 32 位架构还是 64 位?我们总是可以将参数传递给 ant 任务。一种更简洁的方法是在调用实际构建之前以编程方式发出 Ant 使用的系统属性文件。有这个有趣的帖子http://forums.sun.com/thread.jspa?threadID=5306174

于 2008-10-20T16:26:39.970 回答