日构建的基本工具
日构建的工具有很多,但是最基础、最广泛的工具是Ant(http://ant.apache.org)。Ant类似于Make,但是加入了跨平台的特性。在这个目标的驱动下,Ant摒弃了Make工具的给予Shell的缺点,提供了一种使用XML配置文件的构建方式,并定义了一个统一的微核心和强大的扩展机制。这些特点使得Ant很快被人所接受、推广。目前,Ant的最新版本是1.6.0。
<project name="MyProject" default="dist" basedir="."> <description> simple example build file </description> <!-- set global properties for this build --> <property name="src" location="src"/> <property name="build" location="build"/> <property name="dist" location="dist"/> <target name="init"> <!-- Create the time stamp --> <tstamp/> <!-- Create the build directory structure used by compile --> <mkdir dir="${build}"/> </target> <target name="compile" depends="init" description="compile the source " > <!-- Compile the java code from ${src} into ${build} --> <javac srcdir="${src}" destdir="${build}"/> </target> <target name="dist" depends="compile" description="generate the distribution" > <!-- Create the distribution directory --> <mkdir dir="${dist}/lib"/> <!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file --> <jar jarfile="${dist}/lib/MyProject-${DSTAMP}.jar" basedir="${build}"/> </target> <target name="clean" description="clean up" > <!-- Delete the ${build} and ${dist} directory trees --> <delete dir="${build}"/> <delete dir="${dist}"/> </target> </project> |
以上是一个简单,但已经可以完全说明Ant工作流程的例子,来源于Ant的手册。在这个例子中,先定义了项目的基本信息和构建过程中所需要使用到的属性(1),然后初始化环境(2)(创建时戳和目标目录),在3和4中,对项目进行编译和打包,在5处,提供了清除项目输出的途径。
在Ant中,最关键的四个概念就是项目(Project)、目标(Target)、任务(Task)和属性(Property)。这四个概念的定义和调度、配置文件的处理构成了Ant的核心。而具体的任务则作为扩展机制。这种微核心的处理思路在很多成功的软件项目中采用过。
本文并没有打算对Ant进行全面的介绍,因此,如果你打算在组织中引入日构建,那么,学会使用Ant是必须的。目前很多的IDE环境都提供了对Ant的支持(例如Eclipse),所以使用Ant是很方便的。