下圖是針對性的工程目錄結構,有環境差別env目錄,打包或編譯依賴jar包expandLib目錄,其餘都是Java web工程經常使用目錄。注意ant的打包配置文件存放到工程根目錄下,同時還有2個爲增量打包作準備須要的txt文件changeLog.txt(svn、cvs等項目版本控制提交文件記錄)、patchfiles.txt(加工changeLog.txt文件記錄獲得的最終須要增量打包的編譯文件):html
解釋說明下ant打包的build.xml配置文件,裏面含有5個環境的增量打包和全量打包,內容都大同小異,讀者本身根據實際狀況,靈活應變,不吝賜教。裏面包含ant腳本不是較經常使用命令,注意ant版本是否包含該命令,有關ant的命令說明詳見官網API。build.xml配置內容具體以下:java
<?xml version="1.0" encoding="UTF-8"?> <project name="build.taiPingSCV" basedir="." default="help"> <property name="com.dir" value="."/> <property name="com.src.dir" value="${com.dir}/src"/> <property name="com.web.dir" value="${com.dir}/WebContent"/> <property name="com.webinfo.dir" value="${com.web.dir}/WEB-INF"/> <property name="com.lib.dir" value="${com.webinfo.dir}/lib"/> <property name="com.expand.lib.dir" value="${com.dir}/expandLib"/> <!-- PRO生產配置環境地址 --> <property name="com.Pro.dir" value="${com.dir}/env/pro"/> <!-- UAT配置環境地址 --> <property name="com.Uat.dir" value="${com.dir}/env/uat"/> <!-- PRE配置環境地址 --> <property name="com.Pre.dir" value="${com.dir}/env/pre"/> <!-- DEV開發環境配置環境地址 --> <property name="com.Dev.dir" value="${com.dir}/env/dev"/> <!-- REC災備環境配置環境地址 --> <property name="com.Rec.dir" value="${com.dir}/env/rec"/> <property name="com.build.dir" value="mybuild"/> <property name="com.build.webinfo.dir" value="${com.build.dir}/WEB-INF"/> <property name="com.build.class.dir" value="${com.build.webinfo.dir}/classes"/> <property name="com.build.lib.dir" value="${com.build.webinfo.dir}/lib"/> <property name="com.dist.dir" value="mydist"/> <property name="com.patch.dir" value="mypatch"/> <property name="change.log" value="${com.dir}/changeLog.txt"/> <property name="patch.includesfile" value="${com.dir}/patchfiles.txt"/> <path id="lib.class.path"> <pathelement location="${com.build.class.dir}" /> <fileset dir="${com.lib.dir}"> <include name="*.jar"/> </fileset> <fileset dir="${com.expand.lib.dir}"> <include name="*.jar"/> </fileset> </path> <target name="help"> <echo level="info">--------構建全量命令說明------------</echo> <echo level="info">SCVDev:開發測試環境打包</echo> <echo level="info">SCVUat:用戶測試環境打包</echo> <echo level="info">SCVPre:預生產環境打包</echo> <echo level="info">SCVPro:生產環境打包</echo> <echo level="info">SCVRec:災備環境打包</echo> <echo level="info">---------構建增量命令說明------------</echo> <echo level="info">SCVDevPatch:開發測試環境增量打包</echo> <echo level="info">SCVUatPatch:用戶測試環境增量打包</echo> <echo level="info">SCVPrePatch:預生產環境增量打包</echo> <echo level="info">SCVProPatch:生產環境增量打包</echo> <echo level="info">SCVRecPatch:災備環境增量打包</echo> </target> <!-- 刪除以前的打包文件目錄 --> <target name="com.deletepath"> <echo level="info">**************delete path**************</echo> <delete dir="${com.dist.dir}"/> <delete dir="${com.build.dir}"/> <delete dir="${com.patch.dir}"/> </target> <!-- 生成路徑文件 --> <target name="com.init" depends="com.deletepath"> <echo level="info">**************build path**************</echo> <mkdir dir="${com.dist.dir}"/> <mkdir dir="${com.patch.dir}"/> <mkdir dir="${com.build.dir}"/> <mkdir dir="${com.build.webinfo.dir}"/> <mkdir dir="${com.build.class.dir}"/> <mkdir dir="${com.build.lib.dir}"/> </target> <!-- 編譯java代碼--> <target name="com.compile" depends="com.init" description="編譯源文件"> <echo level="info">**************compile code**************</echo> <javac srcdir="${com.src.dir}" destdir="${com.build.class.dir}" debug="on" source="1.6" target="1.6" > <classpath refid="lib.class.path"/> <compilerarg line="-encoding GBK "/> </javac> </target> <!-- 通用配置文件拷貝 --> <target name="com.copyfile" depends="com.compile" description="拷貝文件打WAR包"> <echo level="info">**************copy files start**************</echo> <copy todir="${com.build.dir}" overwrite="true" preservelastmodified="true"> <fileset dir="${com.web.dir}"> <exclude name="**/junit*.jar" /> <exclude name="**/servlet-api.jar" /> <exclude name="**/jsp-api.jar" /> <exclude name="**/rt.jar" /> <exclude name="**/classes/**" /> </fileset> </copy> <copy todir="${com.build.class.dir}" overwrite="true" preservelastmodified="true"> <fileset dir="${com.src.dir}"> <exclude name="**/*.class" /> <exclude name="**/*.java" /> </fileset> </copy> </target> <!-- UAT測試環境文件拷貝並打war包 --> <target name="SCVUat" depends="com.copyfile"> <echo level="info">**************copy uat env files**************</echo> <antcall target="copyEnvFile"> <param name="copyFormDir" value="${com.Uat.dir}" /> </antcall> <antcall target="warPackage"> <param name="war_name" value="COMSCV_Uat" /> <param name="war_dir" value="${com.build.dir}" /> </antcall> <echo level="info">**************UAT WAR IS OK**************</echo> </target> <!-- PRE預上線環境文件拷貝並打war包 --> <target name="SCVPre" depends="com.copyfile"> <echo level="info">**************copy pre env files**************</echo> <antcall target="copyEnvFile"> <param name="copyFormDir" value="${com.Pre.dir}" /> </antcall> <antcall target="warPackage"> <param name="war_name" value="COMSCV_PRE" /> <param name="war_dir" value="${com.build.dir}" /> </antcall> <echo level="info">**************PRE WAR IS OK**************</echo> </target> <!-- PRO生產環境文件拷貝並打war包 --> <target name="SCVPro" depends="com.copyfile"> <echo level="info">**************copy pro env files**************</echo> <antcall target="copyEnvFile"> <param name="copyFormDir" value="${com.Pro.dir}" /> </antcall> <antcall target="warPackage"> <param name="war_name" value="COMSCV_Pro" /> <param name="war_dir" value="${com.build.dir}" /> </antcall> <echo level="info">**************PRO WAR IS OK**************</echo> </target> <!-- DEV測試環境文件拷貝並打war包 --> <target name="SCVDev" depends="com.copyfile"> <echo level="info">**************copy dev env files**************</echo> <antcall target="copyEnvFile"> <param name="copyFormDir" value="${com.Dev.dir}" /> </antcall> <antcall target="warPackage"> <param name="war_name" value="COMSCV_Dev" /> <param name="war_dir" value="${com.build.dir}" /> </antcall> <echo level="info">**************DEV WAR IS OK**************</echo> </target> <!-- REC災備環境文件拷貝並打war包 --> <target name="SCVRec" depends="com.copyfile"> <echo level="info">**************copy rec env files**************</echo> <antcall target="copyEnvFile"> <param name="copyFormDir" value="${com.Rec.dir}" /> </antcall> <antcall target="warPackage"> <param name="war_name" value="COMSCV_Rec" /> <param name="war_dir" value="${com.build.dir}" /> </antcall> <echo level="info">**************REC WAR IS OK**************</echo> </target> <!-- 通用打包 --> <target name="warPackage"> <war destfile="${com.dist.dir}/${war_name}.war" webxml="${com.webinfo.dir}/web.xml"> <fileset dir="${war_dir}" /> </war> <delete dir="${com.build.dir}" /> <delete dir="${com.patch.dir}" /> </target> <!-- 增量打包不須要war包自動添加web.xml文件,如更改了web.xml文件,將web.xml文件路徑添加到changelogtxt便可 --> <!-- needxmlfile屬性Since Apache Ant 1.7,故注意ant版本 --> <target name="warPatchPackage"> <war destfile="${com.dist.dir}/${war_name}.war" needxmlfile="false"> <fileset dir="${war_dir}" /> </war> <delete dir="${com.build.dir}" /> <delete dir="${com.patch.dir}" /> </target> <!-- 通用拷貝環境差別文件 --> <target name="copyEnvFile"> <copy todir="${com.build.class.dir}" overwrite="true" preservelastmodified="true"> <fileset dir="${copyFormDir}" /> </copy> </target> <!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 各個環境增量打包部分start~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~--> <!-- 處理增量文件列表 --> <target name="patchfile" description="處理 changgeLog文件"> <!-- 去除RTC提交日誌記錄中的已添加和已修改 --> <replaceregexp file="${change.log}" byline="true" encoding="GBK"> <regexp pattern="(已修改)|(已添加)|.+(已刪除)\s*" /> <substitution expression="" /> </replaceregexp> <!-- 拷貝提交日誌記錄到增量文件列表中 --> <concat destfile="${patch.includesfile}" append="false" force="true" encoding="GBK" outputencoding="GBK"> <fileset file="${change.log}" /> <filterchain> <containsregex byline="true" pattern="^([\s]*)(COMSCV/env/.+|COMSCV/src/.+|COMSCV/WebContent/.+)$" replace="\2"/> </filterchain> </concat> <!-- 匹配java文件,改爲*.class結尾來保證內部類能被拷貝成功 --> <replaceregexp file="${patch.includesfile}" byline="true" encoding="GBK"> <regexp pattern="^.+/(?:src)/(.+)\.java\s*$" /> <substitution expression="WEB-INF/classes/\1*.class" /> </replaceregexp> <!-- 匹配各個環境差別文件 --> <replaceregexp file="${patch.includesfile}" byline="true" encoding="GBK"> <regexp pattern="^COMSCV/env/\w{3}/" /> <substitution expression="WEB-INF/classes/" /> </replaceregexp> <!-- 匹配src下相關配置文件 --> <replaceregexp file="${patch.includesfile}" byline="true" encoding="GBK"> <regexp pattern="^COMSCV/src/" /> <substitution expression="WEB-INF/classes/" /> </replaceregexp> <!-- 匹配web相關文件 --> <replaceregexp file="${patch.includesfile}" byline="true" encoding="GBK"> <regexp pattern="^COMSCV/WebContent/" /> <substitution expression="" /> </replaceregexp> <!-- 匹配去除文件目錄結尾可能存在的空格 --> <replaceregexp file="${patch.includesfile}" byline="true" encoding="GBK"> <regexp pattern="\s*$" /> <substitution expression="" /> </replaceregexp> <!-- 拷貝增量文件 --> <copy todir="${com.patch.dir}" overwrite="true" preservelastmodified="true"> <fileset dir="${com.build.dir}" includesfile="${patch.includesfile}" /> </copy> </target> <!-- UAT測試環境文件拷貝並打增量war包 --> <target name="SCVUatPatch" depends="com.copyfile"> <echo level="info">**************copy uat env files**************</echo> <antcall target="copyEnvFile"> <param name="copyFormDir" value="${com.Uat.dir}" /> </antcall> <antcall target="patchfile" /> <antcall target="warPatchPackage"> <param name="war_name" value="COMSCV_Uat" /> <param name="war_dir" value="${com.patch.dir}" /> </antcall> <echo level="info">**************UAT Patch WAR IS OK**************</echo> </target> <!-- PRE預上線環境文件拷貝並打增量war包 --> <target name="SCVPrePatch" depends="com.copyfile"> <echo level="info">**************copy pre env files**************</echo> <antcall target="copyEnvFile"> <param name="copyFormDir" value="${com.Pre.dir}" /> </antcall> <antcall target="patchfile" /> <antcall target="warPatchPackage"> <param name="war_name" value="COMSCV_PRE" /> <param name="war_dir" value="${com.patch.dir}" /> </antcall> <echo level="info">**************PRE Patch WAR IS OK**************</echo> </target> <!-- PRO生產環境文件拷貝並打增量war包 --> <target name="SCVProPatch" depends="com.copyfile"> <echo level="info">**************copy pro env files**************</echo> <antcall target="copyEnvFile"> <param name="copyFormDir" value="${com.Pro.dir}" /> </antcall> <antcall target="patchfile" /> <antcall target="warPatchPackage"> <param name="war_name" value="COMSCV_Pro" /> <param name="war_dir" value="${com.patch.dir}" /> </antcall> <echo level="info">**************PRO Patch WAR IS OK**************</echo> </target> <!-- DEV環境文件拷貝並打增量war包 --> <target name="SCVDevPatch" depends="com.copyfile"> <echo level="info">**************copy dev env files**************</echo> <antcall target="copyEnvFile"> <param name="copyFormDir" value="${com.Dev.dir}" /> </antcall> <antcall target="patchfile" /> <antcall target="warPatchPackage"> <param name="war_name" value="COMSCV_Dev" /> <param name="war_dir" value="${com.patch.dir}" /> </antcall> <echo level="info">**************DEV Patch WAR IS OK**************</echo> </target> <!-- REC災備環境文件拷貝並打增量war包 --> <target name="SCVRecPatch" depends="com.copyfile"> <echo level="info">**************copy rec env files**************</echo> <antcall target="copyEnvFile"> <param name="copyFormDir" value="${com.Rec.dir}" /> </antcall> <antcall target="patchfile" /> <antcall target="warPatchPackage"> <param name="war_name" value="COMSCV_Rec" /> <param name="war_dir" value="${com.patch.dir}" /> </antcall> <echo level="info">**************REC Patch WAR IS OK**************</echo> </target> <!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 各個環境增量打包部分end~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~--> </project>
增長打包,只需將增量文件路徑存放到changeLog.txt,而後執行上面ant增量打包腳本便可。下圖爲實例:web
執行ant增量打包腳本時,須要patchfiles.txt這個臨時文件,將changeLog.txt中文件列表信息進行加工處理以後存放至此,供打包使用。下圖爲其對應實例:express
以上信息僅供碼友參考,望靈活使用。若有問題歡迎討論或賜教。
apache
其餘參考網頁:根據Eclipse SVN changelog使用ANT自動打增量包
api