Ant核心类型: 利用Ant工具的核心类型能方便地定义变量,并可在target中进行引用。 所有Ant工具的类型都有id和refid两个属性,id作为这个类型的唯一标识,refid只用指定要引用的类型。 -------------------------------------------------------------------- 1.Assertions Type:断言类型 断言类型只有enableSystemAssertions一个属性,表示是否允许系统断言,默认为unspecified,代表没有指定 <assertions enableSystemAssertions="true"> <enable package="org.apache"/> <disable package="org.apache.tools.ant"/> <enable class="org.apache.tools.ant.Main"/> </assertions> 这里允许系统断言,在Java包前缀为org.apache下的Java class允许执行java断言。 同时限制org.apache.tools.ant前缀的Java包下的类不能执行断言,但是org.apache.tools.ant.Main这个Java类指定为允许执行断言。 -------------------------------------------------------------------- 2.Description Type:描述类型 <description></description> 对构建文件或target进行说明 -------------------------------------------------------------------- 3.PatternSet Type:定义模式集合(可在FileSet和DirSet中引用) 用以定义文件或目录集合 <patternset id="sources"> <include name="std/**/*.java"/> <include name="prof/**/*.java" if="professional"/> <exclude name="**/*Test*"/> </patternset> 如果属性professional在构件文件中设定了,那么在PatternSet类型中将包含子目录prof下的所有java文件 <patternset includesfile="some-file"/> <!--//与下面的patternset定义作用一样--> <patternset> <includesfile name="some-file"/> </patternset> 文件名包含some-file的文件 -------------------------------------------------------------------- 4.DirSet Type:定义目录集合 <dirset dir="${build.dir}"> <include name="apps/**/classes"/> <exclude name="apps/**/*Test*"/> </dirset> 定义一个目录集合,这个目录集合的根目录为${build.dir},在这个目录集合中将包含所有目录名为classes且名称中不包含有Test字符的子目录 <patternset id="non.test.classes"> <include name="apps/**/classes"/> <exclude name="aps/**/*Test*"/> </patternset> <dirset id="${build.dir}"> <patternset refid="non.test.classes"/> </dirset> 在目录集合中通过PatternSet类型来指定目录集合 -------------------------------------------------------------------- 5.FileSet Type:定义文件集合 selector类型可看作是FileSet类型中的一个元素,用以对文件进行筛选 (1).contains selector 选择包含text属性中指定的字符串的文件 <fileset dir="${doc.path}" includes="**/*.html"> <contains text="script" casesensitive="no"/> </fileset> 选择${doc.path}下所有包含script字符串的html文件 (2).date selector 选择文件的修改时间在某个特定的时间之前或之后的文件 <fileset dir="${jar.path}" includes="**/*.jar"> <date datetime="01/01/2001 12:00 AM" when="before"/> </fileset> 选择${jar.path}下所有修改时间在01/01/2001 12:00 AM之前的文件 (3).depend selector 比较两个不同目录下名称相同的文件,然后选择文件修改时间迟的那个文件 <fileset dir="${ant.1.5}/src/main" includes="**/*.java"> <depend targetdir="${ant.1.4.1}/src/main"/> </fileset> 选择相对于Ant1.4版本在Ant1.5版本中修改过的Java源文件 (4).depth selector 指定选择文件的文件目录的深度 <fileset dir="${doc.path}" includes="**/*"> <depath max="1"/> </fileset> 选择根目录及根目录下的第一层子目录的所有文件 (5).different selector 从两个目录中选择被认为不同的文件 属性ignoreFileTimes默认为true,表示忽略文件修改时间 <fileset dir="${ant.1.5}" includes="**/*.java"> <different targetdir="${ant.1.4.1}/src/main" ignoreFileTimes="false"/> </fileset> 比较Ant工具1.4和1.5版本的Java源文件,从中选择不相同的文件,选择过程不忽略文件的修改时间 (6).filename selector 选择符合指定文件匹配模式的文件,通常为文件名匹配。这个selector的作用与PatternSet的include和exclude相似 negate="true"反效果,相当于exclude <fileset dir="${doc.path}" includes="**/*"> <filename name="**/*.css"/> </fileset> 选择${doc.path}下所有的css样式文件 (7).present selector 从FileSet的指定的目录中选择在present的targetdir指定的目录中不存在或者两个目录中都存在的文件 present="srconly"代表只从FileSet目录中选取targetdir指定目录中不存在的文件 present="both"代表从FileSet目录和targetdir目录中选择两个目录都存在的文件 <fileset dir="${ant.1.5}/src/main" includes="**/*.java"> <present present="srconly" targetdir="${ant.1.4.1}/src/main"/> </fileset> 从Ant1.5的src/main包中选择在ant1.4的src.main包中不存在的java源文件 (8).containsregexp selector 选择符合正则表达式的文件 <fileset dir="${doc.path}" includes="*.txt"> <containsregexp expression="[4-6]/.[0-9]"/> </fileset> 从${doc.path}目录中选择文件名包含4、5或6后面跟着一个"."再跟着一个0-9的数字的txt文件 (9).size selector 通过文件的大小对文件进行选择 <fileset dir="${jar.path}"> <patternset> <include name="**/*.jar"/> </patternset> <size value="" units="Ki" when="more"/> </fileset> 或 <fileset dir="${jar.path}" includes="**/*.jar"> <size value="" units="Ki" when="more"/> </fileset> 选择在${jar.path}目录下所有大于4096bytes的jar文件 (10).type selector 用于指定要选择目录还是文件 type="dir"选择目录 type="file"选择文件 <fileset dir="${src}"> <type type="dir"/> </fileset> 选择${src}目录下的所有子目录 用FileSet类型内置的include,exclude属性来定义文件集合 <fileset dir="${server.src}" casesensitive="yes"> <include name="**/*.java"/> <exclude name="**/*Test*"/> </fileset> 可用selector类型实现相同的效果 <fileset dir="${server.src}" casesensitive="yes"> <filename name="**/*.java"/> <filename name="**/*Test*" negate="true"/> </fileset> 这个文件集合包含根目录${server.src}下的所有java文件,但不包含文件名称中含有Test字符的文件。 用PatternSet Type类定义文件集合的实例 <fileset dir="${server.src}" casesensitive="yes"> <patternset id="non.test.sources"> <include name="**/*.java"/> <exclude name="**/*Test*"/> </patternset> </fileset> 如果在构件文件中已定义了PatternSet类型,那么可以通过PatternSet的引用以实现相同的功能 <fileset dir="${server.src}" casesensitive="yes"> <patternset refid="non.test.sources"/> </fileset> -------------------------------------------------------------------- 6.FileList Type:定义文件列表 FileList可以包含不存在的文件,而FileSet类型指定的文件一定要存在 <filelist dir="${doc.src}" files="foo.xml,bar.xml"/> 等同于 <filelist dir="${doc.src}"> <file name="foo.xml"/> <file name="bar.xml"/> </filelist> -------------------------------------------------------------------- 7.Filter Type:文件过滤器定义 定义一组文件过滤器,在移动或复制文件时对文件内容进行替换 begintoken属性定义一个特殊字符用于指定要过滤的字符串的开始位置,默认为@ endtoken属性定义一个特殊字符用于指定要过滤的字符串的结束位置,默认为@ 在FilterSet类型中可以直接包含Filter类型,用以定义具体的文件过滤器 <target name="tokenFilterDemo" depends="prepare"> <tstamp> <format property="now" pattern="MMMM d yyyy hh:mm aa"/> </tstamp> <copy todir="build" filtering="true"> <fileset dir="src"> <include name="**/*.java"/> </fileset> <filterset> <filter token="BUILD_DATE" value="${now}"/> <filter token="COPYRIGHT" value="Copyright(C)2002 O'Reily"/> </filterset> </copy> </target> 用于替换所有java文件中的版权信息和创建时间 自定义替换符的文件过滤器实例 <copy file="${build.dir}/version.txt" toFile="${dist.dir}/version.txt"> <filterset begintoken="%" endtoken="!"> </filter token="DATE" value="${TODAY}"> </filterset> </copy> 等同于 <filterset id="myFilterSet" begintoken="%" endtoken="!"> </filter token="DATE" value="${TODAY}"> </filterset> <copy file="${build.dir}/version.txt" toFile="${dist.dir}/version.txt"> <filterset refid="myFilterSet"/> </copy> 当复制version.txt文件时替换%DATE!为当前日期 在FilterSet类型中可以直接包含FiltersFile类型,用以指定一个Java属性文件来替换 替换前src.txt的内容: This is the test sample. the message will be replaced:#MESSAGE! abc.properties文件中的内容: MESSAGE=HELLO 构件文件的内容: <project name="project1" default="target1"> <target name="target1"> <copy toDir="afterReplace"> <fileset dir="."> <include name="src.txt"/> </fileset> <filterset begintoken="#" endtoken="!"> <filtersfile file="abc.properties"/> </filterset> </copy> </target> </project> 把src.txt文件复制到afterReplace目录下,同时使用abc.properties中的内容去替换#MESSAGE!。 替换后在afterReplace目录下的src.txt文件的内容如下: This is the test sample. the message will be replace:HELLO -------------------------------------------------------------------- 8.PropertySet Type:定义属性集合 在PropertySet中可以通过Propertyref元素引用构件文件中的其他property, 而PropertySet类型之间可以互相引用。 <property name="lib1" value="lib1path"/> <property name="lib2" value="lib2path"/> <propertyset id="projectset1"> <projectref name="lib1"/> <projectref name="lib2"/> </propertyset> 定义一个属性集合,这个集合包含名字为lib1和bib2两个属性 <propertyset id="properties-starting-with-foo"> <propertyref prefix="foo" negate="true"/> </propertyset> <propertyset id="properties-starting-with-bar"> <propertyref prefix="bar" negate="true"/> </propertyset> <propertyset id="my-set"> <propertyset refid="properties-starting-with-foo"/> <propertyset refid="properties-starting-with-bar"/> </propertyset> 定义一个属性集合,这个集合包含非foo、bar开头的属性 -------------------------------------------------------------------- 9.File Mapper Type:文件映射类型(定义文件间的相互关系) 用type属性指定实现类型,可实现的类型如下 (1).identity:指定源文件与目标文件具有相同的名称 (2).flatten:指定源文件与目标文件的名称相同,但忽略源文件中的文件目录,仅包含文件 (3).glob:源文件和目标文件的匹配模式中都包含*的匹配符 (4).merge:用于把打包的源文件进行合并。目标文件名称与源文件相同 (5).regexp:用正则表达式匹配 (6).package:与glob相似,模式中包含*的匹配符。package可用于替换目录名称 (7).composite:用于组合多个Mapper类型,每个Mapper类型都对源文件进行操作,最后返回所有操作后的结果 (8).chained:可包括多个Mapper类型,源文件依次经过每个Mapper,上一个Mapper的输出作为下一个Mapper的输入 (9).filtermapper:用于对文件名进行过滤 用法如<mapper type="identity"/>或<identitymapper/>, 其中chained实现类不能用type属性指定。 -------------------------------------------------------------------- 10.ZipFileSet Type:zip格式的文件集 通过Ant工具内建的zip任务实现打包zip文件的功能,同时可以在zip任务中引用ZipFileSet类型指定文件 <zip destfile="${dist}/manual.zip"> <zipfileset dir="htdocs/manual" prefix="docs/user-guide"/> <zipfileset dir="." includes="ChangeLog27.txt" fullpath="docs/ChangeLog.txt"/> <zipfileset src="example.zip" mce_src="example.zip" includes="**/*.html" prefix="docs/examples"/> </zip> 以zip文件的形式打包htdocs/manual目录下的所有文件到docs/user-guide目录下。 同时添加ChangeLog27.txt到这个zip文件的docs目录下。 在这个zip文件中同时包含example.zip文件,example.zip文件包含docs/examples目录及其子目录下的所有html文件。 -------------------------------------------------------------------- 11.FilterChains和FilterReaders:过滤链和过滤读取器 在Ant工具的内建任务:Concat、Copy、LoadFile、LoadProperties和Move中可以直接使用FilterChain进行过滤 <copy file="${src.file}" tofile="${dest.file}"> <filterchain> <filterreader classname="your.extension.of.java.io.FilterReader"> <param name="foo" value="bar"/> </filterreader> <filterreader classname="another.extension.of.java.io.FilterReader"> <!-- 指定过滤器依赖的类库 --> <classpath> <pathelement path="${classpath}"/> </classpath> <param name="blah" value="blee"/> <param type="abra" value="cadabra"/> </filterreader> </filterchain> </copy>