java程序运行的扩展
1.带有参数的应用程序运行
Ant在<java>任务中提供了<arg>元素,<arg>有四种属性value,file,line,path
public class Hello { public static void main(String[] args) { System.out.println("Hello " + args[0]); File file = new File(args[1]); String[] filename = file.list(); for(int i = 0; i < filename.length; i++) { System.out.println(filename[i]); } } } |
build.xml
<?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 |
2.控制新的JVM
一般的<java>任务都运行在当前的JVM中,单一些特定的情况下将Ant运行在新的JVM下面,这时只需要将<java>中的一个属性fork设置为true就可以了.
我们知道,java命令提供了许多的运行参数,用于指定JVM的属性,同样在Ant也提供相应的属性,看例子:
<?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> |
3.运行jar文件,使用failonerror处理错误
<java>同样提供了运行jar文件的属性
MANIFEST.MF
Mainfest-Version: 1.0 Created-By: myth Sealed: false Main-Class: org.ant.chapter1.Hello |
build.xml
<?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 |
在某些情况下,我们不希望由于一些不重要的任务构建失败,而导致整个构建的失败,所以Ant提供了一个共同的属性-failonerror,多数任务的默认值为failοnerrοr="true",既当此任务构建失败时,失败信息会传递给控制台,并导致build failed,failonerror只支持在新的JVM里运行.
<target name="run" depends="jar"> <java fork="true" maxmemory="64m" jar="test.jar" failοnerrοr="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 |
可以看出虽然run构建失败,但是Ant还是执行了,原来的jar文件,并且BUILD SUCCESSFUL!!
使用Ant运行tomcat
Ant使用<exec>任务运行本地程序,先看一个例子:
<?xml version="1.0"?> <project name="project" default="run"> <target name="run"> <exec executable="cmd"> <arg value="/C a.bat"/> </exec> </target> </project> |
a.bat
@echo off echo Hello >> a.txt |
运行完后,会在根目录生成a.txt文件,里面内容为Hello
下面我们来运行tomcat
<?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"> <exec dir="${tomcat.dir}/bin" executable="cmd"> <env key="CATALINA_HOME" path="${tomcat.dir}"/> <arg value="/c shutdown.bat"/> </exec> </target> </project> |
成功!!