整理一个ANT在J2EE项目中的应用,含预编译JSP和打包WAR/EAR文件!

首先,假设我们的项目的结构如下,相同的目录大家可以自行扩展!
${project.root}
  |
  +-- build/           <-- WAR/EAR档的目录
  +-- classes/         <-- 编译的输出目录,与JBuilder兼容
  +-- src/             <-- Java源文件目录
  +-- web/             <-- WEB Content 目录
  |    |
  |    +--pages/       <-- JSP 文件目录
  |    +--WEB-INF/     <-- web.xml 等配置文件
  +-- ejb              <-- EJB Content 目录
  |    |
  |    +--META-INF/    <-- EJB 配置文件
  +-- ear              <-- EAR Content 目录
  |    |
  |    +--META-INF/    <-- EAR 配置文件
  |-- lib              <-- 项目所要用到的library

接下来第一步,就是要先定义好一些全局的参数,以便以后更改起来方便,风格
大家自己选择好了,我用的
是如下的方式:

(1) 给出项目的目录:

     
     
<!--// project directories //-->
<property name="dir.project" value="D:/Code/MyProject"/>
<property name="dir.project.lib" value="${dir.project}/lib"/>
<property name="dir.project.src" value="${dir.project}/src"/>
<property name="dir.project.web_module" value="${dir.project}/web"/>
<property name="dir.project.ejb_module" value="${dir.project}/ejb"/>
<property name="dir.project.ear_module" value="${dir.project}/ear"/>

<!--// compile, build and deploy directories //-->
<property name="dir.project.compile" value="${dir.project}/classes"/>
<property name="dir.project.compile.jspcache" value="${dir.project.compile}/jspcache"/>
<property name="dir.project.build" value="${dir.project}/build"/>

<!--// J2EE Container //-->
<property name="dir.appserver.tomcat.home" value="D:/J2EE_HOME/AppServer/Tomcat"/>
<property name="dir.appserver.oc4j.j2ee.home" value="D:/J2EE_HOME/AppServer/OC4J/j2ee/home"/>

(2) 定义Java Source的编译选项
 
<!--
// Java compiler options //-->
<property name="opt.compile.source" value="1.4"/>
<property name="opt.compile.target" value="1.4"/>
<property name="opt.compile.encoding" value="UTF-8"/>
<property name="opt.compile.deprecation" value="on"/>
<property name="opt.compile.debug" value="on"/>
<property name="opt.compile.optimize" value="off"/>

(3) 如果需要 import EJB library 的话,给出下面的定义,后面会用到
  
<!--
// EJB imported resource //-->
<property name="import.ejb.report.server" value="${dir.project.lib}/AppFramework.jar"/>

(4) 给出部署WAR/EAR文件的名称:

     
     

<!--// Deployment //-->
<property name="deploy.file.ejb_module" value="MyApp_EJB.jar" />
<property name="deploy.file.web_module" value="MyApp_WEB.war" />
<property name="deploy.file.ear_module" value="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>
<!--// OC4J Libraries
<fileset dir="${dir.appserver.oc4j.j2ee.home}">
<include name="*.jar"/>
</fileset>
<fileset dir="${dir.appserver.oc4j.j2ee.home}/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.target}"
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>

<!--// for OC4J 9.0.4
<target name="jsp_src_generate" depends="src_compile">
<java classname="oracle.jsp.tools.Jspc" fork="yes" failοnerrοr="true">
<classpath refid="CLASSPATH"/>
<arg line="-extend com.orionserver.http.OrionHttpJspPage"/>
<arg line="-srcdir ${dir.project.compile.jspcache}"/>
<arg line="-appRoot ${dir.project.web_module}"/>
<arg line="-noCompile"/>
<arg line="-debug"/>
<arg line="-verbose"/>
<arg line="${dir.project.web}/*.jsp"/>
</java>
</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.target
}"
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}/META-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" />

好了, 把上面的内容综合起来, 就可以用ant build命令来编译了!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值