一、 Ant使用实例
1.1 通过Ant的copt task将当前目录下最近更新的文件(按系统时间来区分)文件提交至指定目录
1.2 用Ant开发java程序
1.3 ant结合junit进行软件自动测试
1.4 ant开发和部署web应用程序
1.5 ant打包(jar)应用程序
1.6 ant开发EJB应用程序
参考
一、 Ant使用实例
1.3 ant结合junit进行软件自动测试
1> 用ant结合junit启动基本编译和测试的自动过程如下:
★ 执行 JUnit 测试
★ 捕获测试结果,将结果记录到 XML 文件或其它格式的文件
★ 将结果转化为基于 XSL 格式的测试报告
★ 将这个报告转化为 HTML 格式
★ 用电子邮件发送报告
一旦捕获结果,就可以使用XML格式化器将每个测试套件运行的故障和错误数,连同软件包和类名还有测试套件的执行时间一起引入。对于每个测试套件,都有以下信息被捕获:
☆ 测试案例的名称
☆ 执行的持续时间
☆ 故障或错误的类型(如果适用)
☆ 任何故障或错误的详细信息
2> 本次实例的目录结构:
D:/car 主目录
/src 源程序目录
/classes 编译后的class文件目录
/etc 其他
/xsl XSL文件目录
首先在D盘创建car目录、src子目录和etc子目录,并从%ANT_HOME%/etc下拷贝junit-noframes.xsl文件至D:/car/etc/xsl子目录下。
3> 从http://download.sourceforge.net/junit/junit3.8.zip下载,解压至D盘,将其目录下的junit.jar文件拷贝至%ANT_HOME%/lib目录下。
4> 在src子目录下新建Car.java以及testCar.java文件。
Car.java的内容如下:
//将要被测试的类
public class Car
{
public int getWheels()
{
return 3;
}
}
testCar.java的内容如下:
//执行测试的类
import junit.framework.*;
public class testCar extends TestCase
{
protected int expectedWheels;
protected Car myCar;
public testCar(String name)
{
super(name);
}
protected void setUp()
{
expectedWheels = 4;
myCar = new Car();
}
public static Test suite()
{
return new TestSuite(testCar.class);
}
public void testGetWheels()
{
assertEquals(expectedWheels, myCar.getWheels());
}
}
5> 在build.xml和build.properties文件。
buil.properties文件内容如下:
src.dir=src
build.dir=classes
xsl.dir=etc/xsl
report.dir=report
build.xml文件内容如下:
<?xml version="1.0"?>
<project default="build" basedir=".">
<property file="build.properties" description="Transfer property from .properties file"/>
<target name="build" description="Build the work directory">
<mkdir dir="${build.dir}" />
<mkdir dir="${report.dir}" />
<javac srcdir="${src.dir}" destdir="${build.dir}">
<include name="**/*.java" />
</javac>
</target>
<target name="junit" depends="build" description="Execute Unit Tests">
<tstamp/>
<junit printsummary="yes">
<classpath>
<pathelement location="${build.dir}"/>
</classpath>
<formatter type="xml" />
<batchtest todir="${report.dir}">
<fileset dir="${src.dir}">
<include name="**/testCar.java" />
</fileset>
</batchtest>
</junit>
<junitreport todir="${report.dir}">
<fileset dir="${report.dir}">
<include name="TEST-*.xml"/>
</fileset>
<report format="noframes" styledir="${xsl.dir}" todir="${report.dir}"/>
</junitreport>
</target>
<target name="clean" description="Delete the work directory">
<delete includeEmptyDirs="true">
<fileset dir="${build.dir}"/>
<fileset dir="${report.dir}"/>
</delete>
</target>
</project>
6> 在D:/car目录下运行ant junit,
这样在car/report中生成了junit-noframes.html、TESTS-TestSuites.xml和TEST-testCar.xml文件,打开junit-noframes.html查看结果。运行ant clean恢复原状。
1.5 ant打包(jar)应用程序
1> 前提:
本例使用的目录结构如下:
D:/ age
src java源文件目录
META-INF 配置文件目录
2> 在src目录下创建VirtualAge.java和MyVirtualAge.java文件。
VirtualAge.java内容如下:
public final class VirtualAge
{
public static int yeasOld(int i)
{
return i+1;
}
}
MyVirtualAge.java内容如下:
public class MyVirtualAge
{
public static void main(String[] args)
{
int myAge= 10;
System.out.println("My Age is "+myAge);
System.out.println("My Virtual Age is "+VirtualAge.yeasOld(myAge));
}
}
3> 在age目录下建立build.properties和build.xml文件。
build.properties文件内容如下:
src=src
classes=classes
jar=jar
manifest=META-INF
author.name=Kay
build.xml文件内容如下:
<?xml version="1.0"?>
<project default="help" basedir=".">
<property file="build.properties"/>
<target name="init">
<mkdir dir="${classes}"/>
<mkdir dir="${jar}"/>
</target>
<target name="build" depends="init">
<javac destdir="${classes}">
<src path="${src}"/>
</javac>
</target>
<target name="jar" depends="build">
<jar destfile="${jar}/age.jar">
<fileset dir="${classes}"/>
<manifest>
<attribute name="Built-By" value="${author.name}"/>
<attribute name="Main-Class" value="MyVirtualAge"/>
</manifest>
</jar>
</target>
<target name="run" depends="jar">
<java classname="MyVirtualAge"
fork="true"
failοnerrοr="true">
<arg value="-jar"/>
<classpath>
<pathelement location="${jar}/age.jar"/>
</classpath>
</java>
</target>
<target name="runjar" depends="jar">
<java jar="${jar}/age.jar"
fork="true"
failοnerrοr="true">
<arg value="-jar"/>
<classpath>
<pathelement location="${jar}/age.jar"/>
</classpath>
</java>
</target>
<target name="clean">
<delete includeEmptyDirs="true">
<fileset dir="${classes}"/>
<fileset dir="${jar}"/>
</delete>
</target>
<target name="help">
<echo message="init Initialization"/>
<echo message="build Compiler the java build class"/>
<echo message="jar Make JAR Archive file"/>
<echo message="run Run JAR Archive file with a appointed class entry"/>
<echo message="runjar Run JAR Archive file with a Main-Class entry"/>
<echo message="clean Clean the ant create's file and directory"/>
<echo message="help Prints this message"/>
</target>
</project>
4> 在age目录下运行ant runjar查看结果(也可以试试运行ant run,结果是一样的)。
1.6 ant开发EJB应用程序
1> 本例使用的目录结构如下:
D:/ demoEJB
src java源文件目录
conf 配置文件目录
2> 在src目录下创建ConverterEJB.java、ConverterHome.java、Converter.java和Client.java文件。
ConverterEJB.java文件内容如下:
import java.rmi.RemoteException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import java.math.*;
public class ConverterEJB implements SessionBean {
BigDecimal yenRate = new BigDecimal("121.6000");
BigDecimal euroRate = new BigDecimal("0.0077");
public BigDecimal dollarToYen(BigDecimal dollars) {
BigDecimal result = dollars.multiply(yenRate);
return result.setScale(2,BigDecimal.ROUND_UP);
}
public BigDecimal yenToEuro(BigDecimal yen) {
BigDecimal result = yen.multiply(euroRate);
return result.setScale(2,BigDecimal.ROUND_UP);
}
public ConverterEJB() {}
public void ejbCreate() {}
public void ejbRemove() {}
public void ejbActivate() {}
public void ejbPassivate() {}
public void setSessionContext(SessionContext sc) {}
}
ConverterHome.java文件内容如下:
import javax.ejb.EJBHome;
import java.io.Serializable;
import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.ejb.EJBHome;
public interface ConverterHome extends EJBHome {
Converter create() throws RemoteException, CreateException;
}
Converter.java文件内容如下:
import javax.ejb.EJBObject;
import java.rmi.RemoteException;
import java.math.*;
public interface Converter extends EJBObject {
public BigDecimal dollarToYen(BigDecimal dollars) throws RemoteException;
public BigDecimal yenToEuro(BigDecimal yen) throws RemoteException;
}
Client.java文件内容如下:
import java.rmi.RemoteException;
import java.util.Collection;
import java.util.Hashtable;
import java.util.Properties;
import java.util.Vector;
import java.util.Iterator;
import javax.ejb.CreateException;
import javax.ejb.DuplicateKeyException;
import javax.ejb.FinderException;
import javax.ejb.ObjectNotFoundException;
import javax.ejb.RemoveException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import java.math.BigDecimal;
public class Client {
private static Context getInitialContext() throws NamingException {
try {
Properties h = new Properties();
h.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
h.put(Context.PROVIDER_URL, "t3://localhost:7001");
return new InitialContext(h);
} catch (NamingException ne) {
throw ne;
}
}
public static void main(String[] args) {
try {
Context initial = getInitialContext();
Object objref = initial.lookup("ejb/session/converter");
ConverterHome home =(ConverterHome)PortableRemoteObject.narrow(objref,ConverterHome.class);
Converter currencyConverter = home.create();
BigDecimal param = new BigDecimal ("100.00");
BigDecimal amount = currencyConverter.dollarToYen(param);
System.out.println(amount);
amount = currencyConverter.yenToEuro(param);
System.out.println(amount);
System.exit(0);
} catch (Exception ex) {
System.err.println("Caught an unexpected exception!");
ex.printStackTrace();
}
}
}
3> 在demoEJB目录下建立build.properties和build.xml文件。
build.properties文件内容如下:
src=src
conf=conf
classes=classes
manifest=classes/META-INF
jar=jar
weblogic.lib=c:/bea/weblogic700/server/lib
author.name=Kay
username=training
user.password=training
ejb.name=demoEJB
weblogic.deploy.dir=C:/bea/user_projects/mydomain/myserver/upload
build.xml文件内容如下:
<?xml version="1.0"?>
<project default="help" basedir=".">
<property file="build.properties"/>
<path id="bea.class.path">
<fileset dir="${weblogic.lib}">
<include name="weblogic.jar"/>
</fileset>
</path>
<target name="init">
<mkdir dir="${classes}"/>
<mkdir dir="${manifest}"/>
<mkdir dir="${jar}"/>
<copy todir="${manifest}">
<fileset dir="${conf}">
<include name="ejb-jar.xml"/>
<include name="weblogic-ejb-jar.xml"/>
</fileset>
</copy>
</target>
<target name="build" depends="init">
<javac srcdir="${src}" destdir="${classes}" includes="*.java">
<classpath refid="bea.class.path"/>
</javac>
</target>
<target name="jar" depends="build">
<jar destfile="${jar}/${ejb.name}.jar">
<fileset dir="${classes}"/>
<manifest>
<attribute name="Built-By" value="${author.name}"/>
<attribute name="Main-Class" value="Client"/>
</manifest>
</jar>
</target>
<target name="deploy" depends="jar">
<serverdeploy action="deploy" source="${jar}/${ejb.name}.jar">
<weblogic application="${ejb.name}"
server="t3://127.0.0.1:7001"
classpath="${weblogic.lib}/weblogic.jar"
username="${username}"
password="${user.password}"
component="${ejb.name}:myserver"
debug="true"/>
</serverdeploy>
</target>
<target name="redeploy" depends="jar">
<serverdeploy action="update" source="${jar}/${ejb.name}.jar">
<weblogic application="${ejb.name}"
server="t3://127.0.0.1:7001"
classpath="${weblogic.lib}/weblogic.jar"
username="${username}"
password="${user.password}"
component="${ejb.name}:myserver"
debug="true"/>
</serverdeploy>
</target>
<target name="undeploy">
<serverdeploy action="undeploy">
<weblogic application="${ejb.name}"
server="t3://127.0.0.1:7001"
classpath="${weblogic.lib}/weblogic.jar"
username="${username}"
password="${user.password}"
debug="true"/>
</serverdeploy>
</target>
<target name="delete">
<serverdeploy action="delete">
<weblogic application="${ejb.name}"
server="t3://127.0.0.1:7001"
classpath="${weblogic.lib}/weblogic.jar"
username="${username}"
password="${user.password}"/>
</serverdeploy>
</target>
<target name="run">
<java classname="Client"
fork="true"
failοnerrοr="true">
<classpath refid="bea.class.path"/>
<classpath>
<pathelement location="${weblogic.deploy.dir}/${ejb.name}/${ejb.name}.jar"/>
</classpath>
</java>
</target>
<target name="clean">
<delete includeEmptyDirs="true">
<fileset dir="${classes}"/>
<fileset dir="${jar}"/>
<fileset dir="${weblogic.deploy.dir}/${ejb.name}"/>
</delete>
</target>
<target name="help">
<echo message="init Initialization"/>
<echo message="build Compiler the java build class"/>
<echo message="jar Make JAR Archive file"/>
<echo message="deploy Deploy the JAR Archive file"/>
<echo message="redeploy Redeploy the JAR Archive file"/>
<echo message="undeploy Undeploy the JAR Archive file"/>
<echo>delete Delete the JAR Archive file's location from Web
application</echo>
<echo message="run Run JAR Archive file with a appointed class entry"/>
<echo message="clean Clean the ant create's file and directory"/>
<echo message="help Prints this message"/>
</target>
</project>
4> 启动Weblogic server,然后在age目录下首先运行ant deploy部署,然后运行ant run查看结果。
Ant 的使用
作者:不详
原址:http://php.igt.com.tw/unit_116.htm
使用 ANT 开发 Java 程序
作者:cinc
原址:http://www.douzhe.com/bbsjh/14/434.html
利用Ant实现项目自动构建测试备份并发布到项目web
作者:beyondii
原址:http://www.youkuaiyun.com/Develop/Read_Article.asp?Id=20443
http://www.youkuaiyun.com/Develop/Read_Article.asp?Id=20444
http://www.youkuaiyun.com/Develop/Read_Article.asp?Id=20445
http://www.youkuaiyun.com/Develop/Read_Article.asp?Id=20446
让编译和测试过程自动化
作者:Erik Hatcher
原址:http://www-900.ibm.com/developerWorks/cn/java/j-junitmail/
《J2EE应用开发(Weblogic+JBuilder)》
出版社: 电子工业出版社
《The BestBook Advanced Programmer Java2》
作者:张洪斌
出版社:不详