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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/218989/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 11:35:04  来源:igfitidea点击:

How to determine build architecture (32bit / 64bit) with ant?

javaantbuild64-bit

提问by HD.

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

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

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?

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

采纳答案by Ray Tayek

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.

您可以使用 ${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。

回答by matt b

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

您可以将参数传递到构建文件中,并使用您想要的值。例如,如果您的目标是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 conditionsto set a default value for the property if it is not set).

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

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

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

回答by Vinko Vrsalovic

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

os.arch 运行得不是很好,另一种方法是询问 JVM,例如:

    ~$ 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.

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

回答by questzen

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.

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

回答by phatypus

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:

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

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

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

回答by mellis

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

顺便说一句,我为 64 位 Linux 获得的 os.arch(os 标签的 arch 属性)是 amd64。

回答by Luis Soeiro

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

这是一个有效的答案(我在 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>