你的位置:首页 > 软件开发 > Java > Ant :DataType

Ant :DataType

发布时间:2016-04-20 16:00:25
DataTypepatternsetfilesetselectorfilelistpathregexp Ant datatypeAnt中,除了Property可以做为Task执行时使用的值以外,Ant也提供了很多的数据类型。 下面就对这些数据类型做简要的介绍: ...

Ant :DataType

  • DataType
    • patternset
    • fileset
    • selector
    • filelist
    • path
    • regexp

 

Ant datatype

Ant中,除了Property可以做为Task执行时使用的值以外,Ant也提供了很多的数据类型。

 Ant :DataType

 

 

下面就对这些数据类型做简要的介绍:

 

 

PatternSet

    PatternSet用于定义一个pattern集合,同时可以指定一个id属性,以供在其它地方引用它。Patterset可以定义在project下,也可以定义在target下。

 

使用patternset时,有两种方式:

·使用includes,includesfile,excludes,excludesfile属性

Attribute

Description

includes

指定要包括的文件名的pattern

includesfile

一个包括的文件的名称

excludes

指定要排除的文件名的pattern

excludesfile

一个要排除的文件名

多个pattern之间用以逗号或者空格作为分隔符。

 

·使用include,exclude|includesfile,excludesfile子元素

 

Include或者exclude元素有下列属性:

Attribute

Description

Required

name

the pattern to in/exclude.

Yes

if

Only use this pattern if the named property is set.

No

unless

Only use this pattern if the named property is not set.

No

 

<?

<project default="main">

 

    <!--define a ptattern set with includes attribute-->

    <patternset id="java_files_pattern" includes="**/*.java,**/*.class">

    </patternset>

 

   

    <!--define a ptattern set with include subelement-->

    <patternset id="java_files_pattern2">

       <include name="**/*.java"/>

       <include name="**/*.class"/>

    </patternset>

 

    <target name="main">

       <echo>java_files_pattern:</echo>

       <echo>${toString:java_files_pattern}</echo>

 

       <echo>java_files_pattern2</echo>

        <echo>${toString:java_files_pattern2}</echo>

    </target>

</project>

上面的代码段中使用了${toString:refid},Ant中有些数据类型(例如PatternSet)是支持toString方法的,使用${toString:refid}可以执行refid对应的toString方法。

测试结果如下:

main:

     [echo] java_files_pattern:

     [echo] patternSet{ includes: [**/*.java, **/*.class] excludes: [] }

     [echo] java_files_pattern2

     [echo] patternSet{ includes: [**/*.java, **/*.class] excludes: [] }

 

BUILD SUCCESSFUL

 

在实际的应用中,显式地使用PatternSet本身用的并不多见。因为FileSet隐式的包括了PatternSet,所以常见的用法都是在FileSet。另外所有隐含FileSet的数据类型,同样也等于隐含了PatternSet。

 

FileSet

    大多数构建过程中,都会操作文件集合,包括编译、复制、删除、打包等操作。这类构建流程中,非常重要,因此Ant提供了一种FileSet的Datatype。

    文件集是以一个单独的目录做为根目录的文件集合。默认情况下,由根目录指定的文件集合包含了整个目录树下的所有的文件,其中包括了所有子目录中的所有文件。

 

Attribute

Description

Required

dir

根目录

必须指定

两者之一

file

指定单一文件

defaultexcludes

参考patternset

No

includes

参考patternset

No

includesfile

参考patternset

No

excludes

参考patternset

No

excludesfile

参考patternset

No

casesensitive

是否大小写敏感。默认是true

No

followsymlinks

Shall symbolic links be followed? Defaults to true. See the note below.

No

erroronmissingdir

Specify what happens if the base directory does not exist.

If true a build error will happen, if false, the fileset will be ignored/empty.

 Defaults to true.

 Since Apache Ant 1.7.1 (default is true for backward compatibility reasons.)

No

 

在使用FileSet时,要么是只有一个文件,指定file属性即可。要么是多个文件,指定一个dir即可。

另外,可以在<fileset />中内嵌<patternset /> 和<slector />

 

下面是官方给出的例子:

使用<patternset />的子元素:

<fileset dir="${server.src}" casesensitive="yes">

  <include name="**/*.java"/>

  <exclude name="**/*Test*"/>

</fileset>

 

使用内嵌<patternset/>:
<fileset dir="${server.src}" casesensitive="yes">
  <patternset id="non.test.sources">
    <include name="**/*.java"/>
    <exclude name="**/*Test*"/>
  </patternset>
</fileset>

 

使用selector:
<fileset dir="${server.src}" casesensitive="yes">
  <filename name="**/*.java"/>
  <not>
    <filename name="**/*Test*"/>
  </not>
</fileset>

 

 

Selector

Patterset 是根据文件名进行匹配的,有时你想要删除过期的文件或者向远程站点上传发生变化的文件。你想用什么办法删除文件而保留目录呢?selector可以对细化对文件的选择。

   Ant :DataType

 

从上图也是可以看出selector分为两类:常用的选择器、选择器容器。

选择器容器中,可以有多个选择器。

 

常用选择器:

  • <contains> - Select files that contain a particular text string
  • <date> - Select files that have been modified either before or after a particular date and time
  • <depend> - Select files that have been modified more recently than equivalent files elsewhere
  • <depth> - Select files that appear so many directories down in a directory tree
  • <different> - Select files that are different from those elsewhere
  • <filename> - Select files whose name matches a particular pattern. Equivalent to the include and exclude elements of a patternset.
  • <present> - Select files that either do or do not exist in some other location
  • <containsregexp> - Select files that match a regular expression
  • <size> - Select files that are larger or smaller than a particular number of bytes.
  • <type> - Select files that are either regular files or directories.
  • <modified> - Select files if the return value of the configured algorithm is different from that stored in a cache.
  • <signedselector> - Select files if they are signed, and optionally if they have a signature of a certain name.
  • <scriptselector> - Use a BSF or JSR 223 scripting language to create your own selector
  • <readable> - Select files if they are readable.
  • <writable> - Select files if they are writable.

 

 

常用选择器容器:

  • <and>
  • <contains>
  • <custom>
  • <date>
  • <depend>
  • <depth>
  • <filename>
  • <majority>
  • <none>
  • <not>
  • <or>
  • <present>
  • <selector>
  • <size>

 

 

有关selector的使用,可以参考官方文档:

http://ant.apache.org/manual/Types/selectors.html

 

 

FileList

    FileList 是一个List,是一个有序集合。如果需要使用有序文件集合时,可以使用这个。

Attribute

Description

Required

dir

根目录

Yes

files

文件列表,使用空格或者逗号分隔

如果没有内嵌<file />,

就必须指定这个属性。

 

 

<filelist 
    id="docfiles" 
    dir="${doc.src}"
    files="foo.
           bar.
 
<filelist 
    id="docfiles" 
    dir="${doc.src}">
    <file name="foo.
    <file name="bar.
</filelist>

 

 

Path

Path用于指定路径,例如环境变量中的PATH、ClassPath。在定义path时,使用:或者;进行分隔。(备注:写build.

<classpath> 与<path />的方法是一样的。<path />下可以有<pathelement />以及其它的资源集合(例如:fileset,filelist,dirset,path等)

 

<pathelement> 使用说明

 

Pathelement可以指定两种属性:

    ·location 用于指定一个文件或者目录。可以是相对路径,也可以是绝对路径。如果是相对路径,则是相对于project的basedir。

    ·path 由,或者;分隔的多个location。

<classpath>
      <pathelement path="${classpath}"/>
      <pathelement location="lib/helper.jar"/>
    </classpath>

 

 

<classpath>
      <pathelement path="${classpath}"/>
      <fileset dir="lib">
        <include name="**/*.jar"/>
      </fileset>
      <pathelement location="classes"/>
      <dirset dir="${build.dir}">
        <include name="apps/**/classes"/>
        <exclude name="apps/**/*Test*"/>
      </dirset>
      <filelist refid="third-party_jars"/>
    </classpath>

 

 

每个path,classpath也有2个属性:id,refid。

id用于被其它地方使用refid引用。

<project ... >
  <path id="project.class.path">
    <pathelement location="lib/"/>
    <pathelement path="${java.class.path}/"/>
    <pathelement path="${additional.path}"/>
  </path>
 
  <target ... >
    <rmic ...>
      <classpath refid="project.class.path"/>
    </rmic>
  </target>
 
  <target ... >
    <javac ...>
      <classpath refid="project.class.path"/>
    </javac>
  </target>
</project>

 

 

 

 

 

Regexp

Regexp代表一个正则表达式,可以指定id属性,供其它地方(task或者selector等)使用。

Attribute

Description

Required

pattern

regular expression pattern

Yes


原标题:Ant :DataType

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

可能感兴趣文章

我的浏览记录