现在的位置: 首页 > 综合 > 正文

ANT build.xml 文件中一些特殊的属性

2017年08月08日 ⁄ 综合 ⁄ 共 4468字 ⁄ 字号 评论关闭

一、Classpath

Ant 在运行一些任务的时候需要用<classpath>指定类路径.<classpath>可以内嵌 <pathelement>.需要注意的是

<pathelement path="${project.jar.path}> 里面path指向的不是一个预先定义的<path>,而是一个符合classpath规则的property.

pathelement的path指定的是一个预先定义的property或者一个符合classpath规则的字符串,

                       location指定的是一个jar文件或者一个目录.

<property name="project.jar.path" value="D:/temp/temp.jar;D:/temp/temp1.jar" />
<classpath>
    <pathelement path="${project.jar.path}" />      
    <pathelement location="lib/helper.jar"/>
</classpath>

1,正确的例子:

<property name="project.jar.path" value="D:/temp/temp.jar;D:/temp/temp1.jar" />
<classpath id="tcls">
       <pathelement path="${project.jar.path}" />
 </classpath>

2,如果误认为path应该指向一个预先定义的path是不正确的.

        <path id="testTask.classpath">
            <fileset dir="${project.lib.path}">
                <include name="**/*.jar" />
            </fileset>
        </path>

不正确示例:

         <classpath id="tcls">
              <pathelement path="${testTask.classpath}" />
         </classpath>

正确的做法:

如果想在<classpath>里引用预先定义好的path,应该如下:

         <classpath id="tcls">
             <path refid="testTask.classpath" />
         </classpath>

classpath内嵌的元素除了pathelement和path之外,还可以是fileset,dirset和filelist.

<path id="cls.path.ref">
    <fileset dir="lib" includes="*.jar" />
</path>
<filelist id="third-party_jars" dir="${jar.dir}"files="foo.jar, bar.jar"/> 

<classpath>
    <pathelement path="d:/temp/temp.jar" />
    <path refid="cls.path.ref" />
    
    <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>

Ant的classpath

<path id="classpath">
       构建文件最经典的classpath,其中id为类路径的名称
    
        <fileset dir="${web.dir}/WEB-INF/lib">
            <include name="*.jar"/>
        </fileset>
       向classpath中加入Web应用程序下的类库
        <fileset dir="${tomcat.home}/common/lib">
            <include name="servlet*.jar"/>
        </fileset>
       向classpath中加入tomcat服务器的类库
        <pathelement path="${build.dir}"/>
pathelement是用来加入单一的路径,例子中加入build.dir全个目录。
全个类路径任务的代码如下:
    <path id="classpath">
        <fileset dir="${web.dir}/WEB-INF/lib">
            <include name="*.jar"/>
        </fileset>
        <fileset dir="${tomcat.home}/common/lib">
            <include name="servlet*.jar"/>
        </fileset>
        <pathelement path="${build.dir}"/>
    </path>

二、fileset

fileset用来定义目录位置及操作适用于该目录下的那些子目录或文件
1. 拷贝单个文件到指定目录下。
例:<copy todir="${basedir}/new" file="${basedir}/old/old1.txt1"> 
将${basedir}/old/old.txt文件拷贝到${basedir}/new下

2. 拷贝一批文件到指定目录下,例:<copy todir="${basedir}/new">
           <fileset dir="${basedir}/old">
              <include name="old1.txt" />
              <include name="old2.txt" />
              <exclude name="old8.txt" />
          </fileset>
       </copy>
      这里fileset定义的是原文件的组成形式,<include/>子属性表示包括,<exclude/>子属性表示排除,很简单,通过他们组合实现多文件的筛选,当然我这个例子用得很傻。比如
                <include name="appgen/**"/>
                <include name="ibatis/**"/>
                <exclude name="**/*.log"/>
      拷贝appget目录和ibatis目录下除了.log文件以外的其它所有文件和子目录。

      可以把<fileset/>简写成<fileset dir="${basedir}/old" includes="old1.txt,old2.txt" />,includes可以理解成include的复数形式,包含多个文件时用逗号隔开,excludes也一样。

3. 拷贝一个目录到指定目录下
例:<copy todir="${basedir}/new">
           <fileset dir="${basedir}/old">
             <include name="appgen" />
             <include name="appgen/" />
             <include name=appgen/**" />
             <include name="appgen/***" />
           </fileset>
       </copy>
      同样使用<fileset/>属性,name指定目录名,不过这里要分两种情况,用<include/>子属性和不用<include/>子属性.
      若使用<include/>, 又要分三种情况
          若是“appgen”,则只会拷贝名为appgen的空目录过去,它里面的文件和子目录则不会拷贝。
          若是“appgen/”,或“appgen/**”,则会把整个appgen目录拷贝过去,包括里面的文件和子目录。
          若是“appgen/*”,则只会把该目录和该目录下第一级子目录的所有东西拷贝过去,而不会拷贝第二级和第二级以下的。注:“appgen/*”这儿是一个*号,*号若大于两个,也跟一个*号是同样效果。比如“appgen/*”和“appgen/****”都只拷贝appgen目录下第一级子目录。

注:若appeng这个目录本身就是个空目录(就是不存在),无论怎么写,这个空目录都不会被拷贝。也就是说,copy操作不会产生创建空目录的作用,要想创建空目录,只有用mkdir。

      若不使用任何<include>属性,如
           <fileset dir="${basedir}/old">
           </fileset>
      则会拷贝${basedir}/old下的所有文件和子目录。

注:使用<exclude/>排除目录时,目录名必须写成“appgen/”或“appgen/**”形式,否则不会生效。

      以上是三种拷贝到目录的种类,注意如果计算机中没有todir指定的路径,ant将会自动创建这个路径。


4. 拷贝单个的文件: 
〈copy tofile="old.txt" file="new.txt" /〉就这么简单就行了。
当然也可以写成

  <copy tofile="${basedir}/new/new.txt">
     <fileset dir="${basedir}/old" includes="old.txt" />
  </copy>

      这里includes就只能写一个文件,不能写上多个文件,因为不能将多个文件复制到一个文件中去,所以这样麻烦的写法是没有意义的。?这个地方还有待去验证一下.

      复制肯定还要涉及到同名覆盖的问题,ant在copy类的API中说明:Files are only copied if the source file is newer than the destination file,这里的newer是指文件的修改时间,即使你在修改时文件内容没有任何变化,只是导致修改时间变了,ant同样会覆盖同名文件,也就是说,ant不会检查文件内容。

      对于是复制目录的情况,由于目录没有修改时间,ant还是通过检查目录内文件的修改时间来决定是否覆盖的,若目录内某文件修改时间有变化,则会覆盖这个文件,而不是整个目录。

如果要强行覆盖,<copy/>有个overwrite属性,默认为false,改成true就行了。

Ant真是太方便了,以前都没注意到它。功能很强大,能创建数据库,配置服务器,部署发布应用……只需要写好build.xml文件,剩下的就交给ant来“安装”你的WEB应用了。
以上就这些:
您也可以去apache ant项目里去看一下 fileset的用法:

抱歉!评论已关闭.