我们继承了一个Ant构建文件,但现在需要部署到32位和64位系统。非Java部分是使用GNUMakefiles完成的,我们只需调用"uname"来获取信息。是否有类似或更简单的方法在Ant中模仿这个过程?
虽然来晚了,但还是说一下...
${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>
编辑: 正如 @GreenieMeanie 指出的那样,这需要从 ant-contrib.sourceforge.net 获取 ant-contrib 库。
您可以使用 ${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。
<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>
顺便说一下,我在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
这必须在脚本或包装器中实现。