09年的时候写过一个ant的简单安装和最基本的使用其实就是一个hello world 级别的东西。
最近因为项目需要又用了一次ant来打包,所以把项目中用到的东西再整理下。
1.定义常量
通常是一些路径
<property name="project-src" value="${basedir}/src" />
也有可能是系统环境变量里面的路径
<property environment="env"/>
<property name="xx-lib" value="${env.XX_HOME}/lib" />
注意:如果新建一个系统变量, 在Eclipse 不会自动更新你新加的变量,需要重启Eclipse。
获取时间戳
<tstamp>
<format property="time" pattern="yyyy-MM-dd hh:mm:ss" />
</tstamp>
2.定义target
基本样子
<target name="jar-all" depends="clean,jar-a,jar-b" description="Builds a.jar, b.jar"> </target>
一个清理文件或者文件夹的target
<target name="clean" description="Removes all generated artifacts">
<delete dir="fouder" />
<delete file="a.jar" />
<delete file="b.jar" />
</target>
对项目中某些文件package打一个jar包,分为两步,先编译,再打JAR包
<target name="compile-a" depends="clean,init">
<mkdir dir="build" />
<javac srcdir="${project-src}" destdir="build" debug="true" deprecation="off" encoding="UTF-8">
<include name="com/tang/test/a/**/*" />
<classpath>
<pathelement path="${xx-lib}/xx.jar" />
<pathelement path="${third-part-lib}/log4j-1.2.15.jar" />
</classpath >
</javac>
</target>
编译好后接下来打包
<target name="jar-a" depends="compile-a" description="Builds a.jar"> <jar basedir="build" destfile="a.jar"> <manifest> <attribute name="Created-By" value="locke." /> <attribute name="Created-Date" value="${time}" /> </manifest> </jar> <delete dir="build" /> </target>
3.引用其他的文件的target
<target name="backend">
<ant antfile="${basedir}/xx_jar.xml" target="jar-all" />
</target>
4.最后给出一个打完整war包的build.xml
<project name="war Builder" default="war-a" basedir="."> <target name="clean"> <delete dir="${tempWebRoot}" /> </target> <target name="init"> <property name="webRoot" value="WebRoot" /> <property name="webRootLib" value="WebRoot/WEB-INF/lib" /> <property name="tempWebRoot" value="tempWebRoot" /> <property name="tempWebRootLib" value="tempWebRoot/WEB-INF/lib" /> </target> <target name="copy" depends="init,clean"> <mkdir dir="${tempWebRoot}" /> <mkdir dir="${tempWebRootLib}" /> <copy todir="${tempWebRoot}"> <fileset dir="${webRoot}"> <exclude name="**/*.war" /> <exclude name="WEB-INF/classes/**/*" /> <exclude name="**/log4j-1.2.15.jar" /> </fileset> </copy> <copy todir="${tempWebRootLib}" file="${basedir}/console.jar"> </copy> </target> <target name="war-a" depends="copy" description="package war"> <war warfile="${basedir}/a.war" webxml="${tempWebRoot}/WEB-INF/web.xml"> <!-- <lib dir="${tempWebRoot}/WEB-INF/lib" /> <classes dir="${tempWebRoot}" />--> <fileset dir="${tempWebRoot}" /> </war> <delete dir="${tempWebRoot}" /> </target> </project>
5. 小技巧
5.1 将多个jar 变成一个jar
<project name="test" default="jarjar" basedir="."> <target name="init"> <mkdir dir="newJar" /> </target> <target name="jarjar"> <jar destfile="newJar/one.jar"> <zipgroupfileset dir="." includes="*.jar" excludes="*.properties" /> </jar> </target> </project>
5.2 打jar包时classpath中包含很多不同jar时
有时候我们写的程序可能包含很多JAR包,如果一个一个写很麻烦不错,还容易出错,最让人受不了的是jar包版本变了之后还需要重新修改编译文件。
<target name="init"> <property name="third-part-lib" value="${basedir}/WebRoot/WEB-INF/lib" description="the third part lib" /> <path id="libPath"> <fileset dir="${third-part-lib}"> <include name="**/*.jar" /> </fileset> </path> <pathconvert property="asmm.classpath" pathsep=" "> <mapper> <chainedmapper> <flattenmapper /> <globmapper from="*" to="WebRoot/WEB-INF/lib/*" /> </chainedmapper> </mapper> <path refid="libPath" /> </pathconvert> </target> <target name="compile" depends="clean,init"> <mkdir dir="build" /> <javac srcdir="${project-src}" destdir="build" debug="true" deprecation="off" encoding="UTF-8"> <classpath> <fileset dir="${third-part-lib}"> <include name="**/*.jar" /> </fileset> <fileset dir="${as-lib}"> <include name="a.jar" /> <include name="b.jar" /> </fileset> </classpath > </javac> </target> <target name="jar" depends="compile" description="Builds power.jar"> <jar basedir="build" destfile="${dest}/${jar-name}"> <manifest> <attribute name="Main-Class" value="com.tang.test.JettyServer" /> <attribute name="Class-Path" value=". lib/a.jar lib/b.jar server_properties.xml log4j.properties ${asmm.classpath}" /> <attribute name="Created-By" value="locke." /> <attribute name="Created-Date" value="${time}" /> </manifest> </jar> </target>
这里我只写出来了部分关键的内容。