Ant使用心得

 

Ant 使用心得
1       修改历史

Version
Change Time
 Author
Description
Effort Hours
0.1
2006-4-23
LevinSoft
这是创建的最初的版本。将记录开发过程中遇到的各种问题。同时和其他相关的方法、资源等等。
1
1.0
2007-8-17
LevinSoft
1.对时间标签详细解释
2.增加使用心得, 突出使用上注意问题和技巧。
0.5

2       介绍
本文主要介绍了ant使用的基本知识、使用的注意事项、一些关键字的详细分析、具体实例,同时也提供了参考资料。
3       内置属性
Ant provides access to all system properties as if they had been defined using a <property> task. For example, ${os.name} expands to the name of the operating system.
For a list of system properties see the Javadoc of System.getProperties
In addition, Ant has some built-in properties:
basedir             the absolute path of the project's basedir (as set
                    with the basedir attribute of <project>).
ant.file            the absolute path of the buildfile.
ant.version         the version of Ant
ant.project.name    the name of the project that is currently executing;
                    it is set in the name attribute of <project>.
ant.java.version    the JVM version Ant detected; currently it can hold
                    the values "1.1", "1.2", "1.3" and "1.4".
 
4       关键字
4.1    <tstamp/>
Sets the DSTAMP, TSTAMP, and TODAY properties in the current project. By default, the DSTAMP property is in the format "yyyyMMdd", TSTAMP is in the format "hhmm", and TODAY is in the format "MMMM dd yyyy". Use the nested <format> element to specify a different format.
 
These properties can be used in the build-file, for instance, to create time-stamped filenames, or used to replace placeholder tags inside documents to indicate, for example, the release date. The best place for this task is probably in an initialization target.
 
Examples:
 <tstamp/>
sets the standard DSTAMP, TSTAMP, and TODAY properties according to the default formats.
 <tstamp>
    <format property="TODAY_UK" pattern="d-MMMM-yyyy" locale="en"/>
 </tstamp>
sets the standard properties as well as the property TODAY_UK with the date/time pattern "d-MMMM-yyyy" using English locale (eg. 21-May-2001).
It is a good practice to place your tstamp tasks in a so-called initialization target, on which all other targets depend. Make sure that target is always the first one in the depends list of the other targets. In this manual, most initialization targets have the name "init".
<target name="init-light">
     <!-- General config -->
     <tstamp/>
     <property environment="env"/>
     <property file="${basedir}/build.properties"/>
 </target>
4.1.1            使用心得
1.其他target可以引用target:init-light,然后就可以方便的获得时间属性。例如:获取当前时间:${ DSTAMP}
4.2    unless
<target name="init">
    <available property="xdoclet1.2+" classname="xdoclet.modules.hibernate.HibernateDocletTask" classpathref="lib.class.path"/>
   </target>
<img src="<img src="">">
   <!-- =================================================================== -->
   <!-- Fails if XDoclet 1.2.x is not on classpath                          -->
   <!-- =================================================================== -->
   <target name="fail-if-no-xdoclet-1.2" unless="xdoclet1.2+">
      <fail>
      You must download several jar files before you can build Middlegen.
      </fail>
   </target>
另外一个例子
    <fail unless="javadocs.dir.dontknow" message="javadocs.dir must be defined before invoking this target. If you would prefer to use the default location then ensure that build.dir is defined. "/>
4.3    *
<classpath>
      <pathelement path="${classpath}"/>
      <fileset dir="lib">
        <include name="**/*.jar"/> // lib 目录下的全部的jar文件,包括
                                     子目录的jar文件
      </fileset>
      <pathelement location="classes"/>
      <dirset dir="${build.dir}">
        <include name="apps/**/classes"/>
        <exclude name="apps/**/*Test*"/>
      </dirset>
      <filelist refid="third-party_jars">
    </classpath>
This builds a path that holds the value of ${classpath}, followed by all jar files in the lib directory, the classes directory, all directories named classes under the apps subdirectory of ${build.dir}, except those that have the text Test in their name, and the files specified in the referenced FileList.
4.3.1            使用心得
"**":是任意的文件名称
*/*.jar:在目录下的所有以.jar结尾的文件。包括子目录的文件。因为有个*/标示。
*.jar 不包括子目录。
4.4    &
    < property name = "axis.home" location = "../.." />
    < property name = "componentName" value = "samples/xbeans" />
    &properties;
    &paths;
    &taskdefs;
    &taskdefs_post_compile;
&targets;
使用心得:在 & 使用之前,必须先引入相关的文件。
<? xml version = "1.0" ?>
<! DOCTYPE project [
        <!ENTITY properties SYSTEM "file:../../xmls/properties.xml">
        <!ENTITY paths SYSTEM "file:../../xmls/path_refs.xml">
        <!ENTITY taskdefs SYSTEM "file:../../xmls/taskdefs.xml">
        <!ENTITY taskdefs_post_compile SYSTEM "file:../../xmls/taskdefs_post_compile.xml">
        <!ENTITY targets SYSTEM "file:../../xmls/targets.xml">
]>
4.5    fileset
<path id="boot.classpath">
    <fileset dir="${lib.dir}/endorsed">
        <include name="**/*.jar"/>
    </fileset>
    <pathelement path="${sun.boot.class.path}"/>
 </path>
 
    <target name= "buildzip" depends= "init" >
       <echo message= "Generating zip file... ..." />
      
       <zip destfile= "${deploy.dir}/${appname}-${build.number}-${DSTAMP}.zip" update= "true" defaultexcludes= "true" >
           <fileset dir= "deploy" >
              <exclude name= "*.sh" />
              <exclude name= "*.zip" />
           </fileset>
       </zip>
    </target>
4.5.1            使用心得
1.对于 <fileset dir= "deploy" > <exclude name= "*.sh" />
*.sh代表是deploy目录下面的文件。而不是根目录下的文件。
4.6    clean
    < target name = "clean" >
        < echo message = "Removing ${build.dir}/classes/${componentName} and ${build.dir}/work/${componentName}" />
        < delete dir = "${build.dir}/classes/${componentName}" />
        < delete dir = "${build.dir}/work/${componentName}" />
    </ target >
4.7    move
<move todir="${merge.dir}/web" flatten="true">
              <fileset dir="${src.generated.dir}">
                    <include name="**/struts-message-resources.xml"/>
                    <include name="**/struts-plugins.xml"/>
              </fileset>
        </move> 
4.8    replace
        < replace file = "${build.dir}/work/${componentName}/deploy.wsdd"
            token = "BeanDeserializerFactory"
            value = "xbeans.XmlBeanDeserializerFactory" >
        </ replace >
4.9    copy
例如:axis1.4/samples/xbeans/build.xml
        < copy todir = "${build.dir}/work/samples/xbeans" overwrite = "yes" >
            < fileset dir = "${axis.home}/samples/xbeans" >
                < include name = "*Test*.java" />
                < include name = "*Impl.java" />
            </ fileset >
        </ copy >
Usboss build.xml 文件:
<target name="copy-generated-to-deploy" depends="">
        <copy todir="${deploy.dir}/WEB-INF"   > <!-- overwrite="true" -->
           <fileset dir="${portal.dir}/WEB-INF">
              <exclude name="lib/**/*"/>
           </fileset>
        </copy> 
     </target>
4.10       javac
例如:axis1.4/samples/xbeans/build.xml
< javac srcdir = "${build.dir}/work" destdir = "${build.dest}" debug = "${debug}" nowarn = "${nowarn}" source = "${source}" fork = "${javac.fork}" >
// refid 将引用 <path id> <classpath path> 将应用路径,例如:
            < classpath refid = "classpath" />
            < include name = "samples/xbeans/*.java" />
            < exclude name = "samples/xbeans/*TestCase*" unless = "junit.present" />
        </ javac >
另外一种情况:
    < target name = "compile" depends = "prepare" >
         < javac classpathref = "classpath.id" srcdir = "${src.dir}" destdir = "${build.dir}" />
</ target >
 
    < path id = "classpath.id" >
       < fileset dir = "${axis.dir}/build/lib" >
           < include name = "*.jar" />
       </ fileset >
    < pathelement location = "${deploy.dir}/../lib/jboss-j2ee.jar" />
    < pathelement location = "${build.dir}" />
    </ path >
4.11       classpath & path&pathelement&filelist
4.11.1      pathelement
location可以指定具体的jar文件,也可以是目录名称。
    < pathelement location = "${deploy.dir}/../lib/jboss-j2ee.jar" />
    < pathelement location = "${build.dir}" />
4.11.2      classpath,fileset,direst,filelist
 <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>
This builds a path that holds the value of ${classpath}, followed by all jar files in the lib directory, the classes directory, all directories named classes under the apps subdirectory of ${build.dir}, except those that have the text Test in their name, and the files specified in the referenced FileList
The shortcuts previously mentioned for <classpath> are also valid for <path> .F or example:
    <path id="base.path">
      <pathelement path="${classpath}"/>
</path>
can be written as:
    <path id="base.path" path="${classpath}"/>
4.11.3      Path设置
If you want to use the same path-like structure for several tasks, you can define them with a <path> element at the same level as targets, and reference them via their id attribute - see References for an example
例如:axis1.4/xmls/path_refs.xml
< path id = "classpath" >
    < pathelement location = "${xercesImpl.jar}" />
    < pathelement location = "${xmlParserAPIs.jar}" />
    < pathelement location = "${xalan.jar}" />
    < pathelement location = "${xml-apis.jar}" />
    < pathelement location = "${xerces.jar}" />
    < pathelement location = "${regexp.jar}" />
    < pathelement location = "${xmlunit.jar}" />
    < pathelement location = "${jsse.jar}" />
    < pathelement location = "${jimi.jar}" />
    < pathelement location = "${activation.jar}" />
    < pathelement location = "${mailapi.jar}" />
    < pathelement location = "${tools.jar}" />
    < pathelement location = "${j2ee.jar}" />
    < pathelement location = "${junit.jar}" />
    < pathelement location = "${servlet.jar}" />
    < fileset dir = "${lib.dir}" >
        < include name = "**/*.jar" />
    </ fileset >
    < pathelement path = "${java.class.path}" />
    < pathelement location = "${build.dest}" />
    < pathelement location = "${build.dir}/tools" />
    < pathelement location = "${build.lib}/axis-ant.jar" />
    < pathelement location = "${build.lib}/axis-schema.jar" />
<!--
    <pathelement location="${build.dir}/work" />
-->
  </ path >
 
  < path id = "boot.classpath" >
     < fileset dir = "${lib.dir}/endorsed" >
        < include name = "**/*.jar" />
    </ fileset >
    < pathelement path = "${sun.boot.class.path}" />
  </ path >
4.11.4      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>
 
4.12       taskdef
例如:axis1.4/samples/xbeans/build.xml
4.12.1      直接定义task
Axis1_4/samples/ejb/ant-build.xml文件中,包括的内容。
  < taskdef name = "axis-java2wsdl" classname = "org.apache.axis.tools.ant.wsdl.Java2WsdlAntTask"
    loaderref = "axis" >
        < classpath refid = "classpath.id" />
  </ taskdef >
  < taskdef name = "axis-admin" classname = "org.apache.axis.tools.ant.axis.AdminClientTask"
    loaderref = "axis" >
        < classpath refid = "classpath.id" />
  </ taskdef >
  < taskdef name = "axis-wsdl2java" classname = "org.apache.axis.tools.ant.wsdl.Wsdl2javaAntTask"
    loaderref = "axis" >
        < classpath refid = "classpath.id" />
  </ taskdef >
 
其中: classpath.id 的配置为:
    < path id = "classpath.id" >
       < fileset dir = "${axis.dir}/build/lib" >
           < include name = "*.jar" />
       </ fileset >
    < pathelement location = "${deploy.dir}/../lib/jboss-j2ee.jar" />
    < pathelement location = "${build.dir}" />
    </ path >
4.12.2      方式一:在target内定义
< target name = "compile" depends = "copy" if = "xmlbeans.present" >
        < echo message = "Compiling samples.xbeans" />
 
        < taskdef name = "xmlbean"
              classname = "org.apache.xmlbeans.impl.tool.XMLBean"
              classpathref = "classpath" />
4.12.3      方式二:包含资源文件的方式
<taskdef resource="axis-tasks.properties" classpathref="axis.classpath" />
4.13       antcall
    < target name = "run" >
        < antcall target = "execute-Component" />
    </ target >
4.14       unless = if not的使用
<!-- Cleans the build directory
 REQUIRED PROPERTIES
    build.dir if not present then this target fails
// 除非build.dir是存在(true)的(“如果不”是true),否则,将产生失败信息,停止向下执行。
// unless = if not
 -->
 <target name="clean" description="Removes the build directory">
    <fail unless="build.dir" message="build.dir must be set before invoking this target"/>
    <delete dir="${build.dir}" />
 </target>
4.15       jar
例如:Axis1_4/samples/ejb/ant-build.xml文件中,
    < target name = "jar" depends = "compile" >
        < jar destfile = "${app.name}.jar" >
            < fileset dir = "${build.dir}" >
                < include name = "**/*.class" />
            </ fileset >
            < metainf dir = "${metainf.dir}" />
        </ jar >  
    </ target >
4.16       在ant中使用符号:&lt; 表示 < &quot; 表示 " &gt; 表示 > 
把相关的信息都加到相关文档中,转义字符说明     <echo file="${src.generated.dir}/struts-message-resources.xml" append="false" >
     &lt;message-resources parameter=&quot;ApplicationResources_${base.portal.java}&quot; key=&quot;${base.portal.java}&quot; /&gt;
     </echo>
     <echo file="${src.generated.dir}/struts-plugins.xml" append="false" >
5       设计方法
1.在copy文件时,copy的原则是根据修改的时间,来比较的, 如果只修改了class下面的properties文件,而它的原文件处没有改动,在执行编译和ant中copy命令时,它不将copy它,因为是根据时间来匹配的, 除非添加上, overwrite=true,
6       总结和展望
a)      简单的总结前面知识
b)      今后的提高部分
7       参考资料
1.  Axis包的是实现方式。把path、properties等等,全都分开。参见xmls/下的相关文件。
2.ANT十五大最佳实践。是个很好的文章。无论在自己ant或修改别人的build.xml时都是一个很好的指导。
3.D盘的Java library中有一个ant resource包。
4.Ant网站。
5. ANT 十五大最佳实践。是个很好的文章。无论在自己ant或修改别人的build.xml时都是一个很好的指导。
6.参照Sun的Adventure的build.xml文件。很的棒的文档。也可以在自己的文档中应用。
8       术语和附录
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值