● AppFuse的ant構建使用非常復雜,功能也非常強大,怎樣能夠充分的利用它,為我所用,在今后的開發中大幅度的減輕開發量,顧做此詳細研究(其中不包括測試任務)。 build.xml的正常運行依賴以下文件i) propertie..
● AppFuse的ant構建使用非常復雜,功能也非常強大,
怎樣能夠充分的利用它,為我所用,在今后的開發中大幅度的減輕開發量,顧做此詳細研究(其中不包括測試任務)。
Ø build.xml的正常運行依賴以下文件
i) properties.xml
該文件定義build.xml所需的屬性值,主要完成通過build.properties 產生
database.properties 文件、定義構建工具需要的Classpath屬性等工作。
ii) app-settings.xml
該文件定義設置應用程序狀態的一些屬性值
iii) build.properties
定義一些全局設置屬性
Ø 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">
<!—導入配置文件properties.xml(其中主要處理classpath,jar文件位置定義)-->
<!-- import project-wide properties (classpath, jar file locations) -->
&properties;
<!—導入配置文件app-settings.xml-->
&app-settings;
<!—定義一些自定義任務,-->
<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 -->
<!—該任務會在上層目錄中構建自定義名字應用程序,內容與本目錄中的大致相同-->
<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目錄下.
在此任務中會進行native2ascii 的轉碼工作.
此任務比較有用,以后可以復用.
-->
<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/的對應目錄下,拷貝時會自動建立相應的目錄結構
-->
<target name="copy-web-files" depends="prepare">
……
</target>
<!—拷貝所有頁面與配置文件以形成web應用程序-->
<target name="stage-web"
depends="copy-resources,copy-web-files"
description="Calls other targets to gather static resources"/>
<!—利用XDoclet根據POJO生成對應的hiberante映射文件
根據src/dao中的POJO生成映射文件,存放于build/dao/gen目錄中.
此任務比較有用,以后可以復用.
執行此任務之前,需執行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模塊打包成對應的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模塊,日后開發自定義的模塊可以參考此任務編譯和打包-->
<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>
<!—根據POJO生成Struts 的ActionForms,生成的FormBean存放在build/web/gen下
此任務比較有用,以后可以復用
-->
<target name="gen-forms" depends="prepare" unless="webdoclet.uptodate">
……
</target>
<!—利用webdoclet生成web.xml;struts-config.xml等web應用程序配置文件
生成的文件放在build/dudu/WEB-INF下
此任務比較有用,以后可以復用
-->
<target name="webdoclet" depends="compile-web"
unless="webdoclet.unnecessary">
……
</target>
<!—打包web模塊,處理build/dudu/WEB-INF/web.xml文件;
根據metadata/conf/tomcat-context.xml 產生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中的文件
此任務比較有用,以后可以復用
-->
<target name="package-web" depends="compile-web,jsp-2">
……
</target>
<!—所有編譯任務都要調用的基本編譯,接收其他任務的參數,編譯相應的模塊
輸入參數:待編譯模塊名稱,編譯所需的路徑.
編譯后的文件存放于build/moduleName/classes中.
此任務比較有用,以后可以復用
-->
<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>
<!—
將數據庫中的數據導出為xml文件
此任務比較有用,以后可以復用
-->
<target name="db-export" depends="prepare"
……
</target>
<!—利用dbunit工具,根據metadata/sql/sample-data.xml 填充數據庫表記錄-->
<target name="db-load" depends="prepare"
……
</target>
<!—
根據metadata/sql/${database.type}-create.sql中的sql語句建立數據庫和用戶
此任務比較有用,以后可以復用
-->
<target name="db-create" depends="init">
……
</target>
<!—
根據映射hibernate映射文件生成數據庫表
此任務比較有用,以后可以復用
-->
<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>
<!—刪除數據庫中的表-->
<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應用,并解包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應用 -->
<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所需的屬性值。
<!—導入lib.properties 文件,其中包括了構建時所需的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 不存在時,定義數據庫的基本配置-->
<property name="database.jar" location="${mysql.jar}"/>
……
<!—定義建立數據庫時所用到的database URL,它被用在db-create任務中-->
<property name="database.url"
value="jdbc:mysql://localhost/jpetstore"/>
……
<!—通過build.properties 產生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文件的主要工作:定義構建工具需要的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文件分析
該文件定義設置應用程序狀態的一些屬性值
<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