引用地址:http://www.easydone.cn/014/200604110005145285.htm
配合ant开发web项目
应用相关说明:
1、ant版本:apache-ant-1.6.1
2、eclipse版本:eclipse-SDK-3.0.1-win32
3、j2sdk1.4.2_08
4、jakarta-tomcat-5.0.19
5、mysql
目录结构如下:
ant具体配置如下
1、"Easydone"project"demo"bin目录下新建build.xml
内容如下:
<?xml version="1.0" encoding="GB2312" ?>
<project name="Demo-Home" basedir="." default="usage">
<property name="app.name" ="demo"/>
<property name="app.dir" location="../WebRoot"/>
<property name="webserver.home" location="../../../platform/jakarta-tomcat-5.0.19"/>
<property name="webserver.deploy" location="${webserver.home}/webapps/"/>
<property name="src.dir" location="../src"/>
<property name="build.dir" location="${app.dir}/WEB-INF/classes"/>
<property name="lib.dir" location="${app.dir}/WEB-INF/lib"/>
<property name="dist.dir" location="../dist"/>
<property name="properties.dir" location="${src.dir}/com/easydone/struts/ApplicationResources.properties"/>
<property name="properties_zh.dir" location="${build.dir}/com/easydone/struts/ApplicationResources_zh.properties"/>
<property name="doc.dir" ="../api"/>
<property name="packages" ="com.easydone.*"/>
<echo>+--------------------------------------------------+</echo>
<echo>| |</echo>
<echo>| R U N N I N G A N T REPOSITORY |</echo>
<echo>| |</echo>
<echo>+--------------------------------------------------+</echo>
<!---Help Information-->
<target name="usage" >
<echo message=""/>
<echo message="-------------------------------------------------------------"/>
<echo message="操作说明:"/>
<echo message="首先将您编写的java文件拷贝至${src.dir}目录下,按先后顺序执行以下命令既可。"/>
<echo message="第一步:compile 命令编译*.java"/>
<echo message="第二步:deploy 命令生成${app.name}.jar文件"/>
<echo message="第三步:doc 命令生成详细javadoc文档"/>
<echo message="每一步操作都能看到 BUILD SUCCESSFUL提示后,表示您操作成功!"/>
<echo message="-------------------------------------------------------------"/>
<echo message=""/>
</target>
<!-- =================================================================== -->
<!-- 设置应用环境变量 -->
<!-- =================================================================== -->
<path id="compile.classpath">
<pathelement path ="${build.dir}"/>
<fileset dir="${lib.dir}">
<include name="**/*.jar"/>
</fileset>
</path>
<!-- =================================================================== -->
<!-- 格式化中文资源包,解决国际化中文问题 -->
<!-- =================================================================== -->
<target name="checkToZh">
<uptodate property="toZh.notRequired"
srcfile="${properties.dir}"
targetfile="${properties_zh.dir}"/>
</target>
<target name="toZh" depends="checkToZh" unless="toZh.notRequired">
<delete file="${properties_zh.dir}"/>
<!-- native2ascii struts.properties -->
<exec executable="native2ascii">
<arg line=" ${properties.dir} ${properties_zh.dir}"/>
</exec>
<echo message="toZh performed!"/>
</target>
<!-- =================================================================== -->
<!-- 初始化创建相关应用目录 -->
<!-- =================================================================== -->
<target name="prepare" deion="create build,dist files">
<mkdir dir="${build.dir}"/>
<mkdir dir="${dist.dir}"/>
</target>
<!-- =================================================================== -->
<!-- 初始化应用程序,删除相关目录 -->
<!-- =================================================================== -->
<target name="clean" deion="Delete build files">
<delete dir="${build.dir}"/>
<delete dir="${dist.dir}"/>
</target>
<!-- =================================================================== -->
<!-- compile 命令,执行javac编译命令 -->
<!-- =================================================================== -->
<target name="compile" depends="prepare">
<javac srcdir="${src.dir}" destdir="${build.dir}" debug="on">
<classpath refid="compile.classpath"/>
</javac>
</target>
<!-- =================================================================== -->
<!-- update命令,编译及处理国际化中文资源文件 -->
<!-- =================================================================== -->
<target name="update" depends="compile,toZh">
<!--copy classes-->
<copy todir="${webserver.deploy}/${app.name}/WEB-INF/classes" includeEmptyDirs="no">
<fileset dir="${build.dir}"/>
</copy>
<!--copy pages-->
<copy todir="${webserver.deploy}/${app.name}" includeEmptyDirs="no">
<fileset dir="${app.dir}"/>
</copy>
<!-- copy libs-->
<copy todir="${webserver.deploy}/${app.name}/WEB-INF/lib" includeEmptyDirs="no">
<fileset dir="${lib.dir}"/>
</copy>
</target>
<!-- =================================================================== -->
<!-- 部署应用程序,依赖于 compile命令clean,prepare,compile,dist -->
<!-- =================================================================== -->
<delete dir="${webserver.deploy}/${app.name}.war"/>
<delete dir="${webserver.deploy}/${app.name}"/>
<copy todir="${webserver.deploy}" includeEmptyDirs="no">
<fileset dir="${dist.dir}"/>
</copy>
</target>
<!-- =================================================================== -->
<!-- 部署应用程序,将其打为mshome.war包,依赖于 compile命令 -->
<!-- =================================================================== -->
<target name="dist" depends="compile">
<echo>building war...</echo>
<war warfile="${dist.dir}/${app.name}.war"
webxml="${app.dir}/WEB-INF/web.xml">
<fileset dir="${app.dir}"/>
<classes dir="${build.dir}"/>
<classes dir="${lib.dir}">
<include name="*.propertries"/>
</classes>
<lib dir="${lib.dir}">
<include name="*.jar"/>
</lib>
</war>
</target>
<!-- =================================================================== -->
<!-- 生成说明文档(api) -->
<!-- =================================================================== -->
<target name="javadoc" depends="usage,compile">
<delete dir="${doc.dir}"/>
<mkdir dir="${doc.dir}"/>
<javadoc packagenames="${packages}"
sourcepath="${src.dir}"
destdir="${doc.dir}"
author="true"
version="true"
use="true"
splitindex="true"
windowtitle="${app.name} API"
doctitle="${app.name}">
<classpath refid="compile.classpath"/>
</javadoc>
</target>
<!-- =================================================================== -->
<!-- 执行一个数据库操作,其中demo为您的数据库名,可测试相关jdbc连接
查询语句为bin目录下的db.sql -->
<!-- =================================================================== -->
<target name = "mysqlInit">
<echo>ANT 操作数据库开始.....</echo>
<sql
driver = "org.gjt.mm.mysql.Driver"
url = "jdbc:mysql://localhost:3306/demo"
userid = "root"
password = "*******"
src = "db.sql"
print="yes"
="continue"
/>
</target>
<!--END-->
</project>
2、在"Easydone"project"demo"bin目录下新建setEnv.bat
dos处理脚本,主要为设置环境变量(这样做您无需手工配置环境变量,将整个工程移动到任何目录都可以使用),内容如下:
set ANT_HOME=../../../platform/apache-ant-1.6.1
set JAVA_HOME=../../../platform/j2sdk1.4.2_08
set CLASSPATH=%JAVA_HOME%/lib/toos.jar;%JAVA_HOME%/lib/dt.jar
set CATALINA_HOME=../../../platform/jakarta-tomcat-5.0.19
set PATH=%ANT_HOME%/bin;%CATALINA_HOME%/BIN;%JAVA_HOME%/bin;%CATALINA_HOME%/BIN
3、服务控制脚本:
tomcat启动服务:
在"Easydone"project"demo"bin目录下新建 run.cmd:
call setEnv.bat
startup
tomcat停止服务:
在"Easydone"project"demo"bin目录下新建 down.cmd
call setEnv.bat
shutdown
更新脚本:
在"Easydone"project"demo"bin目录下新建 update.cmd
call setEnv.bat
ant -buildfile build.xml update
部署脚本:
在"Easydone"project"demo"bin目录下新建 deploy.cmd
call setEnv.bat
ant -buildfile build.xml deploy
java doc 脚本:
在"Easydone"project"demo"bin目录下新建 doc.cmd
call setEnv.bat
ant -buildfile build.xml javadoc
剩下的都在build.xml中,根据你的需要自己定义脚本程序就可以了
很简单,这样可控制你的应用。