Ant :DataType,antdatatype

來源:互聯網
上載者:User

Ant :DataType,antdatatype

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

 

Ant datatype

Ant中,除了Property可以做為Task執行時使用的值以外,Ant也提供了很多的資料類型。

 

 

 

下面就對這些資料類型做簡要的介紹:

 

 

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

 

<?xml version="1.0" encoding="UTF-8" ?>

<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可以對細化對檔案的選擇。

   

 

從也是可以看出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.xml
           bar.xml"/> 
 
<filelist 
    id="docfiles" 
    dir="${doc.src}">
    <file name="foo.xml"/>
    <file name="bar.xml"/>
</filelist>

 

 

Path

Path用於指定路徑,例如環境變數中的PATH、ClassPath。在定義path時,使用:或者;進行分隔。(備忘:寫build.xml時,可以使用:或者;。由Ant自動的根據作業系統轉為相應的分隔字元。)

<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代表一個Regex,可以指定id屬性,供其它地方(task或者selector等)使用。

Attribute

Description

Required

pattern

regular expression pattern

Yes

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.