如何用ant确定构建架构(32位/ 64位)?
题
我们继承了一个ant构建文件,但现在需要部署到32位和64位系统。
使用GNUMakefiles完成非Java位,我们只需要调用“uname”。获取信息。是否有类似甚至更简单的方法来模仿蚂蚁?
解决方案
你可以获得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是否为32 / 64bit。您可能正在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>
修改强> 正如@GreenieMeanie所指出的,这需要来自ant-contrib.sourceforge.net的ant-contrib库
这是一个有效的答案(我在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<*>quot;/>
</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>
BETWEEN,我为64位Linux获得的os.arch(os标签的arch属性)是amd64。
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
这必须是在脚本或包装器中。
假设您使用ANT构建Java应用程序,为什么需要知道它是32位arch还是64位?我们总是可以将参数传递给ant任务。更简洁的方法是在调用实际构建之前以编程方式发出Ant使用的系统属性文件。有一篇有趣的帖子 http://forums.sun.com/thread.jspa?threadID = 5306174 。