Common Features
1, Initialized Project:
import java.io.File; import org.apache.tools.ant.Project; import org.apache.tools.ant.ProjectHelper;
public class InitializedProject extends Project { public InitializedProject(File buildFile) { super(); init(); ProjectHelper projectHelper = ProjectHelper.getProjectHelper(); addReference("ant.projectHelper", projectHelper); projectHelper.parse(this, buildFile); } } |
|---|
2, Share properties with plain java code
<property file="userhome/some.properties" />
3, Organize layered build files or refer tasks/targets/properties in other build file for large scale application
<import file="../../../../../build/imports.xml" />
4, id anywhere for reuse
<pathelement location="${test.dist.dir}" />
<pathelement location="${dist.dir}" />
</path>
5, method target anywhere for reuse
<someoperate />
</target>
6, synchronized call
<target name="xdoclet" depends="init, xdoclet.ejb, xdoclet.test" />
6, script ant
although ant is not a script language, it does glue many operations together, it can combine any "executable programs", such as "Java Class", "OS script", "OS executable program", and ant scripts itself. ant also provides many useful tasks, you can use it instead of batch file.
Special Features for Large Scale Software
1, access environment variable:
<property environment="env"/>
<echo message="Number of Processors = ${env.NUMBER_OF_PROCESSORS}"/>
<echo message="ANT_HOME is set to = ${env.ANT_HOME}"/>
2, access JVM variable(http://java.sun.com/j2se/1.5.0/docs/api/java/lang/System.html#getProperties()):
-
Key Description of Associated Value java.versionJava Runtime Environment version java.vendorJava Runtime Environment vendor java.vendor.urlJava vendor URL java.homeJava installation directory java.vm.specification.versionJava Virtual Machine specification version java.vm.specification.vendorJava Virtual Machine specification vendor java.vm.specification.nameJava Virtual Machine specification name java.vm.versionJava Virtual Machine implementation version java.vm.vendorJava Virtual Machine implementation vendor java.vm.nameJava Virtual Machine implementation name java.specification.versionJava Runtime Environment specification version java.specification.vendorJava Runtime Environment specification vendor java.specification.nameJava Runtime Environment specification name java.class.versionJava class format version number java.class.pathJava class path java.library.pathList of paths to search when loading libraries java.io.tmpdirDefault temp file path java.compilerName of JIT compiler to use java.ext.dirsPath of extension directory or directories os.nameOperating system name os.archOperating system architecture os.versionOperating system version file.separatorFile separator ("/" on UNIX) path.separatorPath separator (":" on UNIX) line.separatorLine separator ("/n" on UNIX) user.nameUser's account name user.homeUser's home directory user.dirUser's current working directory
3, property is immutable
for large scale software development, all source files include ant build files would be committed to SCM system, but many configuration items are not same in every machine, such as Application Server setup folder and listening port; Combining "environment variable, jvm variable, and immutable property", ant provides a perfect solution for this, you can commit default property files to SCM, and put custom property file in your ${user.home}, as long as your build file would load property files in ${user.home} first.
(to be continue...)
本文介绍了Ant在Java项目中的常见特性,如初始化项目、共享属性、组织分层构建文件等,还提到可通过id和方法目标实现复用,支持同步调用和脚本操作。此外,阐述了Ant在大型软件开发中的特殊特性,包括访问环境和JVM变量、使用不可变属性解决配置差异问题。
372

被折叠的 条评论
为什么被折叠?



