Ant编译打包项目
Blog项目对应的文件目录树如下:
blog:.
├─demo —— demo 目录
├─sql —— sql脚本目录
├─src —— Java源文件目录
├─test —— 测试文件目录
└─web —— web存放目录
├─admin
├─common
├─includes
├─scripts
├─system
├─themes
├─UserFiles
└─WEB-INF —— j2ee规范目录
├─classes —— 编译路径
├─config
└─lib —— 类库目录
Ant对应的配置:
<?xml version="1.0"?>
<project name="ntsky_blog" default="war" basedir=".">
<!-- set global properties for this build -->
<property name="product" value="ntsky_blog" />
<property name="version" value="1.0" />
<property name="year" value="2007" />
<property name="author" value="ntsky" />
<echo message="----------- ${product} ${version} [${year}] [${author}] ------------" />
<property name="name" value="ntsky_blog" />
<property name="app.dir" value="." />
<property name="web.dir" value="${app.dir}/web" />
<property name="src.dir" value="${app.dir}/src" />
<property name="lib.dir" value="${web.dir}/WEB-INF/lib" />
<property name="jar.name" value="ntsky_blog.jar" />
<property name="tomcat.home" value="/usr/local/tomcat" />
<property name="war.dir" value="${app.dir}/war" />
<property name="build.dir" value="${web.dir}/WEB-INF/classes" />
<echo message="" />
<echo message="ntsky_blog build file" />
<echo message="------------------------------------------------------" />
<echo message="" />
<echo message="Available targets are:" />
<echo message="" />
<echo message="clean --> Clean output dirs" />
<echo message="build --> Compile main Java sources and copy libraries" />
<echo message="war --> Build the web application archive" />
<echo message="all --> Clean, build, docs, warfile, tests" />
<echo message="" />
<!-- JAVA CLASSPATH -->
<path id="classpath">
<fileset dir="${lib.dir}">
<include name="**/*.jar" />
<include name="**/*.zip" />
</fileset>
</path>
<path id="jsp:classpath">
<!-- Tomcat 5.0 -->
<fileset dir="${tomcat.home}/server/lib">
<include name="*.jar" />
</fileset>
<fileset dir="${tomcat.home}/common/lib">
<include name="*.jar" />
</fileset>
<fileset dir="${tomcat.home}/common/bin">
<include name="*.jar" />
</fileset>
<!--
Tomcat 6.0
<fileset dir="${tomcat.home}/lib">
<include name="*.jar" />
</fileset>
<fileset dir="${tomcat.home}/bin">
<include name="*.jar" />
</fileset>
-->
</path>
<target name="clean" description="Clean all build products">
<delete dir="${build.dir}" quiet="true" />
<delete file="${war.dir}/${name}.war" />
</target>
<target name="compile" depends="clean" description="Compile application">
<echo>Compiled the source.</echo>
<mkdir dir="${build.dir}" />
<javac srcdir="${src.dir}" destdir="${build.dir}" debug="on" encoding="UTF-8">
<include name="**/*.java" />
<classpath refid="classpath" />
</javac>
</target>
<target name="jar" depends="compile">
<jar destfile="${lib.dir}/ntsky_blog.jar" basedir="${build.dir}" includes="**/*.class" >
<fileset dir="${src.dir}">
<include name="**/*.xml" />
<include name="**/*.properties" />
<exclude name="*.properties" />
<exclude name="*.xml" />
</fileset>
</jar>
</target>
<target name="war" depends="compile" description="Build the web application archive">
<delete dir="${build.dir}" quiet="true" />
<copy todir="${build.dir}">
<fileset dir="${src.dir}">
<include name="*.properties" />
<include name="*.xml" />
</fileset>
</copy>
<mkdir dir="${war.dir}" />
<war warfile="${war.dir}/${name}.war" webxml="${web.dir}/WEB-INF/web.xml">
<fileset dir="${web.dir}" />
</war>
</target>
<target name="compile-jsp">
<taskdef classname="org.apache.jasper.JspC" name="jasper">
<classpath refid="jsp:classpath" />
</taskdef>
<mkdir dir="${app.dir}/jspSrc" />
<mkdir dir="${app.dir}/${product}" />
<jasper verbose="0" package="ntsky_blog" outputDir="${app.dir}/jspSrc" uriroot="${app.dir}/WebRoot"/>
<javac srcdir="${app.dir}/jspSrc/ntsky_blog" destdir="${app.dir}" debug="on" encoding="UTF-8">
<include name="**/*.java" />
<exclude name="inc/*.java"/>
<classpath refid="classpath" />
<classpath refid="jsp:classpath" />
</javac>
</target>
<target name="deploy" depends="jar" description="Build the web application archive">
<delete dir="${build.dir}" quiet="true" />
<copy todir="${build.dir}">
<fileset dir="${src.dir}">
<include name="*.properties" />
<include name="*.xml" />
<include name="*.server" />
</fileset>
</copy>
</target>
</project>
说明 :
运行ant -f build.xml war或ant 将生成ntsky_blog.war包,将包丢到tomcat webapps下tomcat就会解包。
ant -f build.xml deploy将执行deploy这个target,将得到blog项目目录,将目录拷贝到tomcat/webapps下也可以运行
发布war包或目录时候都会生成ntsky_blog.jar,jar中包含ibatis对应的xml配置文件,而sturts这类配置文件还是在WEB-INF的classes下。
如果需要可以配置编译jsp的tag(编译jsp省去了tomcat自己编译jsp的步骤,当第一次访问jsp的时候,tomcat自己去编译而感觉速度很慢)
Blog项目对应的文件目录树如下:
blog:.
├─demo —— demo 目录
├─sql —— sql脚本目录
├─src —— Java源文件目录
├─test —— 测试文件目录
└─web —— web存放目录
├─admin
├─common
├─includes
├─scripts
├─system
├─themes
├─UserFiles
└─WEB-INF —— j2ee规范目录
├─classes —— 编译路径
├─config
└─lib —— 类库目录
Ant对应的配置:
<?xml version="1.0"?>
<project name="ntsky_blog" default="war" basedir=".">
<!-- set global properties for this build -->
<property name="product" value="ntsky_blog" />
<property name="version" value="1.0" />
<property name="year" value="2007" />
<property name="author" value="ntsky" />
<echo message="----------- ${product} ${version} [${year}] [${author}] ------------" />
<property name="name" value="ntsky_blog" />
<property name="app.dir" value="." />
<property name="web.dir" value="${app.dir}/web" />
<property name="src.dir" value="${app.dir}/src" />
<property name="lib.dir" value="${web.dir}/WEB-INF/lib" />
<property name="jar.name" value="ntsky_blog.jar" />
<property name="tomcat.home" value="/usr/local/tomcat" />
<property name="war.dir" value="${app.dir}/war" />
<property name="build.dir" value="${web.dir}/WEB-INF/classes" />
<echo message="" />
<echo message="ntsky_blog build file" />
<echo message="------------------------------------------------------" />
<echo message="" />
<echo message="Available targets are:" />
<echo message="" />
<echo message="clean --> Clean output dirs" />
<echo message="build --> Compile main Java sources and copy libraries" />
<echo message="war --> Build the web application archive" />
<echo message="all --> Clean, build, docs, warfile, tests" />
<echo message="" />
<!-- JAVA CLASSPATH -->
<path id="classpath">
<fileset dir="${lib.dir}">
<include name="**/*.jar" />
<include name="**/*.zip" />
</fileset>
</path>
<path id="jsp:classpath">
<!-- Tomcat 5.0 -->
<fileset dir="${tomcat.home}/server/lib">
<include name="*.jar" />
</fileset>
<fileset dir="${tomcat.home}/common/lib">
<include name="*.jar" />
</fileset>
<fileset dir="${tomcat.home}/common/bin">
<include name="*.jar" />
</fileset>
<!--
Tomcat 6.0
<fileset dir="${tomcat.home}/lib">
<include name="*.jar" />
</fileset>
<fileset dir="${tomcat.home}/bin">
<include name="*.jar" />
</fileset>
-->
</path>
<target name="clean" description="Clean all build products">
<delete dir="${build.dir}" quiet="true" />
<delete file="${war.dir}/${name}.war" />
</target>
<target name="compile" depends="clean" description="Compile application">
<echo>Compiled the source.</echo>
<mkdir dir="${build.dir}" />
<javac srcdir="${src.dir}" destdir="${build.dir}" debug="on" encoding="UTF-8">
<include name="**/*.java" />
<classpath refid="classpath" />
</javac>
</target>
<target name="jar" depends="compile">
<jar destfile="${lib.dir}/ntsky_blog.jar" basedir="${build.dir}" includes="**/*.class" >
<fileset dir="${src.dir}">
<include name="**/*.xml" />
<include name="**/*.properties" />
<exclude name="*.properties" />
<exclude name="*.xml" />
</fileset>
</jar>
</target>
<target name="war" depends="compile" description="Build the web application archive">
<delete dir="${build.dir}" quiet="true" />
<copy todir="${build.dir}">
<fileset dir="${src.dir}">
<include name="*.properties" />
<include name="*.xml" />
</fileset>
</copy>
<mkdir dir="${war.dir}" />
<war warfile="${war.dir}/${name}.war" webxml="${web.dir}/WEB-INF/web.xml">
<fileset dir="${web.dir}" />
</war>
</target>
<target name="compile-jsp">
<taskdef classname="org.apache.jasper.JspC" name="jasper">
<classpath refid="jsp:classpath" />
</taskdef>
<mkdir dir="${app.dir}/jspSrc" />
<mkdir dir="${app.dir}/${product}" />
<jasper verbose="0" package="ntsky_blog" outputDir="${app.dir}/jspSrc" uriroot="${app.dir}/WebRoot"/>
<javac srcdir="${app.dir}/jspSrc/ntsky_blog" destdir="${app.dir}" debug="on" encoding="UTF-8">
<include name="**/*.java" />
<exclude name="inc/*.java"/>
<classpath refid="classpath" />
<classpath refid="jsp:classpath" />
</javac>
</target>
<target name="deploy" depends="jar" description="Build the web application archive">
<delete dir="${build.dir}" quiet="true" />
<copy todir="${build.dir}">
<fileset dir="${src.dir}">
<include name="*.properties" />
<include name="*.xml" />
<include name="*.server" />
</fileset>
</copy>
</target>
</project>
说明 :
运行ant -f build.xml war或ant 将生成ntsky_blog.war包,将包丢到tomcat webapps下tomcat就会解包。
ant -f build.xml deploy将执行deploy这个target,将得到blog项目目录,将目录拷贝到tomcat/webapps下也可以运行
发布war包或目录时候都会生成ntsky_blog.jar,jar中包含ibatis对应的xml配置文件,而sturts这类配置文件还是在WEB-INF的classes下。
如果需要可以配置编译jsp的tag(编译jsp省去了tomcat自己编译jsp的步骤,当第一次访问jsp的时候,tomcat自己去编译而感觉速度很慢)