<?xml version="1.0" encoding="GBK"?>
<project name="ch25" default="build" basedir=".">
<!-- //定义不同的日期格式 -->
<tstamp>
<format property="TODAY_UK" pattern="d-MMMM-yyyy" locale="en"/>
</tstamp>
<tstamp>
<format property="TODAY_CN" pattern="d-MMMM-yyyy" locale="zh"/>
</tstamp>
<tstamp>
<format property="touch.time" pattern="MM/dd/yyyy hh:mm aa" offset="-5" unit="hour"/>
</tstamp>
<!-- //输出的日期格式定义 -->
<target name="timestampTarget" >
<echo message="${DSTAMP}"/>
<echo message="${TODAY_UK}" />
<echo message="${TODAY_CN}" />
<echo message="${touch.time}" />
</target>
<!-- //定义变量 -->
<property name="build.dir" value="${basedir}/build"/>
<property name="build.src" value="${basedir}/build/src"/>
<property name="build.webinf" value="${basedir}/build/WEB-INF"/>
<property name="build.dest" value="${basedir}/build/WEB-INF/classes"/>
<property name="project.file" value="${basedir}/ch25"/>
<!-- //读取环境变量 -->
<property environment="env"/>
<!-- //读取环境变量 path中可以定义fileset 用法一样-->
<path id="classpath">
<pathelement path="${env.CATALINA_HOME}/common/lib/servlet-api.jar"/>
</path>
<target name="init" depends="timestampTarget">
<echo>init_${DSTAMP}_${ant.project.name}_{build.dir}/${DSTAMP}${TSTAMP}.war</echo>
<echo>JAVA_HOME_${env.JAVA_HOME}</echo>
<echo message="CLASSPATH_${env.CLASSPATH}"/>
<mkdir dir="${build.dir}"/>
<mkdir dir="${build.src}"/>
<mkdir dir="${build.webinf}"/>
<mkdir dir="${build.dest}"/>
<copy todir="${build.dir}">
<fileset dir="${project.file}">
<include name="**/*.jsp"/>
<include name="*.html"/>
<include name="images/**"/>
<include name="src/**"/>
<include name="WEB-INF/*.*"/>
<exclude name="build.xml"/>
</fileset>
</copy>
</target>
<target name="compile" depends="init"
description="编译源代码,输出到build目录下。">
<javac srcdir="${build.src}" destdir="${build.dest}">
<classpath refid="classpath"/>
</javac>
</target>
<target name="build" depends="compile"
description="在build目录下建立ch24.war">
<war destfile="${build.dir}/${ant.project.name}.war"
webxml="${build.webinf}/web.xml">
<fileset dir="${build.dir}">
<include name="*.jsp"/>
<include name="*.html"/>
<include name="images/**"/>
</fileset>
<classes dir="${build.dest}"/>
</war>
</target>
<target name="clean" depends="init"
description="删除build文件">
<delete dir="${build.dir}"/>
</target>
<!--部署tomcat-->
<target name="deploy" depends="build">
<copy todir="${env.CATALINA_HOME}/webapps">
<fileset dir="${build.dir}">
<include name="${ant.project.name}.war"/>
</fileset>
</copy>
</target>
<!--卸载tomcat-->
<target name="undeploy">
<delete file="${env.CATALINA_HOME}/webapps/${ant.project.name}.war"/>
<delete dir="${env.CATALINA_HOME}/webapps/${ant.project.name}"/>
</target>
<!--启动tomcat-->
<target name="start-tomcat" description="tomcat starting.....">
<exec executable="${env.CATALINA_HOME}/bin/startup.bat" spawn="true" vmlauncher="false">
<arg line="/c start ${env.CATALINA_HOME}/bin/startup.bat" />
</exec>
</target>
<!--关闭tomcat-->
<target name="tomcat.stop">
<java jar="${env.CATALINA_HOME}/bin/bootstrap.jar" fork="true">
<jvmarg value="-Dcatalina.home=${env.CATALINA_HOME}"/>
<arg line="stop"/>
</java>
<waitfor maxwait="5" maxwaitunit="second">
<available file="errors.log"/>
</waitfor>
</target>
<!-- 使用JDK的native2ascii工具执行本地化操作,转换编码。 -->
<target name="locale">
<echo>Target ----- [locale]</echo>
<exec dir="${basedir}/${ant.project.name}/app-classpath" executable="native2ascii" os="Windows XP">
<arg line="-encoding gb2312 MessageResources_temp.properties MessageResources_zh_CN.properties"/>
</exec>
</target>
<!--jar -->
<target name="jar" depends="compile">
<jar jarfile="${build.dir}/${ant.project.name}.jar" basedir="${build.dest}" >
</jar>
</target>
<!--利用属性文件定义变量 -->
<property file="build.properties" />
<target name="getproperties" >
<echo message="${tomcat.dir}" />
<echo message="${build.dest}" />
</target>
<!-- 执行其他的build.xml 文件 ant -f changeBuild.xml -->
<!--
Ant任务(执行多个构件文件)
ant antfile="subfile/projectB.xml" 【引用执行projectB.xml 执行dafault命令】
target="target2" output="out.log" 【引用执行projectB.xml 执行target2命令 并把结果打印在out.log文件中】
【当被引用的文件中 有主文件的属性时 被引用的文件属性被覆盖】
-->
<target name="callProjectB">
<echo message="In projectA calling projectB"/>
<ant antfile="subfile/projectB.xml" />
</target>
<target name="callProjectBbyTargetName">
<echo message="In projectA calling projectB"/>
<ant antfile="subfile/projectB.xml" target="target2" output="out.log"/>
</target>
<property name="porjectPro" value="project" />
<property name="porjectProB" value="projectB" />
<property name="porjectProC" value="projectC" />
<target name="callbyMySelf">
<echo message="In projectA calling projectB == ${porjectPro} == ${porjectProB} == ${porjectProC}"/>
<ant antfile="subfile/projectB.xml" target="target2" output="out.log"/>
<echo message="In projectA calling projectB == ${porjectPro} == ${porjectProB} == ${porjectProC}"/>
</target>
<!--用Ant安装和构建Hibernate项目 -->
<!-- antcall 需要测试param 和 porperty 的覆盖关系-->
<!--
<target name="init" >
<echo message="In init target"/>
</target>
<target name="targetA">
<echo message="In targetA calling targetB"/>
<property name="prop" value="prop property" />
<antcall target="targetB" >
<param name="path1" value="path1 param" />
</antcall>
<echo message="After call targetB" />
</target>
<target name="targetB" depends="init">
<echo message="In targetB" />
<echo message="path1=${path1}" />
<echo message="prop=${prop}" />
</target>
-->
<!--未执行成功-->
<!--
未执行成功
● if和unless
● refid属性:这个属性代表当前project中的属性id。
● torefid属性:用于指定在被调用的project中的引用id。
● 将jar转成exe文件
-->
</project>
build.properties
tomcat.dir=C:/ApacheGroup/Tomcat5.5
<project name="ch25" default="build" basedir=".">
<!-- //定义不同的日期格式 -->
<tstamp>
<format property="TODAY_UK" pattern="d-MMMM-yyyy" locale="en"/>
</tstamp>
<tstamp>
<format property="TODAY_CN" pattern="d-MMMM-yyyy" locale="zh"/>
</tstamp>
<tstamp>
<format property="touch.time" pattern="MM/dd/yyyy hh:mm aa" offset="-5" unit="hour"/>
</tstamp>
<!-- //输出的日期格式定义 -->
<target name="timestampTarget" >
<echo message="${DSTAMP}"/>
<echo message="${TODAY_UK}" />
<echo message="${TODAY_CN}" />
<echo message="${touch.time}" />
</target>
<!-- //定义变量 -->
<property name="build.dir" value="${basedir}/build"/>
<property name="build.src" value="${basedir}/build/src"/>
<property name="build.webinf" value="${basedir}/build/WEB-INF"/>
<property name="build.dest" value="${basedir}/build/WEB-INF/classes"/>
<property name="project.file" value="${basedir}/ch25"/>
<!-- //读取环境变量 -->
<property environment="env"/>
<!-- //读取环境变量 path中可以定义fileset 用法一样-->
<path id="classpath">
<pathelement path="${env.CATALINA_HOME}/common/lib/servlet-api.jar"/>
</path>
<target name="init" depends="timestampTarget">
<echo>init_${DSTAMP}_${ant.project.name}_{build.dir}/${DSTAMP}${TSTAMP}.war</echo>
<echo>JAVA_HOME_${env.JAVA_HOME}</echo>
<echo message="CLASSPATH_${env.CLASSPATH}"/>
<mkdir dir="${build.dir}"/>
<mkdir dir="${build.src}"/>
<mkdir dir="${build.webinf}"/>
<mkdir dir="${build.dest}"/>
<copy todir="${build.dir}">
<fileset dir="${project.file}">
<include name="**/*.jsp"/>
<include name="*.html"/>
<include name="images/**"/>
<include name="src/**"/>
<include name="WEB-INF/*.*"/>
<exclude name="build.xml"/>
</fileset>
</copy>
</target>
<target name="compile" depends="init"
description="编译源代码,输出到build目录下。">
<javac srcdir="${build.src}" destdir="${build.dest}">
<classpath refid="classpath"/>
</javac>
</target>
<target name="build" depends="compile"
description="在build目录下建立ch24.war">
<war destfile="${build.dir}/${ant.project.name}.war"
webxml="${build.webinf}/web.xml">
<fileset dir="${build.dir}">
<include name="*.jsp"/>
<include name="*.html"/>
<include name="images/**"/>
</fileset>
<classes dir="${build.dest}"/>
</war>
</target>
<target name="clean" depends="init"
description="删除build文件">
<delete dir="${build.dir}"/>
</target>
<!--部署tomcat-->
<target name="deploy" depends="build">
<copy todir="${env.CATALINA_HOME}/webapps">
<fileset dir="${build.dir}">
<include name="${ant.project.name}.war"/>
</fileset>
</copy>
</target>
<!--卸载tomcat-->
<target name="undeploy">
<delete file="${env.CATALINA_HOME}/webapps/${ant.project.name}.war"/>
<delete dir="${env.CATALINA_HOME}/webapps/${ant.project.name}"/>
</target>
<!--启动tomcat-->
<target name="start-tomcat" description="tomcat starting.....">
<exec executable="${env.CATALINA_HOME}/bin/startup.bat" spawn="true" vmlauncher="false">
<arg line="/c start ${env.CATALINA_HOME}/bin/startup.bat" />
</exec>
</target>
<!--关闭tomcat-->
<target name="tomcat.stop">
<java jar="${env.CATALINA_HOME}/bin/bootstrap.jar" fork="true">
<jvmarg value="-Dcatalina.home=${env.CATALINA_HOME}"/>
<arg line="stop"/>
</java>
<waitfor maxwait="5" maxwaitunit="second">
<available file="errors.log"/>
</waitfor>
</target>
<!-- 使用JDK的native2ascii工具执行本地化操作,转换编码。 -->
<target name="locale">
<echo>Target ----- [locale]</echo>
<exec dir="${basedir}/${ant.project.name}/app-classpath" executable="native2ascii" os="Windows XP">
<arg line="-encoding gb2312 MessageResources_temp.properties MessageResources_zh_CN.properties"/>
</exec>
</target>
<!--jar -->
<target name="jar" depends="compile">
<jar jarfile="${build.dir}/${ant.project.name}.jar" basedir="${build.dest}" >
</jar>
</target>
<!--利用属性文件定义变量 -->
<property file="build.properties" />
<target name="getproperties" >
<echo message="${tomcat.dir}" />
<echo message="${build.dest}" />
</target>
<!-- 执行其他的build.xml 文件 ant -f changeBuild.xml -->
<!--
Ant任务(执行多个构件文件)
ant antfile="subfile/projectB.xml" 【引用执行projectB.xml 执行dafault命令】
target="target2" output="out.log" 【引用执行projectB.xml 执行target2命令 并把结果打印在out.log文件中】
【当被引用的文件中 有主文件的属性时 被引用的文件属性被覆盖】
-->
<target name="callProjectB">
<echo message="In projectA calling projectB"/>
<ant antfile="subfile/projectB.xml" />
</target>
<target name="callProjectBbyTargetName">
<echo message="In projectA calling projectB"/>
<ant antfile="subfile/projectB.xml" target="target2" output="out.log"/>
</target>
<property name="porjectPro" value="project" />
<property name="porjectProB" value="projectB" />
<property name="porjectProC" value="projectC" />
<target name="callbyMySelf">
<echo message="In projectA calling projectB == ${porjectPro} == ${porjectProB} == ${porjectProC}"/>
<ant antfile="subfile/projectB.xml" target="target2" output="out.log"/>
<echo message="In projectA calling projectB == ${porjectPro} == ${porjectProB} == ${porjectProC}"/>
</target>
<!--用Ant安装和构建Hibernate项目 -->
<!-- antcall 需要测试param 和 porperty 的覆盖关系-->
<!--
<target name="init" >
<echo message="In init target"/>
</target>
<target name="targetA">
<echo message="In targetA calling targetB"/>
<property name="prop" value="prop property" />
<antcall target="targetB" >
<param name="path1" value="path1 param" />
</antcall>
<echo message="After call targetB" />
</target>
<target name="targetB" depends="init">
<echo message="In targetB" />
<echo message="path1=${path1}" />
<echo message="prop=${prop}" />
</target>
-->
<!--未执行成功-->
<!--
未执行成功
● if和unless
● refid属性:这个属性代表当前project中的属性id。
● torefid属性:用于指定在被调用的project中的引用id。
● 将jar转成exe文件
-->
</project>
build.properties
tomcat.dir=C:/ApacheGroup/Tomcat5.5