basedir=".">
$ ant -f diagnostic.xml现在,如果哪个方面出现了问题,你就可以在系统属性中找到问题的源头了,比如错误的Java版本或者类路径等等。
Buildfile: diagnostic.xml
systemProperties:
[echo] Java Runtime Environment version:
1.4.2_05
[echo] Java Runtime Environment vendor:
Apple Computer, Inc.
[echo] Java Runtime Environment vendor URL:
http://apple.com/
...
[echo] Default temp file path: /tmp
[echo] Operating system name: Mac OS X
[echo] Operating system architecture: ppc
[echo] Operating system version: 10.3.9
property="fop.available"/>
property="scriptsdir.exists"/>
unless="fop.available">
depends="files" unless="scriptsdir.exists">
import org.apache.tools.ant.*;
/**
JavaVersionTask is an Ant task for testing if
the installed Java version is greater than a
minimum required version.
**/
public class JavaVersionTask extends Task {
// Minimum required Java version.
private String minVersion;
// Installed Java version.
private String installedVersion;
// The name of the property that gets set when
// the installed Java version is ok.
private String propertyName;
/**
* Constructor of the JavaVersionTask class.
**/
public JavaVersionTask() {
super();
installedVersion = System.getProperty
("java.version");
}
/**
* Set the attribute minVersion.
**/
public void setMinVersion(String version) {
minVersion = version;
}
/**
Set the property name that the task sets when
the installed Java version is ok.
**/
public void setProperty(String propName) {
propertyName = propName;
}
/**
* Execute the task.
**/
public void execute() throws BuildException {
if (propertyName==null) {
throw new BuildException("No property name
set.");
} else if (minVersion==null) {
throw new BuildException("No minimum version
set.");
}
if(installedVersion.compareTo(minVersion)
>= 0) {
getProject().setProperty(propertyName,
"true");
}
}
}
classname="JavaVersionTask" classpath="."/>
property="javaversion.ok"/>
unless="javaversion.ok">
javaVersion: [echo] ERROR: Java version与JavaVersionTask相似,我们也可以写一个任务来执行其他的版本检查,例如已安装的库或者操作系统的版本。
too old: found 1.4.2_05, needs 1.5.
unless="binary.unchanged">
unless="config.unchanged">
checksum:
[echo] Verifying checksums of binary files...
[echo] Verifying checksum of configuration file...
binaryChanged:
configChanged:
all:
BUILD SUCCESSFUL
Total time: 1 second
checksum:
[echo] Verifying checksums of binary files...
[echo] Verifying checksum of configuration file...
binaryChanged:
configChanged:
[echo] WARNING: Configuration file changed.
all:
BUILD SUCCESSFUL
Total time: 1 second
depends="checksum" unless="config.unchanged">
addproperty="config.restore"/>
depends="configChanged" if="config.copy">
tofile="build/config.xml.1" overwrite="true"/>
overwrite="true"/>
将目标加入到目标all的依存关系中。如果配置文件被改变,Ant将询问用户是否希望备份该配置文件并且恢复为初始状态。如果用户按下了“Y”和回车键来选择“Yes”,属性config.copy将被设置,目标configRestore只有在这一属性被设置的情况下才会执行,它备份build/config.xml 为 build/config.xml.1,然后将原始的src/config.xml 拷贝为 build/config.xml。
结论
我们开发了一个Ant脚本来为Java应用软件执行诊断测试,该脚本检查已安装的Java版本是否符合最低要求,一些重要文件是否被改变,一个指定的Java类是否在类路径中,一个目录是否存在等等。在检查了软件的所有先决条件之后,脚本将结果报告给用户。该脚本甚至可以修复一些问题。另外,诊断脚本的输出也可以为技术支持人员所用,来快速地帮助用户,而不需要问上一大堆的问题。
资源
· 本文的源代码
· Apache Ant计划
· Apache Ant用户指南
· "Writing Ant Tasks"
Koen Vervloesem拥有计算机科学的硕士学位,从2000年开始成为IT自由撰稿人,主要为荷兰的IT期刊撰稿。
版权声明:任何获得Matrix授权的网站,转载时请务必保留以下作者信息和链接
原文:http://www.javaworld.com/
译文:http://www.matrix.org.cn/