1.使用Ant运行windows的批处理文件
@echo off echo Hello > test.txt |
四.使用Ant进行Junit测试
<?xml version="1.0"?> <project name="project" default="junit"> <property name="run.classpath" value="bin"></property> <property name="run.srcpath" value="src"></property> <property name="test.srcpath" value="test"></property> <property name="lib.dir" value="lib"/>
<path id="compile.path"> <pathelement location="${lib.dir}/junit-3.8.1.jar"/> <pathelement location="${lib.dir}/log4j-1.2.8.jar"/> </path>
<target name="compile"> <javac destdir="${run.classpath}" srcdir="${run.srcpath}" classpathref="compile.path"/> <javac destdir="${run.classpath}" srcdir="${test.srcpath}" classpathref="compile.path"/> </target>
<target name="junit" depends="compile"> <junit printsummary="true"> <classpath path="${run.classpath}"></classpath> <test name="org.ant.test.Test1"></test> </junit> </target> </project> |
一.Ant简介:
二.介绍Ant的DATATYPE和特性
1.路径(Path)
<classpath>
<pathelement location =”lib/some.jar”/>
</classpath>
<classpath>
<pathelement path =”build/classes;lib/some.jar”/>
</classpath>
2.文件集(Fileset)
<copy todir=”new_web”>
<fileset dir=”web”/>
</copy>
<fileset>
<include name="**/*Test.java"/>
</fileset>
<fileset>
<exclude name="**/*.jsp"/>
</fileset>
3.模式集(Patternset)
4.选择器(Selector)
<copy todir="newfiles">
<fileset dir="web">
<not>
<present targetdir="currebtfiles"/>
</not>
</fileset>
</copy>
<copy todir="newfiles">
<fileset dir="web">
<contains text="System"/>
</fileset>
public class Hello { public static void main(String[] args) { |
<?xml version="1.0"?> <project name="project" default="run"> <property name="run.classpath" value="bin"></property> <target name="compile"> <javac destdir="bin" srcdir="src"></javac> </target> <target name="run" depends="compile"> <java classname="org.ant.chapter1.Hello"> <classpath path="${run.classpath}"></classpath> <arg value="Ant"/> <arg file="D:/rag"/> </java> </target> </project> |
Buildfile: D:/MyEclipse/workspace/sad/build.xml compile: run: [java] Hello Ant [java] hello.dat BUILD SUCCESSFUL Total time: 734 milliseconds |
<?xml version="1.0"?> <project name="project" default="run"> <property name="run.classpath" value="bin"></property> <property name="Search.JVM.extra.args" value="-Xincgc"></property> <target name="compile"> <javac destdir="bin" srcdir="src"></javac> </target> <target name="run" depends="compile"> <java classname="org.ant.chapter1.Hello" fork="true" maxmemory="64m"> <classpath path="${run.classpath}"></classpath> <jvmarg line="${Search.JVM.extra.args}"/> <arg value="Ant"/> <arg file="D:/rag"/> </java> </target> </project> |
Mainfest-Version: 1.0 Created-By: myth Sealed: false Main-Class: org.ant.chapter1.Hello |
<?xml version="1.0"?> <project name="project" default="run"> <property name="run.classpath" value="bin"></property> <property name="Search.JVM.extra.args" value="-Xincgc"></property> <target name="compile"> <javac destdir="bin" srcdir="src"></javac> </target> <target name="jar" depends="compile"> <jar destfile="test.jar" update="true" manifest="MANIFEST.MF"> <fileset dir="bin"> <include name="**/*.class"/> </fileset> </jar> </target> <target name="run" depends="jar"> <java fork="true" maxmemory="64m" jar="test.jar"> <classpath path="${run.classpath}"></classpath> <jvmarg line="${Search.JVM.extra.args}"/> <arg value="Ant"/> <arg file="D:/rag"/> </java> </target> </project> |
Buildfile: D:/MyEclipse/workspace/sad/build.xml compile: jar: [jar] Updating jar: D:/MyEclipse/workspace/sad/test.jar run: [java] Hello Ant [java] hello.dat BUILD SUCCESSFUL Total time: 875 milliseconds |
<target name="run" depends="jar"> <java fork="true" maxmemory="64m" jar="test.jar" failonerror="false" > <classpath path="${run.classpath}"></classpath> <jvmarg line="${Search.JVM.extra.args}"/> <arg value="Ant"/> <arg file="D:/rag"/> </java> </target> |
Buildfile: D:/MyEclipse/workspace/sad/build.xml compile: jar: run: [java] java.lang.NullPointerException [java] at org.ant.chapter1.Hello.main(Hello.java:27) [java] Hello Ant [java] Exception in thread "main" [java] Java Result: 1 BUILD SUCCESSFUL Total time: 984 milliseconds |
<?xml version="1.0"?> <project name="project" default="run"> <target name="run"> <exec executable="cmd"> <arg value="/C a.bat"/> </exec> </target> </project> |
@echo off echo Hello >> a.txt |
<?xml version="1.0"?> <project name="project" default="tomcat-start"> <property name="tomcat.dir" value="c:/Tomcat5"></property> <target name="tomcat-start"> <exec dir="${tomcat.dir}/bin" executable="cmd"> <env key="CATALINA_HOME" path="${tomcat.dir}"/> <arg value="/C startup.bat"/> </exec> </target> <target name="tomcat-stop"> |
<formatter type="brief" usefile="false"/>
<formatter type="brief" usefile="false"/>
<formatter type="xml"/>
<junitreport todir="${test.xml}"> <fileset dir="${test.xml}"> <include name="TEST-*.xml"/> </fileset> <report format="frames" todir="${test.report}"/> </junitreport> |
<?xml version="1.0"?> <project name="project"default="junit"> <propertyname="run.classpath"value="bin"></property> <propertyname="run.srcpath"value="src"></property> <propertyname="test.srcpath"value="test"></property> <propertyname="test.xml"value="xml"></property> <propertyname="test.report"value="report"></property> <propertyname="lib.dir"value="lib"/>
<pathid="compile.path"> <pathelementlocation="${lib.dir}/junit-3.8.1.jar"/> <pathelementlocation="${lib.dir}/log4j-1.2.8.jar"/> </path>
<targetname="init"> <deletedir="${test.report}"/> <mkdirdir="${test.report}"/> <deletedir="${test.xml}"/> <mkdirdir="${test.xml}"/> </target>
<targetname="compile"depends="init"> <javacdestdir="${run.classpath}"srcdir="${run.srcpath}" classpathref="compile.path"/> <javacdestdir="${run.classpath}"srcdir="${test.srcpath}" classpathref="compile.path"/> </target>
<targetname="junit"depends="compile"> <junitprintsummary="false"> <classpathpath="${run.classpath}"></classpath> <formattertype="xml"/> <batchtesttodir="${test.xml}"> <filesetdir="${run.classpath}"> <includename="**/Test*.class"/> </fileset> </batchtest> </junit>
<junitreporttodir="${test.xml}"> <filesetdir="${test.xml}"> <includename="TEST-*.xml"/> </fileset> <reportformat="frames"todir="${test.report}"/> </junitreport> </target> </project> <?xml version="1.0"?> <projectname="project"default="junit"> <propertyname="run.classpath"value="bin"></property> <propertyname="run.srcpath"value="src"></property> <propertyname="test.srcpath"value="test"></property> <propertyname="test.xml"value="xml"></property> <propertyname="test.report"value="report"></property> <propertyname="lib.dir"value="lib"/>
<pathid="compile.path"> <pathelementlocation="${lib.dir}/junit-3.8.1.jar"/> <pathelementlocation="${lib.dir}/log4j-1.2.8.jar"/> </path>
<targetname="init"> <deletedir="${test.report}"/> <mkdirdir="${test.report}"/> <deletedir="${test.xml}"/> <mkdirdir="${test.xml}"/> </target>
<targetname="compile"depends="init"> <javacdestdir="${run.classpath}"srcdir="${run.srcpath}" classpathref="compile.path"/> <javacdestdir="${run.classpath}"srcdir="${test.srcpath}" classpathref="compile.path"/> </target>
<targetname="junit"depends="compile"> <junitprintsummary="false"> <classpathpath="${run.classpath}"></classpath> <formattertype="xml"/> <batchtesttodir="${test.xml}"> <filesetdir="${run.classpath}"> <includename="**/Test*.class"/> </fileset> </batchtest> </junit>
<junitreporttodir="${test.xml}"> <filesetdir="${test.xml}"> <includename="TEST-*.xml"/> </fileset> <reportformat="frames"todir="${test.report}"/> </junitreport> </target> </project> |
生成的文档:
<?xml version="1.0"?> <project name="batch" default="extract" basedir="."> <target name="extract"> <exec executable ="cmd"> <arg line="/c a.bat"/> </exec> </target> </project> |
<?xml version="1.0"?> <projectname="batch"default="tomcat-start"basedir="."> <propertyname="tomcat.dir"value="C:/Tomcat5"></property>
<targetname="tomcat-start"> <execdir="${tomcat.dir}/bin"executable="cmd"> <envkey="CATALINA_HOME"path="${tomcat.dir}"/> <argvalue="/C startup.bat"/> </exec> </target> </project> |
2.使用Ant运行shell文件
<?xml version="1.0"?> <projectname="batch"default="shell"basedir="."> <propertyname="tomcat.dir"value="C:/Tomcat5"></property>
<targetname="shell"> <execdir="${tomcat.dir}/bin"executable="bash"> <envkey="CATALINA_HOME"path="${tomcat.dir}"/> <argvalue="startup.sh"/> </exec> </target> </project> |
3.使用Ant运行cvs
<?xml version="1.0"?> <projectname="batch"default="shell"basedir="."> <propertyname="cvs.root"value="..."></property>
<targetname="cvs"> <cvscvsroot="cvs.root"command="checkout ../.."/> </target> </project> |
六.工程的打包部署
<?xml version="1.0"?> <projectname="project"default="jar"> <targetname="copy"> <tstamp> <formatproperty="time.format" pattern="yyyy-mm-dd'T'HH:mm:ss" locale="en"/> </tstamp> <copytofile="dist/readme"file="test.txt"> <filterset> <filtertoken="TIME"value="${time.format}"/> </filterset> </copy> </target>
<targetname="move">
<movetodir="dist"> <filesetdir="lib"> <includename="*.jar"/> </fileset> </move> </target>
<targetname="delete"depends="copy,move"> <deleteverbose="true"failonerror="false"> <filesetdir="dist"> <includename="*.jar"/> </fileset> </delete> </target> </project>
|
需要说明的是文件删除的时候可能这个文件正在被别人是用而无法删除,所以要用failonerror来标记,文件的复制是时间戳敏感的,如果拷贝的文件比原文件要老,那么Ant将不会执行copy,解决的办法是将overwrite属性设置为true,由于移动文件是复制文件的一个子类,所以它们的原理基本相同.
前面已经例举过一个jar文件打包的例子,下面主要介绍war文件的打包.Ant提供war文件打包的属性.<war>任务是<jar>任务的子类,但是他也提供了一些特有的属性:
<targetname="deploy"depends="init"> <wardestfile="${war.dir}/spring.war"webxml="${web.dir}/web.xml"> <classesdir="${web.dir}/classes"></classes> <filesetdir="WebContent"excludes="web.xml"></fileset> <libdir="${web.dir}/lib"></lib> </war> </target> |
可以看出war任务提供了许多WEB应用程序的特有属性,只要你指定了这些文件,war任务就会自动将他们放到正确的位置.
部署是项目发布的过程,Ant支持FTP,Email,本地和远程等几种部署模式,但是Ant并不内置对一些部署的支持,需要第三方的库.
optional.jar 也可能是这样的名字: jakarta-ant-1.4.1.optional.jar
netcomponents.jar <ftp>和<telnet>需要
activation.jar <mail>需要
mail.jar <mail>需要
下面只以本地部署为例,服务器为tomcat.
由于tomcat支持热部署,可以将webapp文件下的war文件自解压缩,所以最简单的部署方式是将工程打成war包后直接copy到webapp目录下面.另一种方法是使用tomcat的管理员身份,在manager页面装载和删除应用,这种方法比较复杂,也比较正规,他也是远程部署的基础.
<?xml version="1.0"?> <project name="project" default="deploy-local-catalina"> <property name="war.dir" value="dist"></property> <property name="web.dir" value="WebContent/WEB-INF"></property> <property name="webapp.name" value="spring"></property> <property name="catalina.port" value="8080"></property> <property name="catalina.username" value="admin"></property> <property name="catalina.password" value="admin"></property>
<target name="init"> <mkdir dir="${war.dir}"/> </target>
<target name="mkwar" depends="init">
<war destfile="${war.dir}/spring.war" webxml="${web.dir}/web.xml" > <classes dir="${web.dir}/classes"></classes> <fileset dir="WebContent" excludes="web.xml"></fileset> <lib dir="${web.dir}/lib"></lib> </war> </target>
<target name="remove-local-catalina"> <property name="deploy.local.remove.url" value="http://localhost:${catalina.port}/manager/remove"></property> <get dest="deploy.local.remove.txt" src="${deploy.local.remove.url}?path=/${webapp.name}" username="admin" password="admin"/>
<loadfile property="depoly.local.remove.result" srcfile="depoly.local.remove.txt"></loadfile> </target>
<target name="deploy-local-catalina" depends="mkwar, remove-local-catalina"> <property name="deploy.local.urlpath" value="file:///D:/MyEclipse/workspace/springstruts/dist/spring.war"></property> <property name="deploy.local.url.params" value="path=/${webapp.name}&war=${deploy.local.urlpath}"></property> <property name="deploy.local.url" value="http://localhost:${catalina.port}/manager/install"></property> <get src="${deploy.local.url}?${deploy.local.url.params}" dest="deploy-local.txt" username="admin" password="admin"/> <loadfile property="deploy.local.result" srcfile="deploy-local.txt"></loadfile> </target> </project>
|