最近用ant给java web程序打war包时发现打出来的war包里面WEB-INF里的文件均有两个,后来发现加上一句fileset,include,exclude语句之后就没有重复文件了
打包代码如下
<target name="antwar" depends="build" description="not wrap the java file to .war">
<wbr><wbr><delete dir="${build.src}" /><br><wbr><wbr><war warfile="${war.dest}/anttest.war" webxml="${buildwar.dest}/WEB-INF/web.xml"><br><wbr><wbr><wbr><lib dir="${buildwar.dest}/WEB-INF/lib" /><br><wbr><wbr><wbr><classes dir="${build.dest}" /><br><wbr><wbr><wbr><span style="color:#ED1C24; word-wrap:normal; word-break:normal; line-height:21px"><fileset dir="${buildwar.dest}"><br><wbr><wbr><wbr><wbr><include name="***.*" /><br><wbr><wbr><wbr></fileset><br></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span><wbr><wbr></war><br><wbr></target></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
如果要忽略一些文件就需要使用fileset和excludes
比如:复制一个目录中所有.java为结尾的文件,除去文件名含有Test字符的文件
<target name="copyfiles"><wbr><wbr><wbr><wbr><wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><copy todir="${dest.dir}" overwrite="true"><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><fileset dir="${src.dir}"><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><include name="***Test*"/><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></fileset><br><wbr><wbr><wbr><wbr><wbr><wbr></copy><br> </target><br><br> 复制一个目录中所有文件,除去含有Test字符的目录<br> <target name="copyfiles"><wbr><wbr><wbr><wbr><wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><copy todir="${dest.dir}" overwrite="true"><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><fileset dir="${src.dir}"><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><exclude name="****"/><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></fileset><br><wbr><wbr><wbr><wbr><wbr><wbr></copy><br> </target><br> 需要加/**<br></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
下面附上build.xml的所有代码,用于打包java web程序,并将war包拷贝(发布)到tomcat的webapp路径下面
<?xml version="1.0" encoding="GB2312"?>
<project basedir="." default="publish" name="anttest">
<wbr><target name="init" description="set initiation directory"><br><wbr><wbr><property name="src" value="${basedir}/src" /><br><wbr><wbr><property name="config" value="${basedir}/config" /><br><wbr><wbr><property name="lib" value="${basedir}/WebContent/WEB-INF/lib" /><br><wbr><wbr><property name="webapp" value="${basedir}/WebContent" /><br><wbr><wbr><property name="packages" value="ant.*" /></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><property name="build.src" value="${basedir}/AntBuild/anttest/src" /><br><wbr><wbr><property name="build.dest" value="${basedir}/AntBuild/anttest/WEB-INF/classes" /></wbr></wbr></wbr></wbr>
<wbr><wbr><property name="buildwar.dest" value="${basedir}/AntBuild/anttest" /><br><wbr><wbr><property name="build.javadocs" value="${basedir}/Antbuild/doc" /><br><wbr><wbr><property name="jar.dest" value="${basedir}/AntBuild/jar" /><br><wbr><wbr><property name="war.dest" value="${basedir}/AntBuild/war" /></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><property name="tomcat.home" value="D:\JavaSoft\apache-tomcat-6.0.32" /><br><wbr><wbr><property name="j2eelib.dir" value="D:\JavaSoft\eclipse-gee-ganymede-win32\eclipse\plugins" /></wbr></wbr></wbr></wbr>
<wbr><wbr><path id="classpath"><br><wbr><wbr><wbr><fileset dir="${j2eelib.dir}"><br><wbr><wbr><wbr><wbr><include name="***.jar" /><br><wbr><wbr><wbr></fileset><br><wbr><wbr></path><br><wbr></target></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><target name="prepare" depends="init" description="create directories for war, and copy source file to war direcoty"><br><wbr><wbr><mkdir dir="${build.src}" /><br><wbr><wbr><mkdir dir="${build.dest}" /><br><wbr><wbr><mkdir dir="${buildwar.dest}" /><br><wbr><wbr><mkdir dir="${jar.dest}" /><br><wbr><wbr><mkdir dir="${war.dest}" /><br><wbr><wbr><copy todir="${build.src}"><br><wbr><wbr><wbr><fileset dir="${src}" /><br><wbr><wbr></copy><br><wbr><wbr><copy todir="${build.src}"><br><wbr><wbr><wbr><fileset dir="${config}" /><br><wbr><wbr></copy><br><wbr><wbr><copy todir="${buildwar.dest}"><br><wbr><wbr><wbr><fileset dir="${webapp}" /><br><wbr><wbr></copy><br><wbr></target></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><target name="build" depends="prepare" description="compile the java file. copy configuration files about properties to compile result directory"><br><wbr><wbr><javac srcdir="${build.src}" destdir="${build.dest}"><br><wbr><wbr><wbr><classpath refid="classpath" /><br><wbr><wbr></javac><br><wbr><wbr><copy todir="${build.dest}"><br><wbr><wbr><wbr><fileset dir="${build.src}"><br><wbr><wbr><wbr><wbr><include name="***.*" /><br><wbr><wbr><wbr><wbr><exclude name="WEB-INF*.*" /><br><wbr><wbr><wbr></fileset><br><wbr><wbr></war><br><wbr></target></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><target name="publish" depends="antwar,clean" description="publish .war to tomcat directory"><br><wbr><wbr><copy todir="${tomcat.home}/webapps"><br><wbr><wbr><wbr><fileset dir="${war.dest}"><br><wbr><wbr><wbr><wbr><include name="***.classes" /><br><wbr><wbr></delete><br><wbr><wbr><delete dir="${buildwar.dest}" /><br><wbr></target><br> </project></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>