作者:fbysss
msn:[email protected]
blog:blog.csdn.net/fbysss
声明:本文由fbysss原创,转载请注明出处
关键字:Ant
前言:本人记忆力不好,这些东西,每次都得现看文档。把我最常用的Ant关键点东西写下来,以供备忘。
要点:在docs目录找到Using Ant->Example Buildfile
好习惯:一个build.xml配置一个build.properties
在第一个target(习惯命名为init)中和build.xml绑定
<property file="${basedir}/build.properties" />
其中的basedir是内置属性,即所谓“build-in properties”
时间戳:<tstamp/>
可以指定时间戳格式
<tstamp prefix="date">
<format property="date_stamp" pattern="yyyy-MM-dd" offset="2" unit="month"/>
</tstamp>
其中,prefix必须要,用于指定一个结构体变量,存放生成的时间。使用:内置成员有${date.DSTAMP},${date.TSTAMP},${date.TODAY}、
其他成员,比如format里面定义的属性。使用:${date.date_stamp}
unit和offset配合使用。表示生成一个与某个段相差多久的时间值。
其中unit包括:
• millisecond
• second
• minute
• hour
• day
• week
• month
• year
offset为正数表示往后的时间,负数往前生成。
<echo message=”打印提示信息”/>
需要的类库:
<classpath>
<pathelement path="${classpath}"/>
<fileset dir="lib">
<include name="**/*.jar"/>
</fileset>
<pathelement location="classes"/>
<dirset dir="build">
<include name="apps/**/classes"/>
<exclude name="apps/**/*Test*"/>
</dirset>
<filelist refid="third-party_jars"/>
</classpath>
执行命令行:
<exec executable="cmd">
<arg value="/c"/>
<arg value="ant.bat"/>
<arg value="-p"/>
</exec>
其他重要task:copy,zip,war等,看看帮助里面Ant Tasks,都有详细解释。
命令行中,可以使用ant <targetname>来执行相应的target。但是如果要实现从清理,编译到部署一气呵成的步骤,怎么做呢?
target的depends属性的意思,就是要执行本target,首先要执行depends里面的target。
因此我们可以利用这个属性,写一个run target:
<target name=”run” depends=”clean,init,build,copy,deploy/>
然后在project节点中定义<project name=”myapp” basedir=”.” default=”run”>
一些内置(build-in)变量:
ant.project.name 使用${ant.project.name} 也就是<project name=”myapp”>这里配置的name
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.2", "1.3", "1.4" and "1.5".
其他:
ant taskName -D<property>=<value>
这个-D参数,可以在命令行中,动态指定一些属性的值
应用场景:比如我们要拷贝指定扩展名的文件,如果每个扩展名都制定一个task,没有必要,可以把可变部分做成变量,动态传入。