ant打包Web工程

时间:2022-01-23 15:41:15

build.properties文件内容如下:

project        = Tqla
version = 1.1.1

build.compiler = modern
[color=green]#build.compiler = classic
#build.compiler = jikes[/color]

year = 2012
debug = on
optimize = on
deprecation = on

os = windows
[color=green]# os = linux[/color]
author = Jambhala
email = shihuan830619@163.com
url = www.shihuan.com.cn
company = shihuan

build.xml文件内容如下:

<?xml version="1.0" encoding="UTF-8"?>

[color=brown]<!-- build.xml和build.properties文件要放在跟WebRoot平级目录下 -->
<!-- Build file for project -->
<!-- ${basedir}就是TestQuartz工程的名字 -->[/color]
<project basedir="." default="publish" name="TestQuartz">

<property file="${basedir}/build.properties" description="加载ant的properties文件" />

<target name="init" description="设置初始化打war包需要的路径变量">
<property name="name" value="TestQuartz"/>
<property name="src" value="${basedir}/src"/>
<property name="lib" value="${basedir}/WebRoot/WEB-INF/lib"/>
<property name="webapp" value="${basedir}/WebRoot"/>
<property name="packages" value="com.*"/>

<property name="build.src" value="${basedir}/dist/src"/>
<property name="build.class" value="${basedir}/dist/WEB-INF/classes"/>
<property name="buildwar.dest" value="${basedir}/dist"/>
<property name="build.javadocs" value="${basedir}/dist/doc"/>
<property name="jar.dest" value="${basedir}/dist/jar"/>
<property name="war.dest" value="${basedir}/dist/war"/>
<property name="ear.dest" value="${basedir}/dist/ear"/>
<property name="zip.dest" value="${basedir}/dist/zip"/>
<property name="db.dest" value="${basedir}/dist/db"/>
<property name="doc.dest" value="${basedir}/dist/doc"/>
<property name="xls.dest" value="${basedir}/dist/xls"/>

<property name="tomcat.home" value="D:\Tomcat 7.0"/>
[color=brown]<!--
<property name="j2eelib.dir" value="D:\Program Files\MyEclipse 6.5\myeclipse\eclipse\plugins\com.genuitec.eclipse.j2eedt.core_6.5.0.zmyeclipse650200806\data\libraryset\EE_5"/>
-->[/color]

<echo message="----------- ${project} ${version} [${year}] ------------" />
<echo message="----------- ${src} ------------" />
<echo message="----------- ${lib} ------------" />
<echo message="----------- ${webapp} ------------" />
<echo message="----------- ${build.src} ------------" />
<echo message="----------- ${build.class} ------------" />
<echo message="----------- ${buildwar.dest} ------------" />
<echo message="----------- ${build.javadocs} ------------" />
<echo message="----------- ${jar.dest} ------------" />
<echo message="----------- ${war.dest} ------------" />
<echo message="----------- ${tomcat.home} ------------" />

<path id="classpath" description="设置依赖jar文件">
[color=brown]<!--
<fileset dir="${j2eelib.dir}">
<include name="**/*.jar"/>
</fileset>
-->[/color]
<fileset dir="${lib}">
<include name="**/*.jar"/>
<exclude name="**/*.bak"/>
</fileset>
</path>

<filter token="year" value="${year}" />
<filter token="version" value="${version}" />
<filter token="date" value="${TODAY}" />
<filter token="log" value="true" />
<filter token="verbose" value="true" />
</target>

<target name="prepare" depends="init" description="创建打包需要的路径,拷贝源文件到打包路径下">
<mkdir dir="${build.src}"/>
<mkdir dir="${build.class}"/>
<mkdir dir="${buildwar.dest}"/>
<mkdir dir="${jar.dest}"/>
<mkdir dir="${war.dest}"/>
<mkdir dir="${ear.dest}"/>
<mkdir dir="${zip.dest}"/>
<mkdir dir="${db.dest}" />
<mkdir dir="${doc.dest}" />
<mkdir dir="${xls.dest}" />
<copy todir="${build.src}">
<fileset dir="${src}"/>
</copy>
<copy todir="${buildwar.dest}">
<fileset dir="${webapp}"/>
</copy>
</target>

<target name="build" depends="prepare" description="编译java文件,拷贝properties属性配置文件到编译后的路径下">
<javac srcdir="${build.src}" destdir="${build.class}">
<classpath refid="classpath" description="依赖上面path标签设置的jar文件" />
</javac>
<copy todir="${build.class}">
<fileset dir="${build.src}">
<include name="**/*.properties"/>
<exclude name="**/*.bak"/>
</fileset>
</copy>
</target>

<target name="javadoc" depends="build" description="生成 java 的 API 帮助文档">
[color=brown]<!--
<mkdir dir="${build.javadocs}"/>
<javadoc packagenames="${packages}" sourcepath="${build.src}" destdir="${build.javadocs}" author="true" version="true" use="true" splitindex="true" windowtitle="${name} API" doctitle="${name}">
<classpath refid="classpath"/>
</javadoc>
-->[/color]
</target>

<target name="antjar" depends="build" description="把部分java文件打成可运行jar包">
<jar destfile="${jar.dest}/myapp.jar">
[color=brown]<!--
<classes dir="${build.class}" />s
-->[/color]
<fileset dir="${build.class}">
[color=brown]<!--
<include name="com/**" />
<include name="${build.src}/*.properties" />
<include name="${build.src}/resources" />
<exclude name="**/Test.class" />
-->[/color]

</fileset>
</jar>
</target>

<target name="antear" depends="build" description="EJB工程用">
[color=brown]<!--
<ear destfile="${ear.dest}/${name}-${version}.ear" appxml="META-INF/application-j1.3.xml">
<fileset dir="${war.dest}" includes="${name}-${version}.war" />
</ear>
-->[/color]
</target>

<target name="antwar" depends="build" description="打war包,不将java文件打入包内">
[color=brown]<!-- <delete dir="${build.src}"/> -->[/color]
<war warfile="${war.dest}/TestQuartz.war" webxml="${buildwar.dest}/WEB-INF/web.xml">
<lib dir="${buildwar.dest}/WEB-INF/lib" />
<classes dir="${build.class}" />
<fileset dir="${buildwar.dest}">
<include name="**/*.conf" />
<include name="**/*.html" />
<include name="**/*.htm" />
<include name="**/*.jsp" />
<include name="**/*.js" />
<include name="**/*.css" />
<include name="**/*.swf" />
<include name="**/*.jpg" />
<include name="**/*.jpeg" />
<include name="**/*.png" />
<include name="**/*.gif" />
<exclude name="**/*.bak"/>
</fileset>
</war>
</target>

<target name="antzip" depends="antjar" description="打zip文件包">
[color=brown]<!--
<zip destfile="${zip.dest}/TestQuartzPro.zip">
<zipfileset dir="${war.dest}" includes="TestQuartz.war" prefix="." />
<zipfileset dir="${db.dest}" includes="*.*" prefix="db" />
<zipfileset dir="${doc.dest}" includes="*.*" prefix="doc" />
<zipfileset dir="${xls.dest}" includes="*.*" prefix="xls" />
</zip>
-->[/color]
</target>

<target name="clean" depends="init" description="清除打包用临时文件">

<delete dir="${build.src}" description="删除dist包里的java文件" />
<delete>
<fileset dir="${build.class}" includes="**/*.classes" />
</delete>

</target>

<target name="publish" depends="antjar,antear,antwar,antzip,javadoc,clean" description="发布war包至tomcat目录下(注意depends属性里面的内容,name=publish是跟project标签里的default属性同名的)">
<copy todir="${tomcat.home}/webapps">
<fileset dir="${war.dest}">
<include name="**/*.war"/>
<exclude name="**/*.bak"/>
</fileset>
</copy>

<delete dir="${buildwar.dest}" description="删除dist目录" />

</target>

</project>

  下面是针对MyEclipse建立的Web工程打war包的build.xml脚本: 

<?xml version="1.0" encoding="UTF-8"?> 
<!-- <project default="war" basedir="." name="MyYywlProForAnt"> -->
<project default="publish" basedir="." name="MyYywlProForAnt">

<property name="project" value="MyYywlPro" />
<property name="version" value="1.0" />
<property name="year" value="2013" />

<property name="src" value="${basedir}/src" />
<property name="lib" value="${basedir}/WebRoot/WEB-INF/lib" />
<property name="webapp" value="${basedir}/WebRoot" />
<property name="packages" value="com.*" />

<property name="classes" value="${basedir}/build/classes" />
<property name="war.dest" value="${basedir}/build" />

<!-- 这个地方的war包名称最好跟工程名称一致,否则放到Tomcat下发布的时候容易跟Tomcat里的server.xml文件内容设置的不一致 -->
<property name="ProductName" value="MyYywlPro" />

<property name="compiler.debug" value="on" />
<property name="compiler.generate.no.warnings" value="off" />
<property name="compiler.args" value="-encoding UTF8" />
<property name="compiler.max.memory" value="400m" />

<property name="tomcat.home" value="D:\Tomcat 6.0" />
<property name="tomcat.home.lib" value="D:\Tomcat 6.0\lib" />

<patternset id="ignored.files">
<exclude name="**/.svn/**" />
</patternset>

<target name="clean">
<delete dir="${war.dest}" />

<echo message="----------- ${project} ${version} [${year}] ------------" />
<echo message="----------- ${src} ------------" />
<echo message="----------- ${lib} ------------" />
<echo message="----------- ${webapp} ------------" />
<echo message="----------- ${war.dest} ------------" />
<echo message="----------- ${tomcat.home} ------------" />

<path id="classpath" description="设置依赖jar文件">
<fileset dir="${tomcat.home.lib}">
<include name="**/*.jar"/>
<exclude name="**/*.bak" />
</fileset>
<fileset dir="${lib}">
<include name="**/*.jar" />
<exclude name="**/*.bak" />
</fileset>
</path>

<filter token="year" value="${year}" />
<filter token="version" value="${version}" />
<filter token="date" value="${TODAY}" />
<filter token="log" value="true" />
<filter token="verbose" value="true" />
</target>

<target name="compile" depends="clean">
<mkdir dir="${classes}" />
<javac encoding="utf-8" srcdir="${src}" destdir="${classes}">
<classpath refid="classpath" description="依赖上面path标签设置的jar文件" />
</javac>
</target>

<target name="war" depends="compile">
<war destfile="${war.dest}/${ProductName}.war" webxml="WebRoot/WEB-INF/web.xml">
<!-- <fileset dir="${webapp}" includes="**/*.jsp" /> -->
<fileset dir="${webapp}">
<include name="**/*" />
<exclude name="**/*.bak" />
</fileset>
</war>
</target>


<target name="publish" depends="war" description="发布war包至tomcat目录下(注意depends属性里面的内容,name=publish是跟project标签里的default属性同名的)">
<copy todir="${tomcat.home}/webapps">
<fileset dir="${war.dest}">
<include name="**/*.war" />
<exclude name="**/*.bak" />
</fileset>
</copy>
</target>

</project>