● AppFuse的ant構(gòu)建使用非常復(fù)雜,功能也非常強大,
Ø build.xml的正常運行依賴以下文件
i) properties.xml
該文件定義build.xml所需的屬性值,主要完成通過build.properties 產(chǎn)生
database.properties 文件、定義構(gòu)建工具需要的Classpath屬性等工作。
ii) app-settings.xml
該文件定義設(shè)置應(yīng)用程序狀態(tài)的一些屬性值
iii) build.properties
定義一些全局設(shè)置屬性
Ø build.xml文件具體分析
<!DOCTYPE project [
<!ENTITY properties SYSTEM "properties.xml">
<!ENTITY app-settings SYSTEM "app-settings.xml">
]>
<!—XML DTD定義properties 和app-settings -->
<project name="dudu" basedir="." default="package-web">
<!—導(dǎo)入配置文件properties.xml(其中主要處理classpath,jar文件位置定義)-->
<!-- import project-wide properties (classpath, jar file locations) -->
&properties;
<!—導(dǎo)入配置文件app-settings.xml-->
&app-settings;
<!—定義一些自定義任務(wù),-->
<target name="init" description="defines custom tasks">
……
</target>
<!—建立所需的目錄:build/dudu;build/dudu/WEB-INF;dist/webapps-->
<target name="prepare" depends="build.properties.missing,init"
……
</target>
<!—用定義的名字新建一個項目 -->
<!—使用方法ant new -Dapp.name=NAME -Ddb.name=NAME -->
<!—該任務(wù)會在上層目錄中構(gòu)建自定義名字應(yīng)用程序,內(nèi)容與本目錄中的大致相同-->
<target name="new" depends="clean">
……
</target>
<!—拷貝資源和配置文件:將web/WEB-INF/classes下的ApplicationResources_zh_CN.properties;log4j.properties等配置文件拷貝到build/web/classes下.
拷貝src/dao/packageName/applicationContext-hibernate.xml 到dudu/WEB-INF目錄下,
拷貝src/service/packageName/applicationContext-service.xml到dudu/WEB-INF目錄下.
在此任務(wù)中會進行native2ascii 的轉(zhuǎn)碼工作.
此任務(wù)比較有用,以后可以復(fù)用.
-->
<target name="copy-resources" depends="prepare">
<native2ascii src="http://dev.csdn.net/article/"web/WEB-INF/classes""
dest="${build.dir}/web/classes"
includes="ApplicationResources_zh_CN.properties" encoding="gb2312"/>
……
</target>
<!—拷貝web目錄下footer.jsp; pages/*.jsp;**/classes/**;**/*-resources.xml到
build/dudu/的對應(yīng)目錄下,拷貝時會自動建立相應(yīng)的目錄結(jié)構(gòu)
-->
<target name="copy-web-files" depends="prepare">
……
</target>
<!—拷貝所有頁面與配置文件以形成web應(yīng)用程序-->
<target name="stage-web"
depends="copy-resources,copy-web-files"
description="Calls other targets to gather static resources"/>
<!—利用XDoclet根據(jù)POJO生成對應(yīng)的hiberante映射文件
根據(jù)src/dao中的POJO生成映射文件,存放于build/dao/gen目錄中.
此任務(wù)比較有用,以后可以復(fù)用.
執(zhí)行此任務(wù)之前,需執(zhí)行ant clean刪除build目錄
-->
<target name="hibernatedoclet" depends="prepare"
unless="hibernatedoclet.unnecessary"
description="Generate Hibernate mapping files">
<taskdef name="hibernatedoclet"
classname="xdoclet.modules.hibernate.HibernateDocletTask"
classpat/>
<!-- generate hibernate files -->
<hibernatedoclet
destdir="${build.dir}/dao/gen"
mergedir="metadata/dao"
excludedtags="@version,@author"
addedtags="@xdoclet-generated at ${TODAY}"
force="${xdoclet.force}">
<fileset dir="src/dao"/>
<hibernate validatexml="true" version="2.0"/>
</hibernatedoclet>
</target>
<!—編譯dao模塊-->
<target name="compile-dao" depends="hibernatedoclet">
……
</target>
<!—將編譯好的dao模塊打包成對應(yīng)的jar文件,生成的jar文件存放在dist目錄下-->
<target name="package-dao" depends="prepare,compile-dao"
……
</target>
<!—編譯service模塊-->
<target name="compile-service" depends="package-dao"
description="Compile service module">
<antcall target="compile-module" inheritAll="true">
<param name="module" value="service"/>
<reference refid="service.compile.classpath"
torefid="http://dev.csdn.net/article/"compile.classpath"/>
<reference refid="service.test.classpath"
torefid="test.classpath"/>
</antcall>
</target>
<!—打包service模塊,日后開發(fā)自定義的模塊可以參考此任務(wù)編譯和打包-->
<target name="package-service" depends="compile-service">
<jar destfile="${dist.dir}/${webapp.name}-service.jar">
<manifest>
<attribute name="Class-Path"
value="${webapp.name}-dao.jar ${webapp.name}-service.jar"/>
</manifest>
<fileset dir="${build.dir}/service/classes" includes="**/*.class"/>
</jar>
</target>
<!—根據(jù)POJO生成Struts 的ActionForms,生成的FormBean存放在build/web/gen下
此任務(wù)比較有用,以后可以復(fù)用
-->
<target name="gen-forms" depends="prepare" unless="webdoclet.uptodate">
……
</target>
<!—利用webdoclet生成web.xml;struts-config.xml等web應(yīng)用程序配置文件
生成的文件放在build/dudu/WEB-INF下
此任務(wù)比較有用,以后可以復(fù)用
-->
<target name="webdoclet" depends="compile-web"
unless="webdoclet.unnecessary">
……
</target>
<!—打包web模塊,處理build/dudu/WEB-INF/web.xml文件;
根據(jù)metadata/conf/tomcat-context.xml 產(chǎn)生dist/webapps/context.xml;
改變build/dudu/WEB-INF/ applicationContext-hibernate.xml中的路徑,以便
dudu-dao.jar可以找到.hbm文件,
拷貝src/web/**/*.properties和*.xml文件到build/web/classes中,
將build/dudu下的文件打包成dist/dudu.war,其中包括build/web/classes中的文件
生成WEB-INF/classes中的文件,
dist/*.jar生成WEB-INF/lib中的文件
此任務(wù)比較有用,以后可以復(fù)用
-->
<target name="package-web" depends="compile-web,jsp-2">
……
</target>
<!—所有編譯任務(wù)都要調(diào)用的基本編譯,接收其他任務(wù)的參數(shù),編譯相應(yīng)的模塊
輸入?yún)?shù):待編譯模塊名稱,編譯所需的路徑.
編譯后的文件存放于build/moduleName/classes中.
此任務(wù)比較有用,以后可以復(fù)用
-->
<target name="compile-module">
<!-- Inputs: module, compile.classpath, test.classpath -->
<echo level="info">Compiling ${module}...</echo>
<mkdir dir="${build.dir}/${module}/classes"/>
<mkdir dir="${test.dir}/${module}/classes"/>
<property name="excludes" value=""/>
<property name="additional.src.dirs" value=""/>
<javac srcdir="src/${module};${additional.src.dirs}"
destdir="${build.dir}/${module}/classes"
debug="${compile.debug}"
deprecation="${compile.deprecation}"
optimize="${compile.optimize}"
classpat/>
</target>
<!—
將數(shù)據(jù)庫中的數(shù)據(jù)導(dǎo)出為xml文件
此任務(wù)比較有用,以后可以復(fù)用
-->
<target name="db-export" depends="prepare"
……
</target>
<!—利用dbunit工具,根據(jù)metadata/sql/sample-data.xml 填充數(shù)據(jù)庫表記錄-->
<target name="db-load" depends="prepare"
……
</target>
<!—
根據(jù)metadata/sql/${database.type}-create.sql中的sql語句建立數(shù)據(jù)庫和用戶
此任務(wù)比較有用,以后可以復(fù)用
-->
<target name="db-create" depends="init">
……
</target>
<!—
根據(jù)映射hibernate映射文件生成數(shù)據(jù)庫表
此任務(wù)比較有用,以后可以復(fù)用
-->
<target name="db-prepare" depends="clean,package-dao"
description="creates database tables">
<taskdef name="schemaexport"
classname="net.sf.hibernate.tool.hbm2ddl.SchemaExportTask">
<classpath>
<path refid="xdoclet.classpath"/>
<path refid="hibernate.classpath"/>
</classpath>
</taskdef>
<antcall target="generate.database.properties"/>
<schemaexport quiet="no" text="no" drop="no" delimiter=";"
properties="database.properties" output="create-tables.sql">
<fileset dir="${build.dir}/dao/gen" includes="**/*.hbm.xml"/>
</schemaexport>
</target>
<!—刪除數(shù)據(jù)庫中的表-->
<target name="db-drop" depends="hibernatedoclet">
……
</target>
<!—刪除目錄-->
<target name="clean" description="Removes build artifacts">
<delete dir="${build.dir}"/>
</target>
<!—生成JavaDoc,生成文件存放在E:\dudu\build\docs\api -->
<target name="javadoc" depends="java2html">
……
</target>
<!—
將build/dudu下的文件拷貝到tomcat-home/webapps/dudu/,包括完整的目錄和所有的
Jsp文件,但不包括jar文件和WEB-INF/classes中的文件,也不包括web.xml文件和
spring的配置文件這些文件不在build/dudu目錄下.
-->
<target name="deploy-web" depends="stage-web" if="tomcat.home">
……
</target>
<!—部署web應(yīng)用,并解包war文件-->
<target name="deploy" depends="package-web" if="tomcat.home">
<unwar src="http://dev.csdn.net/article/"${webapp.dist}/${webapp.war}"
dest="${tomcat.home}/webapps/${webapp.name}"/>
</target>
<!—刪除web應(yīng)用 -->
<target name="undeploy" if="tomcat.home">
<delete file="${tomcat.home}/webapps/${webapp.war}"/>
<delete dir="${tomcat.home}/webapps/${webapp.name}"/>
</target>
<!—將war文件拷貝到tomcat容器-->
<target name="deploy-war" depends="package-web" if="tomcat.home">
<copy file="${webapp.dist}/${webapp.war}"
todir="${tomcat.home}/webapps"/>
</target>
</project>
Ø properties.xml文件分析
該文件定義build.xml所需的屬性值。
<!—導(dǎo)入lib.properties 文件,其中包括了構(gòu)建時所需的jar文件定義-->
<property name="lib.dir" location="lib"/>
<property file="${lib.dir}/lib.properties"/>
<!—加載build.properties 文件-->
<property file="build.properties"/>
<!—加載運行unit tests所需的tomcat配置屬性文件 -->
<property file="tomcat.properties"/>
<!-- webapp.name被定義在build.properties,在此做檢查看該屬性是否被定義-->
<target name="build.properties.missing" unless="webapp.name">
<fail message="missing build.properties file in current directory or in ${user.home}"/>
</target>
<!—如果build.properties 不存在時,定義數(shù)據(jù)庫的基本配置-->
<property name="database.jar" location="${mysql.jar}"/>
……
<!—定義建立數(shù)據(jù)庫時所用到的database URL,它被用在db-create任務(wù)中-->
<property name="database.url"
value="jdbc:mysql://localhost/jpetstore"/>
……
<!—通過build.properties 產(chǎn)生database.properties 文件-->
<!—由于build.properties 中的屬性被注釋了,所以這些屬性會從properties.xml中取得-->
<target name="generate.database.properties">
<propertyfile comment="Hibernate Configuration for JUnit tests"
file="${basedir}/database.properties">
<entry key="hibernate.dialect" value="${hibernate.dialect}"/>
……
</propertyfile>
</target>
<!—初始化屬性值-->
<property environment="env"/>
<property name="env.COMPUTERNAME" value="${env.HOSTNAME}"/>
……
<!—properties.xml文件的主要工作:定義構(gòu)建工具需要的Classpath屬性-->
<path id="xdoclet.classpath">
……
</path>
<path id="hibernate.classpath">
……
</path>
<path id="dao.compile.classpath">
……
</path>
<path id="dao.test.classpath">
……
</path>
<path id="service.compile.classpath">
……
</path>
<path id="service.test.classpath">
……
</path>
<path id="web.compile.classpath">
……
</path>
<path id="web.test.classpath">
……
</path>
Ø app-settings.xml文件分析
該文件定義設(shè)置應(yīng)用程序狀態(tài)的一些屬性值
<property name="encrypt.password" value="true"/>
<property name="encrypt.algorithm" value="SHA"/>
<property name="secure.login" value="false"/>
<property name="secure.application" value="false"/>
<property name="rememberMe.enabled" value="true"/>
Øbuild.properties文件分析
build.compiler=modern
webapp.name=dudu
webapp.version=1.7
webapp.war=${webapp.name}.war
compile.deprecation=true
build.dir=${basedir}/build
dist.dir=${basedir}/dist
test.dir=${build.dir}/test
javac.debug=true
junit.fork=true
xdoclet.force=false
error.mailTo=junk@raibledesigns.com
error.mailHost=localhost
error.server=localhost
ftp.server=upload.sourceforge.net
ftp.remoteDir=incoming
ftp.user=anonymous
Ø tomcat.properties文件分析
tomcat.server=localhost
tomcat.manager.url=http://${tomcat.server}:8080/manager
tomcat.username=admin
tomcat.password=admin