Ant 實戰篇 (二)

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"
???????? 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查看結果(也可以試試運行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"
???????? 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部署,然后運行ant run查看結果。