ANT打war包時出現重複檔案的解決方法
最近用ant給java web程式打war包時發現打出來的war包裡面WEB-INF裡的檔案均有兩個,後來發現加上一句fileset,include,exclude語句之後就沒有重複檔案了
打包程式碼如下
<target name="antwar" depends="build" description="not wrap the java file to .war">
<delete dir="${build.src}" />
<war warfile="${war.dest}/anttest.war" webxml="${buildwar.dest}/WEB-INF/web.xml">
<lib dir="${buildwar.dest}/WEB-INF/lib" />
<classes dir="${build.dest}" />
<fileset dir="${buildwar.dest}">
<include name="***.*" />
</fileset>
</target>
如果要忽略一些檔案就需要使用fileset和excludes
比如:複製一個目錄中所有.java為結尾的檔案,除去檔名含有Test字元的檔案
<target name="copyfiles">
<copy todir="${dest.dir}" overwrite="true">
<fileset dir="${src.dir}">
<include name="***Test*"/>
</fileset>
</copy>
</target>
複製一個目錄中所有檔案,除去含有Test字元的目錄
<target name="copyfiles">
<copy todir="${dest.dir}" overwrite="true">
<fileset dir="${src.dir}">
<exclude name="****"/>
</fileset>
</copy>
</target>
需要加/**
下面附上build.xml的所有程式碼,用於打包java web程式,並將war包拷貝(釋出)到tomcat的webapp路徑下面
<?xml version="1.0" encoding="GB2312"?>
<project basedir="." default="publish" name="anttest">
<target name="init" description="set initiation directory">
<property name="src" value="${basedir}/src" />
<property name="config" value="${basedir}/config" />
<property name="lib" value="${basedir}/WebContent/WEB-INF/lib" />
<property name="webapp" value="${basedir}/WebContent" />
<property name="packages" value="ant.*" />
<property name="build.src" value="${basedir}/AntBuild/anttest/src" />
<property name="build.dest" value="${basedir}/AntBuild/anttest/WEB-INF/classes" />
<property name="buildwar.dest" value="${basedir}/AntBuild/anttest" />
<property name="build.javadocs" value="${basedir}/Antbuild/doc" />
<property name="jar.dest" value="${basedir}/AntBuild/jar" />
<property name="war.dest" value="${basedir}/AntBuild/war" />
<property name="tomcat.home" value="D:\JavaSoft\apache-tomcat-6.0.32" />
<property name="j2eelib.dir" value="D:\JavaSoft\eclipse-gee-ganymede-win32\eclipse\plugins" />
<path id="classpath">
<fileset dir="${j2eelib.dir}">
<include name="***.jar" />
</fileset>
</path>
</target>
<target name="prepare" depends="init" description="create directories for war, and copy source file to war direcoty">
<mkdir dir="${build.src}" />
<mkdir dir="${build.dest}" />
<mkdir dir="${buildwar.dest}" />
<mkdir dir="${jar.dest}" />
<mkdir dir="${war.dest}" />
<copy todir="${build.src}">
<fileset dir="${src}" />
</copy>
<copy todir="${build.src}">
<fileset dir="${config}" />
</copy>
<copy todir="${buildwar.dest}">
<fileset dir="${webapp}" />
</copy>
</target>
<target name="build" depends="prepare" description="compile the java file. copy configuration files about properties to compile result directory">
<javac srcdir="${build.src}" destdir="${build.dest}">
<classpath refid="classpath" />
</javac>
<copy todir="${build.dest}">
<fileset dir="${build.src}">
<include name="***.*" />
<exclude name="WEB-INF*.*" />
</fileset>
</war>
</target>
<target name="publish" depends="antwar,clean" description="publish .war to tomcat directory">
<copy todir="${tomcat.home}/webapps">
<fileset dir="${war.dest}">
<include name="***.classes" />
</delete>
<delete dir="${buildwar.dest}" />
</target>
</project>