本文主要介绍如何Xdoclet标注开发EJB,以及如何发布到weblogic。
如何发布WAR,EAR到weblogic。
新建一个java文件,EJB的bean文件,如下,标注为蓝色的为xdoclet注解,会用了生成ejb的descriptor文件。
具体可参考xdoclet文档。
package com.bt.presentation.demo.ejb;
/**
* @ejb:bean type="Stateless" name="com.bt.presentation.demo.ejb.EJBDemo"
* jndi-name="ejb/ejbdemo" display-name="EJB global transaction demo"
*/
public class EJBDemoBean implements SessionBean {
private static final boolean VERBOSE = true;
private SessionContext ctx;
private void log(String s) {
if (VERBOSE)
System.out.println(s);
}
public void ejbActivate() {
log("ejbActivate called");
}
public void ejbRemove() {
log("ejbRemove called");
}
public void ejbPassivate() {
log("ejbPassivate called");
}
public void setSessionContext(SessionContext ctx) {
log("setSessionContext called");
this.ctx = ctx;
}
public void ejbCreate() throws CreateException {
log("ejbCreate called");
}
/**
* @ejb:interface-method view-type="both"
* @ejb:transaction type="Required"
*/
public void testRef(DataStr data) {
System.out.println("testRef Started.");
data.setStr("testRef");
}
}
准备一个application.xml文件如下。
<?xml version="1.0" encoding="UTF-8"?>
<application id="Application_ID" version="1.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/application_1_4.xsd">
<display-name>PresentationDemoEAR</display-name>
<module>
<ejb>PresentationDemoEJB.jar</ejb>
</module>
<module>
<web>
<web-uri>PresentationDemoWeb.war</web-uri>
<context-root>PresentationDemoWeb</context-root>
</web>
</module>
</application>
准备一个build.properties,如下:
#set property
WL_HOME=C:/bea/weblogic92
PORT=7001
DEVELOPMENTSERVER=t3://localhost:7001
DEVELOPMENTUSER=weblogic
DEVELOPMENTUSERPASSWORD=weblogic
准备一个build.xml,如下:
<project name="PresentationDemoBuilder" basedir=".">
<target name="jar_util">
<property name="util.src.dir" value="D:/JAVAWorkspace/Test/PresentationUtil/src" />
<property name="utilbuild.dir" value="utilbuild" />
<property name="utiljar.dir" value="utiljar" />
<mkdir dir="${utilbuild.dir}" />
<mkdir dir="${utiljar.dir}" />
<javac srcdir="${util.src.dir}" destdir="${utilbuild.dir}" includes="**/*.java" excludes="**/*Client.java" />
<mkdir dir="${utilbuild.dir}/META-INF" />
<jar jarfile="${utiljar.dir}/PresentationUtil.jar" basedir="${utilbuild.dir}" update="yes">
<manifest>
<attribute name="class-path" value="PresentationUtil.jar" />
</manifest>
</jar>
</target>
<target name="init" depends="jar_util">
<!-- class lib folder path difine -->
<property name="Weblogic_lib_folder" value="C:/bea/weblogic92/server/lib" />
<property name="xdoclet.jar.path" value="C:/Dev/xdoclet-1.2.3" />
<!-- folder path difine -->
<property name="ejb.src.dir" value="D:/JAVAWorkspace/Test/PresentationDemoEJB/src" />
<property name="generated.src.dir" value="gen_source" />
<property name="ejbcfg.dir" value="allcfg" />
<property name="build.dir" value="ejbbuild" />
<property name="webbuild.dir" value="webbuild" />
<property name="ejbjar.dir" value="ejbjar" />
<property name="webwar.dir" value="webwar" />
<property name="ear.dist.dir" value="ear" />
<property name="client.dist.dir" value="clientjar" />
<property name="ejbJAR" value="PresentationDemoEJB.jar" />
<property name="ejbClientJAR" value="PresentationDemoEJBClient.jar" />
<property name="webWAR" value="PresentationDemoWeb.war" />
<property name="web.src.dir" value="D:/JAVAWorkspace/Test/PresentationDemoWeb/src" />
<property name="web.content.dir" value="D:/JAVAWorkspace/Test/PresentationDemoWeb/WebContent" />
<property name="APPNAME" value="PresentationDemoEAR" />
<property name="appear" value="PresentationDemo.ear" />
<!-- property from file -->
property file="build.properties"
<!-- Classpath define-->
<path id="project.class.path">
<pathelement path="${java.class.path}/" />
<pathelement path="${additional.path}" />
<pathelement path="${utiljar.dir}/PresentationUtil.jar" />
<pathelement path="${Weblogic_lib_folder}/weblogic.jar" />
<fileset dir="${xdoclet.jar.path}">
<include name="**/*.jar" />
</fileset>
</path>
<echo message="${java.class.path}" />
<!-- Classpath define-->
<path id="project.client.class.path">
<pathelement path="${java.class.path}/" />
<pathelement path="${Weblogic_lib_folder}/weblogic.jar" />
<pathelement path="${client.dist.dir}" />
<pathelement path="${client.dist.dir}/${ejbClientJAR}" />
</path>
<taskdef name="wldeploy" classname="weblogic.ant.taskdefs.management.WLDeploy">
<classpath refid="project.class.path" />
</taskdef>
<!--
<taskdef name="wldeploy" classname="weblogic.ant.taskdefs.management.WLDeploy">
<classpath refid="project.class.path"/>
</taskdef>
-->
</target>
<target name="prepare" depends="init">
<mkdir dir="${build.dir}" />
<mkdir dir="${ejbjar.dir}" />
<mkdir dir="${generated.src.dir}" />
<mkdir dir="${ejbcfg.dir}" />
<mkdir dir="${webwar.dir}" />
<mkdir dir="${webbuild.dir}" />
</target>
<target name="clean" depends="init">
<delete dir="${build.dir}" />
<delete dir="${ejbjar.dir}" />
<delete dir="${generated.src.dir}" />
<delete dir="${ejbcfg.dir}" />
<delete dir="${webwar.dir}" />
<delete dir="${webbuild.dir}" />
<delete dir="${utilbuild.dir}" />
<delete dir="${utiljar.dir}" />
</target>
<target name="cleanall" depends="init">
<delete dir="${build.dir}" />
<delete dir="${ejbjar.dir}" />
<delete dir="${generated.src.dir}" />
<delete dir="${ejbcfg.dir}" />
<delete dir="${webwar.dir}" />
<delete dir="${webbuild.dir}" />
<delete dir="${utilbuild.dir}" />
<delete dir="${utiljar.dir}" />
<delete includeEmptyDirs="true">
<fileset dir="${ear.dist.dir}" includes="**/*,." />
<fileset dir="${ear.dist.dir}" includes="**/*,." />
<fileset dir="${client.dist.dir}" includes="**/*,." />
</delete>
</target>
<target name="ejbdoclet" depends="prepare">
<taskdef name="ejbdoclet" classname="xdoclet.modules.ejb.EjbDocletTask" classpathref="project.class.path" />
<ejbdoclet destdir="${generated.src.dir}" excludedtags="@version,@author" ejbspec="2.0">
<fileset dir="${ejb.src.dir}/">
<include name="**/*Bean.java" />
</fileset>
<remoteinterface />
<homeinterface />
<localhomeinterface />
<localinterface />
<deploymentdescriptor destdir="${ejbcfg.dir}">
<configParam name="Clientjar" value="${ejbClientJAR}" />
</deploymentdescriptor>
<weblogic xmlencoding="UTF-8" destdir="${ejbcfg.dir}" validatexml="true" />
</ejbdoclet>
</target>
<target name="copysrcfile" depends="ejbdoclet">
<copy todir="${generated.src.dir}">
<fileset dir="${ejb.src.dir}">
<include name="**/*.java" />
</fileset>
</copy>
</target>
<!-- above is gen src and config, below is compile source, jar file, deploy file -->
<target name="compile" depends="copysrcfile">
<javac srcdir="${generated.src.dir}" destdir="${build.dir}" includes="**/*.java" excludes="**/*Client.java" classpathref="project.class.path" />
</target>
<target name="copyejbfiles" depends="copysrcfile">
<mkdir dir="${build.dir}/META-INF" />
<copy todir="${build.dir}/META-INF">
<fileset dir="${ejbcfg.dir}">
<include name="ejb-jar.xml" />
<include name="weblogic-ejb-jar.xml" />
<include name="*.MF" />
</fileset>
</copy>
</target>
<target name="jar_ejb" depends="compile,copyejbfiles">
<delete file="${ejbjar.dir}/${ejbJAR}" />
<jar jarfile="${ejbjar.dir}/${ejbJAR}" basedir="${build.dir}" update="yes">
<manifest>
<attribute name="class-path" value="PresentationUtil.jar" />
</manifest>
</jar>
</target>
<!-- genereate ejb stub files-->
<target name="ejbc" depends="jar_ejb">
<java classname="weblogic.ejbc" fork="yes" failonerror="yes">
<sysproperty key="weblogic.home" value="${WL_HOME}/server" />
<arg line="-verbose -compiler javac ${ejbjar.dir}/${ejbJAR}" />
<classpath refid="project.class.path" />
</java>
<move file="${ejbClientJAR}" tofile="${client.dist.dir}/${ejbClientJAR}" />
</target>
<!-- compile, war the web application -->
<target name="compileweb" depends="init">
<copy todir="${webbuild.dir}" overwrite="yes">
<fileset dir="${web.content.dir}">
<include name="**/*" />
</fileset>
</copy>
<mkdir dir="${webbuild.dir}/WEB-INF/classes" />
<javac srcdir="${web.src.dir}" destdir="${webbuild.dir}/WEB-INF/classes" includes="**/*.java" classpathref="project.class.path" />
</target>
<target name="war_web" depends="compileweb">
<delete file="${webwar.dir}/${webWAR}" />
<jar jarfile="${webwar.dir}/${webWAR}" basedir="${webbuild.dir}" update="yes">
</jar>
</target>
<target name="ear_app" depends="ejbc,war_web">
<mkdir dir="${ear.dist.dir}" />
<ear earfile="${ear.dist.dir}/${appear}" appxml="application.xml">
<fileset dir="${ejbjar.dir}" includes="${ejbJAR}" />
<fileset dir="${webwar.dir}" includes="${webWAR}" />
<fileset dir="${utiljar.dir}" includes="PresentationUtil.jar" />
</ear>
</target>
<target name="deploy" depends="init">
<java classname="weblogic.Deployer" fork="yes" failonerror="yes">
<sysproperty key="weblogic.home" value="${WL_HOME}/server" />
<arg line="-adminurl ${DEVELOPMENTSERVER} -verbose -debug -user ${DEVELOPMENTUSER} -password ${DEVELOPMENTUSERPASSWORD} -activate -upload -name ${APPNAME} -source ${ear.dist.dir}/${appear}" targets="AdminServer" />
<classpath refid="project.class.path" />
</java>
</target>
<!--
<target name="deploy">
<wldeploy action="deploy"
verbose="true" debug="true"
name="MYEJB" source="mystatelessSession.jar"
user="weblogic" password="weblogic"
adminurl="t3://localhost:7001" targets="AdminServer"/>
</target>
-->
<target name="undeploy" depends="init">
<wldeploy action="undeploy" verbose="true" debug="true" name="MyApp" user="weblogic" password="weblogic" adminurl="t3://localhost:7001" targets="AdminServer" />
</target>
<target name="deployWL" depends="init">
<wldeploy action="activate" source="${ear.dist.dir}/${appear}" name="MyApp" user="${DEVELOPMENTUSER}" password="${DEVELOPMENTUSER}" verbose="true" adminurl="t3://localhost:7001" debug="true" targets="AdminServer" />
</target>
<target name="deployTMP" depends="init">
<wldeploy action="activate" source="D:/TEMP/TempWeb.war" name="TempWeb" user="${DEVELOPMENTUSER}" password="${DEVELOPMENTUSER}" verbose="true" adminurl="t3://localhost:7001" debug="true" targets="AdminServer" />
</target>
<target name="undeployTMP" depends="init">
<wldeploy action="undeploy" verbose="true" debug="true" name="TempWeb" user="weblogic" password="weblogic" adminurl="t3://localhost:7001" targets="AdminServer" />
</target>
</project>
上面的ejb.src.dir,web.src.dir根据你自己的情况调整,application.xml,build.properties,build.xml放在同一个文件夹下。