流程、自動化工具的好處,就是能讓我們在保證效率的同時,也不容易犯一些低級錯誤。用ant來編譯java web工程就是這樣,能讓我們在發佈環節,更加規範、高效。這裏分享一套,我所使用的ant腳本
一、使用的前提
你的代碼目錄結構應該是這樣:
resources
-----common 不管測試環境,還是正式環境,都是一樣的配置文件,如web.xml,strtus.xml
-----daily 測試環境配置文件,如應用、數據庫的配置文件
------formal 正式環境配置文件
WebRoot
-------WEB-INF
==lib 依賴包
<?xml version="1.0" encoding="utf-8" ?>
<project name="apprelease" default="build" basedir="." >
<property name="release.dir" value="apprelease"/>
<property name="release.inf.dir" value="${release.dir}/WEB-INF"/>
<property name="release.classes.dir" value="${release.inf.dir}/classes"/>
<property name="release.lib.dir" value="${release.inf.dir}/lib"/>
<property name="release.file" value="${release.dir}.zip"/>
<property name="webroot.dir" value="WebRoot"/>
<property name="webroot.class.dir" value="WEB-INF/classes"/>
<property name="src.dir" value="src"/>
<property name="resources.dir" value="resources"/>
<property name="lib.dir" value="WEB-INF/lib"/>
<property name="classes.dir" value="classes"/>
<property name="daily" value="daily"/>
<property name="formal" value="formal"/>
<!--
設置類路徑
-->
<path id="compile.classpath">
<fileset dir="${webroot.dir}/${lib.dir}">
<exclude name="**/.svn"/>
<exclude name="test/"/>
</fileset>
</path>
<target name="build" description="Build this project" depends="clean,compile">
<antcall target="buildWithType">
<param name="buildtype" value="${formal}"/>
</antcall>
<antcall target="buildWithType">
<param name="buildtype" value="${daily}"/>
</antcall>
</target>
<target name="clean" description="Remove all generated files.">
<delete dir="${classes.dir}"/>
<delete dir="${formal}-${release.dir}"/>
<delete file="${formal}-${release.file}"/>
<delete dir="${daily}-${release.dir}"/>
<delete file="${daily}-${release.file}"/>
</target>
<target name="compile">
<mkdir dir="${classes.dir}"/>
<javac encoding="utf-8" srcdir="${src.dir}" classpathref="compile.classpath" destdir="${classes.dir}"/>
</target>
<target name="buildWithType" description="Build formal or daily">
<property name="buildtype" value="formal"/>
<copy todir="${buildtype}-${release.classes.dir}">
<fileset dir="${classes.dir}">
<exclude name="**/.svn"/>
<exclude name="*.properties"/>
<exclude name="*.property"/>
<exclude name="*.xml"/>
</fileset>
<!---從resources/daily或者resources/formal(daily)目錄下,拷貝配置文件到classes目錄 -->
<fileset dir="${resources.dir}/${buildtype}">
<include name="log4j.properties"/>
<include name="databases.properties"/>
<include name="application.properties"/>
<include name="struts.xml"/>
<include name="shiro.ini"/>
<include name="mess.properties"/>
</fileset>
<!---從resources/common目錄,拷貝配置文件到classes目錄 -->
<fileset dir="${resources.dir}/common">
<include name="key.ini"/>
<include name="struts.xml"/>
</fileset>
</copy>
<copy todir="${buildtype}-${release.inf.dir}">
<!---從resources/common目錄,拷貝配置文件到WEB-INF目錄 -->
<fileset dir="${resources.dir}/common">
<exclude name="**/.svn"/>
<include name="web.xml"/>
</fileset>
<!---從resources/daily或者resources/formal(daily)目錄下,拷貝配置文件到WEB-INF目錄 -->
<fileset dir="${resources.dir}/${buildtype}">
<include name="beans.xml"/>
</fileset>
</copy>
<copy todir="${buildtype}-${release.dir}">
<fileset dir="${webroot.dir}">
<exclude name="**/.svn"/>
<exclude name="**/${lib.dir}/**"/>
<exclude name="**/${webroot.class.dir}/**"/>
</fileset>
</copy>
<copy todir="${buildtype}-${release.lib.dir}" flatten="true">
<fileset dir="${webroot.dir}/${lib.dir}">
<exclude name="**/.svn"/>
<include name="**/*.jar"/>
</fileset>
</copy>
<zip destfile="${buildtype}-${release.file}">
<fileset dir="${buildtype}-${release.dir}"/>
</zip>
</target>
</project>
三、腳本說明
1、會過濾掉.svn文件
2、WebRoot目錄下的內容,除了WEB-INF\lib和WEB-INF\classes,其他的內容,都會拷貝到最終的zip包中
3、如果你在<copy todir="${buildtype}-${release.inf.dir}">節點中,定義了從resource目錄拷貝配置文件,那麼則會以resource目錄的爲準。例如,你在那個節點下定義了,要將resources\common\web.xml拷貝到${buildtype}-${release.inf.dir},那麼,最後zip包中的web.xml,則是resources\common的那份,而不是WebRoot目錄的那份
4、會生成daily 、formal兩個包,區別在於,daily使用的是resources\daily中的資源,而formal則是resources\formal下的
5、每次都是全量編譯java文件,這樣可以避免一些問題,例如《遇過的坑-用ant編譯java項目,如果static變量有修改,使用了該static變量的java文件, 不會重編譯》
6、WEB-INF\lib下,可以存在二級目錄,腳本執行時,會將二級目錄的內容,也都拷貝到lib下,這樣能避免“web容器下不會加載lib二級目錄jar包”這個問題