您的位置:首页 > 其它

Ant实战篇 (二)

2003-10-24 16:37 423 查看
Ant[/b]实战篇 (二)[/b]
作 者:[/b]黄 凯 [/b]
E_mail:[/b]hk_sz@163.com[/b]
前 言[/b]
由于现在公司进行Unit Test Case的整理阶段,所以抽空对Ant和Junit技术进行了一下了解,以下是集合了众家所长之精华(考虑到是按我的思路总结的,也许不能完全表述原作者的思路,所以在参考[/b]中我把所有参考过的文章网址或书籍都罗列了出来,大家有时间不妨去看看原文)。
如果对Ant部分参数不明白的话,请参看《Ant理论篇》系列或ant自带的文档。

在这里要特别感谢我的同事王万鹏为本文档提供[/b]demoEJB[/b]实例,促使本文档提前完成。同时也要感谢一直以来给以我帮助的葛威龙同事,因为本人英文水准不高,很多不解的地方都得到了他大力的协助。在此特对他们表示衷心的谢意!!![/b]
[/b]
目 录[/b]
一、 [/b]Ant[/b]使用实例[/b][/b]
[/b]1.1 通过Antcopt task将当前目录下最近更新的文件(按系统时间来区分)文件提交至指定目录
[/b]1.2 用Ant开发java程序
1.3 ant结合junit进行软件自动测试
1.4 ant开发和部署web应用程序
1.5 ant打包(jar应用程序
1.6 ant开发EJB应用程序
参考[/b]
[/b]
1.5 ant打包(jar应用程序[/b]
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"
failonerror="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"
failonerror="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[/b]查看结果(也可以试试运行ant run[/b],结果是一样的)。
[/b]
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"
failonerror="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[/b]部署,然后运行ant run[/b]查看结果。

参考
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.csdn.net/Develop/Read_Article.asp?Id=20443
http://www.csdn.net/Develop/Read_Article.asp?Id=20444
http://www.csdn.net/Develop/Read_Article.asp?Id=20445
http://www.csdn.net/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》
作者:张洪斌
出版社:不详
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: