Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
ANT 南京大学软件学院 2009 1 The Who, What, When and Where • James Duncan Davidson • Open source, BSD style license application providing build functions for Java programs • Available since mid 2000 • http://ant.apache.org/ Why Ant? • More popular than 'make' for building Java projects • It runs on any platform that the Java projects it builds are on • Updated and improved regularly • Straightforward XML syntax • Plug-in oriented architecture encourages expansion • Directly supported by some IDEs (with more coming) • Free and open source • Fast, fast, and fast Getting Ant • Binary Distributions – http://ant.apache.org/bindownload.cgi • Source Distributions – http://ant.apache.org/srcdownload.cgi • Bundled in IDEs – All the main Java IDEs ship with Ant, products such as Eclipse, NetBeans System Requirements • For the current version of Ant, you will also need a JDK installed on your system, version 1.2 or later required, 1.5 or later strongly recommended. • The later the version of Java , the more Ant tasks you get. Installing Ant • The binary distribution of Ant consists of the following directory layout: ant +--- README, LICENSE, fetch.xml, other text files. +--- bin // contains launcher scripts | +--- lib // contains Ant jars plus necessary dependencies | +--- docs // contains documentation | | | +--- images // various logos for html documentation | | | +--- manual // Ant documentation | +--- etc // contains xsl goodies Installing Ant • Windows and OS/2 • Assume Ant is installed in c:\ant\. The following sets up the environment: – set ANT_HOME=c:\ant – set JAVA_HOME=c:\jdk-1.5.0.05 – set PATH=%PATH%;%ANT_HOME%\bin Installing Ant • Linux/Unix (bash) • Assume Ant is installed in /usr/local/ant. The following sets up the environment: – export ANT_HOME=/usr/local/ant – export JAVA_HOME=/usr/local/jdk1.5.0.05 – export PATH=${PATH}:${ANT_HOME}/bin Using Ant • 运行ant – 命令行模式下到buildfile所在目录 – ant [-buildfile/-f] [文件名] [目标] • 默认文件为build.xml • 如果需要指定buildfile,则加入-buildfile或f参数 • 查阅antdoc – ant使用帮助:安装目录 \docs\index.html – ant任务介绍 Command Line Options • ant [options] [target [target2 [target3] ...]] • Options: -help print this message -version print the version information and exit -quiet be extra quiet -verbose be extra verbose -debug print debugging information -emacs produce logging information without adornments -logfile file use given file for log output -logger classname the class that is to perform logging -listener classname add an instance of class as a project listener -buildfile file use specified buildfile -find file search for buildfile towards the root of thefilesystem and use the first one found -Dproperty=value set property to value -projecthelp print project help information Structure of Buildfile Buildfile文件 • 以XML文件来描述的 • 每个构建文件包含一个工程(project) • 每个工程包含若干个目标(target) • 目标可以依赖于其他的目标 (depends) • 目标包含任务(task) A simple example <?xml version="1.0"?> <project name=“myproject" default=“test“ basedir="." > <target name=“test" description=“ test ant."> <javac srcdir=“.”/> <echo message=“Ant is working properly”/> </target> </project> • project:每个构建文件以project为根节点 • name=“myproject” 为工程命名 • default=“test”:默认执行test目标 A simple example(2) <?xml version="1.0"?> <project name=“myproject" default=“test" basedir="." > <target name=“test" description=“ test ant."> <javac srcdir=“.”/> <echo message=“Ant is working properly”/> </target> </project> • JAVAC:用于编译JAVA源码 • ECHO:当构建抵达这里时,它将显示该 文本内容 Project • default:表示默认的运行目标,即指定默 认的target。这个属性是必须的 • basedir:the base directory from which all path calculations are done. This attribute might be overridden by setting the "basedir" property beforehand. If neither the attribute nor the property have been set, the parent directory of the buildfile will be used • name:表示项目名 – <project name=“first" default="init“ basedir=“.”> Target • Represent the fundamental tasks you want the build file to perform. • name表示目标名字,这个属性是必 须的 • depends表示依赖的目标,多个目标 用逗号分隔 • if表示仅当属性设置时才执行 • unless表示当属性没有设置时才执行 • description表示对这个目标的描述 Depends • 指定了target的执行顺序,被依赖的target 先执行 • 例: <target name=“run” depends=“compile”/> <target name=“compile” depends=“prepare”/> <target name=“prepare”> 执行顺序:preparecompilerun • <target name=“run” depends=“compile,prepare”/> 每个target 只执行一次! If & unless • If:只有此条件成立时才执行 <target name="build-module-A" if="module-Apresent"/> • Unless:只有此条件不成立时才执行 <target name="build-module-A" unless="module-A-present"/> • Note: Ant will only check whether the property has been set, the value doesn't matter. A property set to the empty string is still an existing property Property • 每个property可以有一个名字和一个 值 • property可用于task的属性值。这是 通过将属性名放在 "${" 和 "} " 之间 并放在属性值的位置来实现的 • 例: <property name="builddir" value="build"/> 一个task引用时,语法为${builddir},将 被解析为build Property (2) – Pulls all the properties from a file. The format of the file is the traditional "name=value" style that Java properties files have always used <property file="build.properties"/> – Demonstrates that properties can also be pulled from system environment variables on certain operating systems (not necessarily all though) <property environment="env"/> <echo message="ANT_HOME is set to = ${env.ANT_HOME}"/> Task • 一个task是一段可执行的代码 • 一个task可以有多个属性(变量)。 属性只可能包含对property的引用。 这些引用会在task执行前被解析 • 下面是Task的一般构造形式 <name attribute1="value1" attribute2="value2" ... /> Task • 核心任务。核心任务是Ant自带的任务 • 可选任务。可选任务实来自第三方的任 务,因此需要一个附加的JAR文件 • 用户自定义的任务。用户自定义的任务 实用户自己开发的任务 • 一个可选task一般需要额外的库才能工作。 这些外部库可以放到Ant的lib目录下,这 样Ant就能自动装入,或者将其放入环境 变量中 Core tasks • ..ant\docs\manual\CoreTasks 常用的task • File(Directory)相关 – – – – mkdir move copy delete • Java相关 – javac – java – jar • Others File(Directory)相关 • mkdir – 创建一个目录,如果他的父目录不存 在,也会被同时创建 – 它只有一个属性dir,而且是必须的 – 例子: <mkdir dir=“${builddir}/classes"/> – 说明:如果 ${builddir} 不存在,也会被 同时创建 File(Directory)相关 • move 用于文件或文件集的移动,其属性如下 – – – – – file 表示源文件。也可以用嵌套的<fileset> tofile 表示目标文件 todir 表示目标目录 overwrite 表示指定是否覆盖目标文件,默认值是覆盖 includeEmptyDirs 表示制定是否拷贝空目录,默认值为 yes – failonerror 表示指定如目标没有发现是否自动停止, 默认值是停止 – verbose 表示制定是否显示详细信息,默认值不显示 – 一个简单的fileset通常有三个属性: dir,includes,excludes Example • Move a single file (rename a file) <move file="file.orig" tofile="file.moved"/> • Move a single file to a directory <move file="file.orig" todir="dir/to/move/to"/> • Move a directory to a new directory <move todir="new/dir/to/move/to"> <fileset dir="src/dir"/> </move> or, since Ant 1.6.3: <move file="src/dir" tofile="new/dir/to/move/to"/> • Move a set of files to a new directory <move todir="some/new/dir"> <fileset dir="my/src/dir"> <include name="**/*.jar"/> <exclude name="**/ant.jar"/> </fileset> </move> File(Directory)相关 • copy 用于文件或文件集的拷贝,其属性 如下 – – – – file 表示源文件。也可以用嵌套的<fileset> tofile 表示目标文件 todir 表示目标目录 overwrite 表示指定是否覆盖目标文件,默认 值是不覆盖 – includeEmptyDirs 表示制定是否拷贝空目录, 默认值为拷贝 – failonerror 表示指定如目标没有发现是否自动 停止,默认值是停止 – verbose 表示制定是否显示详细信息,默认值 不显示 Example • Copy a single file <copy file="myfile.txt" tofile="mycopy.txt"/> • Copy a single file to a directory <copy file="myfile.txt" todir="../some/other/dir"/> • Copy a directory to another directory <copy todir="../new/dir"> <fileset dir="src_dir"/> </copy> • Copy all the files from one directory to another, skipping any java source files <copy todir="../dest/dir" > <fileset dir="src_dir"> <exclude name="**/*.java"/> </fileset> </copy> Or <copy todir="../dest/dir"> <fileset dir="src_dir" excludes="**/*.java"/> </copy> File(Directory)相关 • delete 用于删除一个文件或一组文 件,属性如下 – file 表示要删除的文件 – dir 表示要删除的目录 – includeEmptyDirs 表示指定是否要删除 空目录,默认值是删除 – failonerror 表示指定当碰到错误是否停 止,默认值是自动停止 – verbose 表示指定是否列出所删除的文 件,默认值为不列出 Example • 删除一个文件 <delete file="/lib/ant.jar"/> • 删除指定目录及其子目录 <delete dir="lib"/> • 删除指定的一组文件 <delete> <fileset dir="." includes="**/*.bak"/> </delete> Javac • Javac 用于编译一个或一组java文件,属 性如下 – – – – – – – – – srcdir 表示源程序的目录。 destdir 表示class文件的输出目录 includes 表示被编译的文件的模式 excludes 表示被排除的文件的模式 classpath 表示所使用的类路径 debug 表示包含的调试信息 optimize 表示是否使用优化 verbose 表示提供详细的输出信息 fileonerror 表示当碰到错误就自动停止 Examples • 例1 <javac srcdir=“${src} " destdir=“${build} " classpath="xyz.jar" debug="on"/> – 编译{src}目录及其子目录下的所有Java 文件,.Class文件将放在${build}指定的 目录下,classpath表示需要用到的类文 件或者目录,debug设置为on表示输出 debug信息 Examples • 例2 <javac srcdir=“${src}:${src2}“ destdir=”${build}" includes="mypackage/p1/**" excludes="mypackage/p1/testpackage/**" classpath="xyz.jar debug="on"/> – 编译${src}和${src2}目录及其子目录下的所有 Java文件, Class文件将放在${build}指定的目 录下。其中mypackage/p1/**将被编译,而 mypackage/p1/testpackage/**将不会被编译 Java • java 用来执行编译生成的.class文件,其 属性如下 – – – – – – – – classname 表示将执行的类名 jar 表示包含该类的JAR文件名 classpath 所表示用到的类路径 fork 表示在一个新的虚拟机中运行该类 failonerror 表示当出现错误时自动停止 output 表示输出文件 append 表示追加或者覆盖默认文件 执行的参数用嵌套的<arg> Example • Invokes a class named test.Main from within test.jar. Passes the argument -h to test.Main <java classname="test.Main"> <arg value="-h"/> <classpath> <pathelement location="\test.jar"/> <pathelement path="${java.class.path}"/> </classpath> </java> Jar • jar 用来生成一个JAR文件,属性如下 – – – – destfile 表示JAR文件名 basedir 表示被归档的文件名 includes 表示被归档的文件模式 excludes 表示被排除的文件模式 • 例: <jar destfile="${dist}/lib/app.jar" basedir="${build}/classes" includes="mypackage/test/**" excludes="**/Test.class"/> A Complete Example <project name="MyProject" default="dist" basedir="."> <!-- set global properties for this build --> <property name="src" value="."/> <property name="build" value="build"/> <property name="dist" value="dist"/> <target name="init"> <!-- Create the build directory structure used by compile --> <mkdir dir="${build}"/> </target> A Complete Example(contd. ) <target name="compile" depends="init"> <!-- Compile the java code from ${src} into ${build} --> <javac srcdir="${src}" destdir="${build}"/> </target> <target name="dist" depends="compile"> <!-- Create the distribution directory --> <mkdir dir="${dist}/lib"/> <!-- Put things in ${build} into MyProject-${DSTAMP}.jar --> <jar jarfile="${dist}/lib/MyProject-${DSTAMP}.jar" basedir="${build}"/> </target> <target name="clean"> <!-- Delete the ${build} and ${dist} directory trees --> <delete dir="${build}"/> <delete dir="${dist}"/> </target> </project> Others • zip 文件的 Ant 任务 <zip destfile="output.zip" basedir="output"/> • 创建 tar 文件 <gzip src="output.tar" zipfile="output.tar.gz"/> • 解压缩和提取文件 <unzip src="output.tar.gz" dest="extractDir"/> 时间戳生成 • tstamp任务一般在init目标中调用 最简单的格式<tstamp/> • 在调用 tstamp 任务之后,能够根据 日期命名该 JAR 文件,如下所示 <jar destfile="package-${DSTAMP}.jar" basedir="classes"/> • 如果这个任务在 2009年 4月 1 日调 用,该 JAR 文件将被命名为package20090401.jar 制作java文档——javadoc任务 • Generates code documentation using the javadoc tool • 示例 <target name="javadoc" description="Generates javadoc."> <javadoc destdir="${dir.doc}"> <fileset dir="${dir.src}"> <include name="**/*.java"/> <exclude name=“**/*Test.java”/> </fileset> </javadoc> </target> DataType • DataType可以很自然的处理构建过 程中的文件和路径问题 • 常见的DataType – DirSet 类型、 FileList类型、 FileSet类型、 FilterSet 类型、 Regexp类型 使用ant实例 • Eclipse中集成Ant实现快速开发 创建Eclipse工程 • File→New→Project,打开“New Project“对话框,选择Java Project项 目并点击Next • 输入工程名AntExample,并点击 Finish 添加Java代码 • 选择AntExample工程并且选择 “File→New→Class”以打开“New Java Class”对话框 • 然后,填写包名为cn.nju,新类的名 字为AntClass • 添加代码 public static void main(String args[]){ System.out.println(“Hello world!"); } 在Eclipse中编写Ant Build文 件 • AntExample工程并且选择 “New→File”。在“File Name”框中, 输入build.xml,并且点击Finish • 只要文件后缀是.xml的,Eclipse就把 认为是可能的Ant buildfile 编辑 Ant build文件 <?xml version="1.0"?> <project name="AntExample" default="all"> <description> test </description> <target name="all" depends="compile,compress" description="compile and compress"> <echo>Building the .jar file.</echo> </target> <target name="compile"> <javac srcdir="src/cn/nju" /> </target> <target name="compress"> <jar jarfile="Project.jar" basedir="src/cn/nju" includes="*.class" /> </target> </project> 运行ant的buildfile文件 • 方式一:在build.xml右键菜单上选 择Run As > Ant Build – 这个选择后直接会远行ant的buildfile文 件 • 方式二:在build.xml右键菜单上选 择Run As > Ant Build... – 会弹出对话框来设置运行的参数 执行结果 执行后生成文件 Eclipse中使用ant • 参见Eclipse帮助文档 Further Readings • antdocs:没什么比它更权威 • 《Ant极限编程》 – 【作者】 孟浩文 – 【出版社】 清华大学出版社 • 《使用Ant进行JAVA开发》 – 【原书名】 Java Development With Ant – 【作者】 Erik Hatcher,Steve Loughran – 【译者】 刘永丹 – 【出版社】 电子工业出版社 Maven • Maven is a software project management and comprehension tool. Based on the concept of a project object model (POM), Maven can manage a project's build, reporting and documentation from a central piece of information. • http://maven.apache.org/