Ant开发二(只求最好)
看后要回帖的(绝对经典)
首先先介绍一下ant的工程标签
<project name="" default="" basedir="" >
??? ................
</project>
?
上面的就是ant的工程标签了
中name是工程的属性名,当ant开始时,会显示工程名,default标签是默认运行的target(任务),就是ant运行这个build时,会默认的找到那个target,然后执行它。 basedir标签一般都是"." 也就是查找project时的基本路径用”.“查找
下面介绍project内部的标签和属性:
1.数据元素(英文翻译)DataElement
特性:property 和DataType类型(很像全局变量和局部变量,大家都能用到,我是这么理解的)
举个很简单的小例子:
<?xml version="1.0" encoding="UTF-8"?><project basedir="." default="prepare" name="Testproperty"> <property name="prop1" value="It is my first property" /><property name="prop2" value="${prop1}+It is my second property" /> <property name="prop3" value="${prop2}: It is my third property" /> <property file="conf/log4j.properties" /> <property name="build.dir" value="build" /> <property name="lib.dir" value="${build.dir}/lib" /> <!-- The DataType value--> <path id="classpath"><fileset dir="${lib.dir}"><include name="**/*.jar" /></fileset></path> <!-- Create a file of build--> <target name="prepare"><mkdir dir="${build.dir}" /></target></project>?下面分析一下:
project 标签就不说了
1.property标签有3个属性:
name------------------------property的名字value------------------------property的值file---------------------------显示的导入文件(同java的System.setproperty()方法)2.DataType是定义的属性
像path ,fileset ,src 都是DataType类型,指定的类型。。
说明下path的作用:
是将build/lib包下的所有jar文件全部包含进来。。。。。
3.Target标签了,也是project下用的最多,也是最重要的 。。。project标签必须有个默认的target,上面的target的作用是,在build.xml目录下创建一个build目录,就是顶级目录下创建build目录。。
?
很简单的例子,但很能说明一个问题:DataType包含property,有些复杂的property可以很简单的用DataType解决
很强大。。
下面我发个比较复杂的例子,然后具体分析下target的标签
?
<?xml version="1.0" encoding="UTF-8"?><!-- Comment are just as important in build files ,do not avoid writing them! --><!-- Example build file for "Ant : The Definitive Guide" and its sample project: AntDemo3" --><project name="AntDemo3" default="all" basedir="."><!-- Project-wide setting .All directories are relative to the project root directory --><!-- Project directory --><property name="src.dir" value="src" /><property name="doc.dir" value="src" /><property name="dist.dir" value="src" /><property name="lib.dir" value="src" /><property name="bin.dir" value="src" /><!-- Temporary build directory names --><property name="build.dir" value="build" /><property name="build.classes" value="${build.dir}/classes" /><property name="build.doc" value="${build.dir}/doc" /><property name="build.lib" value="${build.dir}/lib" /><!-- Global setting --><property name="javac.debug" value="on" /><property name="debug.flag" value="on" /><property name="java.lib" value="${java.home}/jre/lib/rt.jar" /><!-- Global "magic" property for <javac> --><property name="build.compiler" value="modern" /><!-- DataType for lib of classpath --><path id="classpath"><fileset dir="${lib.dir}"><include name="**/*.jar" /></fileset></path><!-- Target to create the build directories prior to a compile target --><!-- We also mark the start time of the build ,for the log. --><target name="prepare"><mkdir dir="${build.dir}" /><mkdir dir="${build.lib}" /><mkdir dir="${build.classes}" /><mkdir dir="${build.classes}/modules" /><tstamp /><echo message="${TSTAMP}" /></target><!-- Build the application --><target name="application" depends="prepare"><!-- Compile the application classes,not the module classes --><javac destdir="${build.classes}" debug="${debug.flag}"deprecation="on"><!-- We could have used javac's srcdir attribute --><src path="${src.dir}" /><exclude name="AntDemo3/modules/**" /><classpath refid="classpath" /></javac><!-- Package the application into a JAR --><jar jarfile="${build.lib}/ AntDemo3.jar " basedir="${build.classes}"><exclude name="AntDemo3/modules/**" /></jar></target><!-- Build the modules --><target name="modules" depends="prepare,application"><!-- Compile just the module classes --><javac destdir="${build.classes}/modules" debug="${debug.flag}"deprecation="on"><!-- We could have used javac's srcdir attribute --><src path="${src.dir}" /><exclude name="AntDemo3/modules/**" /><classpath refid="classpath" /></javac><!-- Package the modules as a JAR --><jar jarfile="${build.lib}/ modules.jar " basedir="${build.classes}"><exclude name="AntDemo3/modules/**" /></jar></target><!-- Do all --><target name="all" depends="modules,application" /><!-- Generate the API documentation AntDemo3 and the modules --><target name="javadoc" depends="application" ><mkdir dir="${doc.dir}/api"></mkdir><javadoc packagenames="AntDemo3.*" sourcepath="${src.dir}" destdir="${doc.dir}/api" author="true" version="true" use="true" ><classpath refid="classpath" /> </javadoc></target><!-- Delete class files build during previous builds.leave directories --><target name="clean"><delete><fileset dir="${build.classes}" includes="**/*.class" /></delete></target><!-- Delete any created directories and their contents --><target name="cleanall" depends="clean" ><delete dir="${build.dir}" /><delete dir="${dist.dir}" /><delete dir="${doc.dir}/api" /></target><!-- Deploy the application in a "ready-to-run" state --><target name="deplpy" depends="application,javadoc" ><!-- Create the distribution directory --><mkdir dir="${dist.dir}" /><mkdir dir="${dist.dir}/bin" /><mkdir dir="${dist.dir}/lib" /><mkdir dir="${dist.dir}/doc" /><mkdir dir="${dist.dir}/config" /><!-- Copy the primary program and modules --><copy todir="${dist.dir}/lib" ><fileset dir="${build.classes}" /><fileset dir="${build.lib}" includes="AntDemo3.jar" /><fileset dir="${build.lib}" includes="modules.jar" /><fileset dir="${lib.dir}" includes="*.jar" /></copy><!-- Copy the documentation --><copy todir="${dist.dir}/doc" ><fileset dir="${doc.dir}" /></copy><!-- Copy the pre-fab configuration files --><copy todir="${dist.dir}/config"><fileset dir="${lib.dir}" includes="*.xml" /></copy><!-- Copy the running scripts --><copy todir="${dist.dir}/bin" ><fileset dir="${bin.dir}" includes="*.script" /><fileset dir="${bin.dir}" includes="*.bat" /></copy></target></project>?以上很多但很简单。。。希望能仔细阅读
?
分析:
?
首先分析下ant的错误级别:
看懂了错误才能做修正。。。。嘿嘿
?
1.工程级错误,也就是project标签内的错误,ant遇到错误会立即停止,如果你其中一个target写错了,那么ant会认为这次的执行失败,会导致不可预料的错误和时间资源的浪费。。
2.目标级错误,也就是target标签内的错误,ant遇到不会停止任务,只会报错,也很好解决。。。
还有一点要说明一下:
ant是按顺序执行的,如果你要颠倒顺序的话,有些变量是不能识别的。。注意
?
说说一些标签的用处:
src (DataType) == srcdir属性
destdir----------------------编译后class类的放置目录
excludes--------------------不包含name的东西(可能是目录和文件)
refid--------------------------找寻上面定义的id
debug------------------------是否debug(值是boolean类型)
deprecation-----------------是否用到过期类(值是Boolean类型)
echo--------------------------给log输出信息
jarfile-------------------------打包后jar的位置目录
basedir----------------------目标目录
todir--------------------------目标目录
delete------------------------删除
failonerror-------------------Ant将一个失败的copy也看做是成功的。(属性)
?
上面初步的介绍了ant的标签和属性,其实到这,ant你也能写东西了,如果还想研究的话。。请看ant开发3。。。。。。