關于ANT構建應用時,根據判定條件執行不同任務的實例(以操作系統類型為判定條件舉例)
最近在一個項目(湖北質檢),要求搭建一個專門的版本發布服務器,為了將源代碼構建并在服務器上測試,而選用ANT來實現。但是,在執行起動Tomcat服務時會有二個起動腳本(startup.sh與startup.bat),這二個腳本分別用于Unix與Windows操作系統;因此,為了適應不同平臺的構建,需要根據不同OS來執行不同的腳本。但是,ANT本身還沒有條件判斷的功能,所以,此處需要借助Ant-Contrib包來實現。具體如下:一:)下載apache-ant-版本號-bin.zip文件(此處是apache-ant-1.7.0-bin.zip):
下載apache-ant-1.7.0-bin.zip
二:)下載ant-contrib-版本號.jar文件(此處是ant-contrib-1.0b3.jar):
下載ant-contrib-1.0b3.jar
三:)解壓ANT安裝文件(apache-ant-1.7.0-bin.zip)到某一目錄,如:
D:\Program Files\Apache_ANT_1.7.0
四:)配置環境變量(path與classpath):
ANT_HOME=D:\Program Files\Apache_ANT_1.7.0
path=%path%;%ANT_HOME%\bin;
classpath=%ANT_HOME%\lib;
五:)安裝ant-contrib-1.0b3.jar,下面是ant-contrib-1.0b3.jar解壓包中的指南對安裝的說明,如下:
Installation
First you must install Apache Ant itself, most of the Ant-Contrib tasks require Ant 1.5 or higher to work properly, however, there are some tasks, specifically <for> which require Ant 1.6. You can download Ant from Apache.
Then you need the Ant-Contrib tasks themselves. As there is no release of these tasks yet, you have to build them from sources. Fortunately this is easy, check out the sources (grab the ant-contrib
module from CVS), change into the source directory of ant-contrib and type ant
. After Ant has completed, you'll find ant-contrib-version.jar
in the lib
subdirectory.
You now have the choice:
- Copy
ant-contrib-version.jar
to thelib
directory of your Ant installation, or on your CLASSPATH environment variable. If you want to use one of the tasks in your project, add the line<taskdef resource="net/sf/antcontrib/antlib.xml"/>
to your build file.
- Keep
ant-contrib-version.jar
in a separate location. You now have to tell Ant explicitly where to find it (say in/usr/share/java/lib
):<taskdef resource="net/sf/antcontrib/antlib.xml"> <classpath> <pathelement location="/usr/share/java/lib/ant-contrib-version.jar"/> </classpath> </taskdef>
- If you would like to use run with Ant Version 1.5 you must use the the .properties file instead. Keep in mind that some tasks will not be available to you , such as the <for> task:
<taskdef resource="net/sf/antcontrib/antcontrib.properties"> <classpath> <pathelement location="/usr/share/java/lib/ant-contrib-version.jar"/> </classpath> </taskdef>
六:)現在可以使用ant-contrib的功能來添加到ANT構建文件中,下面是本項目(湖北質檢)的構建文件,如下(注意紅色部分的條件定義與判斷的執行):
<?xml version="1.0" encoding="UTF-8" ?> <project name="QCSystem" default="startWebApplication" basedir=".">
<description>湖北質檢ANT構建與Tomcat應用起動配置文件,起動應用時根據不同的操作系統調用不同的Tomcat起運腳本(startup.sh或startup.bat)!</description> <!-- ********** 構件文件相關屬性 *********** --> <!-- 系統環境變量 --> <property environment="sys-env"/>
<!-- 通用路徑或文件夾名稱 --> <property name="srcDir" location="src"/> <property name="webAppDir" location="webapp"/>
<property name="webInfName" value="WEB-INF"/> <property name="destDirName" value="classes"/>
<property name="tomcatCommonName" value="common"/>
<!-- 添加"Ant-Contrib"對ANT構建文件的支持 -->
<taskdef resource="net/sf/antcontrib/antcontrib.properties">
<classpath>
<pathelement location="${sys-env.ANT_HOME}/Ant-Contrib/ant-contrib-1.0b3.jar"/>
</classpath>
</taskdef>
<!-- 初始化操作系統判斷條件 -->
<property name="winOS" value="win"/>
<property name="unixOS" value="unix"/> <target name="init">
<echo message="初始化操作系統判斷條件[開始]..." />
<condition property="OSType" value="${winOS}">
<os family="Windows"/>
</condition>
<condition property="OSType" value="${unixOS}">
<os family="UNIX"/>
</condition>
<echo message="初始化操作系統判斷條件[結束]..." />
</target>
<!-- 第三方類庫--> <path id="compileLibFiles"> <fileset dir="${webAppDir}/${webInfName}/lib"> <include name="**/*.jar"/> </fileset>
<fileset dir="${sys-env.CATALINA_HOME}/${tomcatCommonName}/lib"> <include name="**/*.jar"/> </fileset> </path>
<!-- 編譯工程 --> <target name="compile"> <echo message="湖北質檢項目編譯[開始] ..."/> <javac srcdir="${srcDir}" debug="on" destdir="${webAppDir}/${webInfName}/${destDirName}" includes="**/*.java"> <classpath refid="compileLibFiles"/> </javac>
<!-- 拷貝配置文件到編譯目錄 --> <copy todir="${webAppDir}/${webInfName}/${destDirName}"> <fileset dir="${srcDir}" excludes="**/*.java"/> </copy> <echo message="湖北質檢項目編譯[結束] ..."/> </target>
<!-- 根據不同的操作系統調用不同的Tomcat起動程序(Windows操作系統:startup.bat Unix操作系統:startup.sh) --> <target name="startWebApplication" depends="init, compile"> <echo message="起動湖北質檢應用[開始] ..."/> <if>
<equals arg1="${OSType}" arg2="${winOS}"/>
<!-- 下邊這種方式直接判斷操作系統也可以 -->
<!--os family="WINDOWS"/-->
<then>
<exec dir="${sys-env.CATALINA_HOME}/bin" executable="${sys-env.CATALINA_HOME}/bin/startup.bat"/>
</then>
<else>
<exec dir="${sys-env.CATALINA_HOME}/bin" executable="${sys-env.CATALINA_HOME}/bin/startup.sh"/>
</else>
</if>
<echo message="起動湖北質檢應用[結束] ..."/> </target>
</project>
posted on 2008-05-14 11:36 心無痕 閱讀(4894) 評論(1) 編輯 收藏 所屬分類: JAVA