首先,假设我们的项目的结构如下 ${project.root}
|
+-- build/ <-- WAR/EAR档的目录
+-- classes/ <-- 编译的输出目录,与JBuilder兼容
+-- src/ <-- Java源文件目录
+-- web/ <-- WEB Content 目录
| |
| +--pages/ <-- JSP 文件目录
| +--WEB-INF/ <-- web.xml 等配置文件
+-- ejb <-- EJB Content 目录
| |
| +---INF/ <-- EJB 配置文件
+-- ear <-- EAR Content 目录
| |
| +---INF/ <-- EAR 配置文件
|-- lib <-- 项目所要用到的library
接下来第一步,就是要先定义好一些全局的参数,以便以后更改起来方便,风格
大家自己选择好了,我用的是如下的方式:
(1) 给出项目的目录:
<!--// project directories //--><property name="dir.project"
="D:/Code/MyProject"/><property name="dir.project.lib"
="${dir.project}/lib"/><property name="dir.project.src"
="${dir.project}/src"/><property name="dir.project.web_module"
="${dir.project}/web"/><property name="dir.project.ejb_module"
="${dir.project}/ejb"/><property name="dir.project.ear_module"
="${dir.project}/ear"/><!--// compile, build and deploy
directories //--><property name="dir.project.compile"
="${dir.project}/classes"/><property
name="dir.project.compile.jspcache"
="${dir.project.compile}/jspcache"/><property
name="dir.project.build" ="${dir.project}/build"/><!--// J2EE
Container //--><property name="dir.appserver.tomcat.home"
="D:/J2EE_HOME/AppServer/Tomcat"/><property
name="dir.appserver.oc4j.j2ee.home"
="D:/J2EE_HOME/AppServer/OC4J/j2ee/home"/>
(2) 定义Java Source的编译选项
<!--//
Java compiler options //--> <property name="opt.compile.source"
="1.4"/><property name="opt.compile.target"
="1.4"/><property name="opt.compile.encoding"
="UTF-8"/><property name="opt.compile.deprecation"
="on"/><property name="opt.compile.debug" ="on"/><property
name="opt.compile.optimize" ="off"/>
(3) 如果需要 import EJB library 的话,给出下面的定义,后面会用到
<!--// EJB imported resource //--><property name="import.ejb.report.server" ="${dir.project.lib}/Appwork.jar"/>
(4) 给出部署WAR/EAR文件的名称:
<!--// Deployment //--><property name="deploy.file.ejb_module"
="MyApp_EJB.jar" /><property name="deploy.file.web_module"
="MyApp_WEB.war" /><property name="deploy.file.ear_module"
="MyApp.ear" />
(5) 给出CLASSPATH的定义,引入相关的Library和项目编译生成的Java Classes
<path id="CLASSPATH"><!--// Tomcat Libraries
//--><fileset dir="${dir.appserver.tomcat.home}/bin">
<include name="*.jar"/></fileset> <fileset
dir="${dir.appserver.tomcat.home}/server/lib"> <include
name="*.jar"/> </fileset> <fileset
dir="${dir.appserver.tomcat.home}/common/lib"> <include
name="*.jar"/> </fileset> <!--// Depended Libraries
//--><pathelement
location="${dir.project.lib}/abc.jar"/><pathelement
location="${dir.project.lib}/AppServer.jar"/><pathelement
location="${dir.project.lib}/AppServiceClient.jar"/><pathelement
location="${dir.project.lib}/AppUtil.jar"/><pathelement
location="${dir.project.lib}/xport.jar"/><pathelement
location="${dir.project.lib}/jaxen-full.jar"/><pathelement
location="${dir.project.lib}/jstl.jar"/><pathelement
location="${dir.project.lib}/log4j-1.2.13.jar"/><pathelement
location="${dir.project.lib}/ojdbc14.jar"/><pathelement
location="${dir.project.lib}/saxpath.jar"/><pathelement
location="${dir.project.lib}/standard.jar"/><pathelement
location="${dir.project.lib}/taglibs-log.jar"/><!--// Project
classes //--><pathelement
location="${dir.project.compile}"/></path>
(6) 基本的clean和init的task
<target name="clean"><delete
dir="${dir.project.build}"/><delete
dir="${dir.project.compile.jspcache}"/><delete
dir="${dir.project.compile}"/><delete
dir="${dir.project.web_module}/WEB-INF/classes"/><delete
dir="${dir.project.web_module}/WEB-INF/lib"/></target><target
name="init"><mkdir dir="${dir.project.compile}"/><mkdir
dir="${dir.project.compile.jspcache}"/><mkdir
dir="${dir.project.web_module}/WEB-INF/classes"/><mkdir
dir="${dir.project.web_module}/WEB-INF/lib"/><mkdir
dir="${dir.project.build}"/></target>
(7) 编译Java Source
<target
name="src_compile" depends="init"><!--// compile the Java source
//--><javac srcdir="${dir.project.src}"
destdir="${dir.project.compile}" encoding="${opt.compile.encoding}"
source="${opt.compile.source}" target="${opt.compile.source}"
deprecation="${opt.compile.deprecation}"debug="${opt.compile.debug}"
optimize="{opt.compile.optimize}" ><classpath
refid="CLASSPATH"/><include
name="**/*.java"/></javac><!--// copy the classpath
resources //--><copy
todir="${dir.project.compile}"><fileset
dir="${dir.project.src}"><exclude
name="**/*.java"/></fileset></copy> </target>
(8) 预编译JSP文件,可选择基于Tomcat或是Oracle Application Server
<!--// for Tomcat //--> <target name="jsp_src_generate"
depends="src_compile"><taskdef classname="org.apache.jasper.JspC"
name="jasper2"><classpath refid="CLASSPATH"/>
</taskdef><jasper2 validateXml="false"
verbose="9"uriroot="${dir.project.web_module}"
outputDir="${dir.project.compile.jspcache}"
webXmlFragment="${dir.project.compile.jspcache}/generated-web.xml"
/> </target><target name="jsp_src_compile"><javac
srcdir="${dir.project.compile.jspcache}"
destdir="${dir.project.compile.jspcache}"
encoding="${opt.compile.encoding}" source="${opt.compile.source}"
target="${opt.compile.source}"
deprecation="${opt.compile.deprecation}"debug="${opt.compile.debug}"
optimize="{opt.compile.optimize}" ><classpath
refid="CLASSPATH"/><include
name="**"/></javac></target><target
name="jsp_precompile" depends="src_compile, jsp_src_generate,
jsp_src_compile" />
(9) 开始进行compile,make 和 build,并生成部署档案
<target name="compile" depends="src_compile, jsp_precompile"
/><!--// Build WAR file //--><target name="make.web_module"
depends="compile"><war
destfile="${dir.project.build}/${deploy.file.web_module}"
webxml="${dir.project.web_module}/WEB-INF/web.xml"><fileset
dir="${dir.project.web_module}" ><include
name="**"/><exclude name="WEB-INF/classes/**"/><exclude
name="WEB-INF/lib/**"/></fileset><classes
dir="${dir.project.compile}"><include name="**"/><exclude
name="project/package/trx/**"/><exclude
name="jspcache/**"/></classes><lib
dir="${dir.project.lib}"><include
name="AppUtil.jar"/><include
name="AppClient.jar"/></lib></war></target><!--//
Build EJB Jar file //--><target name="make.ejb_module"
depends="src_compile"><jar
destfile="${dir.project.build}/${deploy.file.ejb_module}"><zipgroupfileset
dir="${dir.project.lib}"><include
name="AppServer.jar"/></zipgroupfileset><fileset
dir="${dir.project.ejb_module}"><include
name="**"/></fileset><fileset
dir="${dir.project.compile}"><include name="**"/><exclude
name="project/package/controller/**"/><exclude
name="jspcache/**"/></fileset></jar></target><!--//
Build EAR file //--><target name="make.ear_module"
depends="compile, make.web_module, make.ejb_module"><ear
destfile="${dir.project.build}/${deploy.file.ear_module}"
appxml="${dir.project.ear_module}/-INF/application.xml"><fileset
dir="${dir.project.build}"><include
name="${deploy.file.web_module}"/><include
name="${deploy.file.ejb_module}"/></fileset></ear></target><target
name="make" depends="init, compile, make.web_module, make.ejb_module,
make.ear_module" /><target name="build" depends="clean, init,
make" />