Ant高级-path和fileset

<path/> 和 <classpath/>

你可以用":"和";"作为分隔符,指定类似PATH和CLASSPATH的引用。Ant会把分隔符转换为当前系统所用的分隔符。

 

当需要指定类似路径的值时,可以使用嵌套元素。一般的形式是

<classpath>

<pathelement path="{classpath}"/>

<pathelement location="lib/helper.jar"/>

</classpath>

location属性指定了相对于project基目录的一个文件和目录,而path属性接受逗号或分号分隔的一个位置列表。path属性一般用作预定义的路径,其他情况下,应该用多个location属性。

为简洁起见,classpath标签支持自己的path和location属性。所以:

<classpath>

<pathelement path="{classpath}"/>

</classpath>

可以被简写作:

<classpath path="{classpath}"/>

也可通过<fileset>元素指定路径。构成一个fileset的多个文件加入path-like structure的顺序是未定的。

<classpath>

<pathelement path="{classpath}"/>

<fileset dir="lib">

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

</fileset>

<pathelement location="classes"/>

</classpath>

上面的例子构造了一个路径值包括:{classpath}的路径,跟着lib目录下的所有jar文件,接着是classes目录。

 

如果你想在多个task中使用相同的path-like structure,你可以用<path>元素定义他们(与target同级),然后通过id属性引用。

path-like structure可能包括对另一个path-like structurede的引用(通过嵌套<path>元素):

<path id="base.path">

<pathelement path="{classpath}"/>

<fileset dir="lib">

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

</fileset>

<pathelement location="classes"/>

</path>

<path id="tests.path">

<path refid="base.path"/>

<pathelement location="testclasses"/>

</path>

前面所提的关于<classpath>的简洁写法对于<path>也是有效的,如:

<path id="tests.path">

<path refid="base.path"/>

<pathelement location="testclasses"/>

</path>

可写成:

<path id="base.path" path="{classpath}"/>

 

二 fileset

1)FileSet是一组文件,这些文件可以在基目录树下找到且与指定的PatternSets和Selectors匹配。FileSet的结构类似于如下:

<fileset dir="${server.src}">
<patternset/>
<Selector/>
</fileset>

 

2)patternset一般用作FileSet的子元素,用来帮助筛选文件。可以包含如下的子元素:include,exclude,includes,excludes,includesfile,excludesfile.

FileSet中隐式地包含了一个patternset元素,所以可以在FileSet中直接包含patterset中的元素,例如<include>, <includesfile>, <exclude> and <excludesfile> 。

如下的patternset包含std子目录下的java文件,如果professional定义的话还包含prof下的java文件,但是不包含名字中包含Test的文件。
<patternset id="sources">
<include name="std/**/*.java"/>
<include name="prof/**/*.java" if="professional"/>
<exclude name="**/*Test*"/>
</patternset>

 

3)Selector一般用作FileSet的子元素,用来帮助筛选文件。

常用的核心的selector有:
<contains> - 用来选择包含指定字符串的文件
<date> - 用来选择在某个特定时间前或后修改的文件
<depend> - Select files that have been modified more recently than equivalent files elsewhere
<depth> - 用来选择指定目录深度的文件
<different> - Select files that are different from those elsewhere
<filename> - 用来选择文件名匹配特定模式的文件。等价于include和exclude的patternset。
<present> - 用来选择在某个位置存在或不存在的文件
<containsregexp> - 用来选择匹配指定正则表达式的文件
<size> - 用来选择比指定size大或小的文件
<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> - 选择有readable属性的文件
<writable> - 选择有writable属性的文件

例如选择包含script的所有的html文件
<fileset dir="${doc.path}" includes="**/*.html">
<contains text="script" casesensitive="no"/>
</fileset>

例如选择所有在January1,2001前修改的JAR文件
<fileset dir="${jar.path}" includes="**/*.jar">
<date datetime="01/01/2001 12:00 AM" when="before"/>
</fileset>

例如选择所有满足正则表达式的txt文件
<fileset dir="${doc.path}" includes="*.txt">
<containsregexp expression="[4-6]\.[0-9]"/>
</fileset>

如下的selector与patternset等价:
<fileset dir="${server.src}" casesensitive="yes">
<filename name="**/*.java"/>
<not>
<filename name="**/*Test*"/>
</not>
</fileset>
等价于
<fileset dir="${server.src}" casesensitive="yes">
<filename name="**/*.java"/>
<filename name="**/*Test*" negate="true"/>
</fileset>
等价于
<fileset dir="${server.src}" casesensitive="yes">
<include name="**/*.java"/>
<exclude name="**/*Test*"/>
</fileset>


selector容器可以包含其他的selector,常用的selector容器有:
<and>
<contains>
<custom>
<date>
<depend>
<depth>
<filename>
<majority>
<none>
<not>
<or>
<present>
<selector>
<size>


例如选择比4096bytes大且从上个millenium没有更新的JAR文件
<fileset dir="${dist}" includes="**/*.jar">
<and>
<size value="4" units="Ki" when="more"/>
<date datetime="01/01/2001 12:00 AM" when="before"/>
</and>
</fileset>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值