2004年9月10日

Web 應用程式 ant 建置檔

這篇是我在公司中用來建置各項 Web 應用程式的 ant 建置檔。使用時若要部署到測試網站上就執行 ant uat ,要部署到正式網站上時則執行 ant official ,不管是建置還是部署都覺得非常方便。

<?xml version="1.0" encoding="Big5"?>
<project name="myphs">
  <!--此為我特別針對現有各JAVA/JSP網站系統所規納之 ANT 單一部署設定檔。
      請配合基本目錄規劃及檔案中應修改之註記以完成新專案部署之作業。
      基本目錄規劃如下,其中 webroot 為 WEB 應用系統之跟目錄,
      通常會放置部份 JSP / HTML 檔;
                           src 為 .java 原始檔;
                    properties 為 .properties 參數設定檔集中存放區,
                               目前放置 commons-logging.properties 、
                                       log4j.properties 、
                                    及 web.xml 等三個檔。
                   WEB-INF/lib 為 系統所需額外參考之 jar 檔。
     webroot +- src
             +- properties
             +- WEB-INF/lib

※註:web.xml 在初始化過程中會被複製到 /WEB-INF/ 下並作為建立 .war 檔時之 參考檔 -->

<target name="init" description="初始設定作業,除 tmp.path 及 output.path 外,餘皆為預設設定。" > <property environment="env"/> <!-- 設定專案部署前的暫存目錄,為新專案必改之參數設定。 --> <property name="tmp.path" value="E:/Projects/Ant-Project/${ant.project.name}_TMP"/> <!-- 設定專案部署前的輸出目錄,當建置作業完成後會在本目錄下產生相關的輸出檔案。 此為新專案必改之參數設定。 --> <property name="output.path" value="E:/Projects/Ant-Project/${ant.project.name}_OUTPUT"/> <property name="source.path" value="${basedir}"/> <property name="sourcefile.path" value="${source.path}/src"/> <echo message="Building Project: '${ant.project.name}' at ${basedir}, using J2SDK v${ant.java.version}"/> <property name="build.file" value="${source.path}/build.xml"/> <property name="java.home.path" value="${env.JAVA_HOME}"/> <property name="std.lib.path" value="${java.home.path}/jre/lib/ext"/> <property name="third.lib.path" value="${source.path}/WEB-INF/lib"/> <property name="lib.path" value="${tmp.path}/WEB-INF/lib"/> <property name="classes.path" value="${tmp.path}/classes"/> <property name="java.doc.path" value="${tmp.path}/javadoc"/> <property name="jar.name" value="${lib.path}/${ant.project.name}.jar"/> <property name="war.name" value="${output.path}/${ant.project.name}.war"/> <property name="source.web.xml" value="${source.path}/WEB-INF/web.xml"/> <property name="commons.logging.properties.file" value="commons-logging.properties"/> <property name="log4j.properties.file" value="${ant.project.name}-log4j.properties"/> <property name="exception.log.file" value="${ant.project.name}-exception.log"/> <tstamp> <format pattern="yyyy-MM-dd HH:mm:ss" property="build.time"/> </tstamp> <path id="build.classpath"> <fileset dir="${lib.path}"> <include name="**/*.jar"/> </fileset> </path> </target>

<target depends="init" name="setProps" description="專案記錄初始化作為,為各專案獨自有關之參數設定。" > <!--請依實際設定修改此區塊內容。--> <property description="測試環境使用之 ftp 伺服器位址" name="uat.ftp.server" value="0.0.0.0"/> <property description="測試 ftp 伺服器使用之登入帳號" name="uat.deploy.name" value="xxxxxxxxxxxx"/> <property description="測試 ftp 伺服器使用之登入密碼" name="uat.deploy.passwd" value="xxxxxxxxxx"/> <property description="測試 web 主機中實際的 web root " name="uat.path" value="/home/webapps"/> <property description="正式環境使用之 ftp 伺服器位址" name="official.ftp.server" value="0.0.0.0"/> <property description="正式環境使用之 ftp 伺服器使用之登入帳號" name="official.deploy.name" value="xxxxxxxxx"/> <property description="正式環境使用之 ftp 伺服器使用之登入密碼" name="official.deploy.passwd" value="xxxxxxxxxx"/> <property description="正式環境使用之 web 主機中實際的 web root " name="official.path" value="/home/webapps"/> </target>

<target depends="setProps" description="清除輸出目錄" name="clean"> <delete dir="${tmp.path}" quiet="true"/> <delete dir="${output.path}" quiet="true"/> <delete quiet="true"> <fileset dir="${source.path}"> <include name="**/*.bak"/> </fileset> </delete> </target>

<target depends="clean" description="建立輸出所需相關目錄" name="prepare"> <mkdir dir="${output.path}"/> <mkdir dir="${tmp.path}"/> <mkdir dir="${lib.path}"/> <mkdir dir="${classes.path}"/> <mkdir dir="${tmp.path}/src"/> <mkdir dir="${tmp.path}/properties"/> <mkdir dir="${tmp.path}/WEB-INF/classes"/> </target>

<target depends="prepare" name="copy-system" description="屬於 Project 通用複製作業" > <copy file="${build.file}" todir="${tmp.path}"/> <copy file="${source.path}/properties/log4j.properties" tofile="${output.path}/${log4j.properties.file}"/> <!-- 若將設定檔置於 /WEB-INF/classes 下會有錯誤 --> <copy file="${source.path}/properties/${commons.logging.properties.file}" todir="${tmp.path}"/> <copy todir="${lib.path}"> <fileset dir="${third.lib.path}" includes="**/*.jar"/> </copy> <copy todir="${tmp.path}/src"> <fileset dir="${sourcefile.path}" excludes="**/*.bak" includes="**/*.java"/> </copy> <copy todir="${tmp.path}/properties"> <fileset dir="${source.path}/properties" includes="**/*.properties"/> <fileset dir="${source.path}/properties" includes="web.xml"/> </copy> <copy file="${source.path}/properties/web.xml" overwrite="true" todir="${source.path}/WEB-INF"/> </target>

<target depends="copy-system" name="copy-project" description="與該 Project 有關且必須自行調整之複製作業" > <copy todir="${tmp.path}"> <fileset dir="${source.path}" excludes="**/*.bak" includes="**/*.jsp"/> <fileset dir="${source.path}" excludes="**/*.bak" includes="**/*.html"/> <fileset dir="${source.path}" includes="**/*.txt"/> </copy> <copy todir="${tmp.path}/images"> <fileset dir="${source.path}/images"/> </copy> </target>

<target depends="copy-project" description="編譯 java 原始程式" name="compile"> <javac destdir="${classes.path}" srcdir="${sourcefile.path}"> <classpath refid="build.classpath"/> </javac> </target>

<target depends="compile" if="env.JAVADOC" name="javadoc" description="建立 JavaDoc 文件,只在已設定環境變數 JAVADOC 時執行此作業" > <mkdir dir="${java.doc.path}"/> <javadoc description="請視需要調整 packagenames 屬性之內容" author="true" bottom="Copyright by FITEL. Co., 2004 " classpath="${classes.path}" classpathref="build.classpath" destdir="${java.doc.path}" doctitle="${ant.project.name} 專案類別庫說明文件" header="${ant.project.name} JavaDcos 專案類別庫說明文件" packagenames="net.t_times" protected="true" use="true" version="true" sourcepath="${sourcefile.path}" windowtitle="T-Times.NET ${ant.project.name} 專案類別庫說明文件"> <link href="http://java.sun.com/j2se/1.4.2/docs/api/index.html"/> <link href="http://java.sun.com/j2ee/1.4/docs/api/index.html"/> </javadoc> </target>

<target description="測試環境中需要準備的動作" depends="compile" name="uat-prepare"> <property name="log4j.properties.path" value="${uat.path}"/> <property name="exception.path" value="${uat.path}"/> <mkdir dir="${exception.path}"/> <replaceregexp byline="true" file="${output.path}/${log4j.properties.file}" match="=default.exception.log" replace="=${exception.path}/${exception.log.file}"/> <replaceregexp byline="true" file="${source.web.xml}" match="${LOG4J-INIT_FILE}" replace="${log4j.properties.path}/${log4j.properties.file}"/> </target>

<target description="正式環境中所需要先行準備的動作" depends="compile" name="official-prepare"> <property name="log4j.properties.path" value="${official.path}"/> <property name="exception.path" value="${official.path}"/> <mkdir dir="${exception.path}"/> <replaceregexp byline="true" file="${output.path}/${log4j.properties.file}" match="=default.exception.log" replace="=${exception.path}/${exception.log.file}"/> <replaceregexp byline="true" file="${source.web.xml}" match="${LOG4J-INIT_FILE}" replace="${log4j.properties.path}/${log4j.properties.file}"/> </target>

<target depends="compile" name="package" description="將編譯出的 .class 檔壓製成單一 .jar 檔,放入 web root 下的 WEB-INF/lib 目錄中" > <jar basedir="${classes.path}" jarfile="${jar.name}"/> <delete dir="${classes.path}" quiet="true"/> </target>

<target depends="package" name="build" description="將整個 WEB ROOT 目錄打包成 .war 部署檔,此部署檔已完整包含開發所需各原始程式。"> <war warfile="${war.name}" webxml="${source.web.xml}"> <fileset dir="${tmp.path}" excludes="**/*.jar"/> <lib dir="${lib.path}"/> </war> </target>

<target depends="uat-prepare, build" name="uat" description="部署 UAT 測試環境" > <ftp binary="yes" depends="yes" server="${uat.ftp.server}" userid="${uat.deploy.name}" password="${uat.deploy.passwd}" > <fileset dir="${output.path}"/> </ftp> </target>

<target depends="official-prepare, build" name="official" description="部署 Official 正式環境" > <ftp binary="yes" depends="yes" server="${official.ftp.server}" userid="${official.deploy.name}" password="${official.deploy.passwd}" > <fileset dir="${output.path}"/> </ftp> </target> </project>

沒有留言:

張貼留言