通过ant+jBoss学习MasteringEJB,部署HelloWorld

类别:Java 点击:0 评论:0 推荐:

所需的软件:

ant,j2sdk,j2ee.

我的环境变量设置(我定义在用户变量中):

J2EE_HOME=f:\j2sdkee1.3.1  JAVA_HOME=f:\j2sdk1.4.1_01 JBOSS_HOME=f:\jboss

Path=.;f:\j2sdk1.4.1_01\bin;f:\j2sdkee1.3.1\bin;f:\ant1.5.2\bin

CLASSPATH=.;f:\j2sdk1.4.1_01\lib\dt.jar;f:\j2sdk1.4.1_01\lib\tools.jar;f:\j2sdkee1.3.1\lib\j2ee.jar

我的模板项目的结构和文件内容,便于以后拷贝。

 

build.xml

<?xml version="1.0"?>

 

<!-- ======================================================================= -->

<!-- Template build file                                                                               -->

<!-- ======================================================================= -->

 

<project name="template" default="main" basedir=".">

  

   <!--

      Give user a chance to override without editing this file

      (and without typing -D each time they run it)

   -->

   <property file=".ant.properties" />

   <property file="${user.home}/.ant.properties" />

  

   <property name="Name" value="Template"/>

   <property name="version" value="1.1"/>

  

   <target name="check-jboss" unless="jboss.home">

      <fail>

         Property "jboss.home" is not set. Please use the file

         ".ant.properties" in this directory ${basedir} to

         set this property. It must point to the directory which

         contains the following directory: "deploy", "conf", "tmp"

         etc.

      </fail>

   </target>

  

   <target name="wrong-jboss" unless="jboss.present">

      <fail>

         Property "jboss.home" is set but it does not seem

         to point to the right directory. The file "run.jar"

         must be available at ${jboss.home}/bin.

      </fail>

   </target>  

  

   <target name="check-environment">

      <antcall target="check-jboss"/>

      <available property="jboss.present" file="${jboss.home}/bin/run.jar"/>

      <antcall target="wrong-jboss"/>                 

   </target>

  

   <target name="init" depends="check-environment">

      <echo message="build.compiler = ${build.compiler}"/>

      <echo message="user.home = ${user.home}"/>

      <echo message="java.home = ${java.home}"/>

      <echo message="ant.home = ${ant.home}"/>

      <echo message="jboss.home = ${jboss.home}"/>     

      <echo message="java.class.path = ${java.class.path}"/>

      <echo message=""/>     

   </target>

  

   <property name="jboss.lib" value="${jboss.home}/lib" />

   <property name="jboss.client" value="${jboss.home}/client" />

   <!-- Configuration used on JBoss 3 to run your server. There must be

        a directory with the same name under "${jboss.home}/server" -->

   <property name="jboss.configuration" value="default" />

   <property name="jboss.deploy" value="${jboss.home}/server/${jboss.configuration}/deploy" />

   <property name="src.dir" value="${basedir}/src"/>

   <property name="src.main.dir" value="${src.dir}/main"/>

   <property name="src.client.dir" value="${src.main.dir}/client"/>

   <property name="src.ejb.dir" value="${src.main.dir}/ejb"/>  

   <property name="src.etc.dir" value="${src.dir}/etc"/>  

   <property name="src.resources" value="${src.dir}/resources"/>

   <property name="resources" value="${basedir}/resources"/>

   <property name="build.dir" value="${basedir}/build"/>

   <property name="build.tmp.dir" value="${build.dir}/tmp"/>

   <property name="build.deploy.dir" value="${build.dir}/deploy"/>

   <property name="build.generate.dir" value="${build.dir}/generate"/>

   <property name="build.classes.dir" value="${build.dir}/classes"/>

   <property name="build.war.dir" value="${build.dir}/war"/>

   <property name="build.client.dir" value="${build.dir}/client"/>

   <property name="build.bin.dir" value="${build.dir}/bin"/>

   <property name="build.javadocs.dir" value="${build.dir}/docs/api"/>

  

  

  

   <path id="base.path">     

      <pathelement location="${jboss.client}/jboss-j2ee.jar" />

      <pathelement location="${jboss.client}/jnp-client.jar" />

      <pathelement location="${jboss.client}/jbossmq-client.jar" />

      <pathelement location="${jboss.client}/jbosssx-client.jar" />

      <pathelement location="${jboss.client}/concurrent.jar" />

      <pathelement location="${jboss.client}/jaas.jar" />

      <pathelement location="${jboss.lib}/jboss-jmx.jar" />

      <pathelement location="${jboss.home}/server/${jboss.configuration}/lib/jbosssx.jar" />

      <pathelement location="${jboss.home}/server/${jboss.configuration}/lib/mail.jar" />

      <pathelement location="${build.classes.dir}" />

   </path>

  

   <!-- =================================================================== -->

   <!-- Generates the necessary EJB classes and deployment descriptors      -->

   <!-- =================================================================== -->

     

   <!-- =================================================================== -->

   <!-- Compiles the source code                                            -->

   <!-- =================================================================== -->

  

   <target name="compile" depends="init">

      <mkdir dir="${build.classes.dir}"/>     

      <javac

         srcdir="${src.ejb.dir}"

         destdir="${build.classes.dir}"

         debug="on"

         deprecation="off"

         optimize="on"

         classpathref="base.path"

      >        

      </javac>     

      <javac

         srcdir="${src.client.dir}"

         destdir="${build.classes.dir}"

         debug="on"

         deprecation="off"

         optimize="on"        

         classpathref="base.path"

      >

      </javac>

   </target>

  

   <!-- =================================================================== -->

   <!-- Creates the jar archives                                            -->

   <!-- =================================================================== -->

  

   <target name="jar" depends="compile">

      <mkdir dir="${build.deploy.dir}"/>     

      <mkdir dir="${build.bin.dir}"/>

      <mkdir dir="${build.client.dir}"/>

      <jar

         jarfile="${build.deploy.dir}/ejb-test.jar"

      >

         <fileset

            dir="${build.classes.dir}"                

            excludes="*Client.*"      

         >

         </fileset>        

         <fileset

            dir="${resources}/ejb-jar"

            includes="META-INF/**"

         >

         </fileset>

      </jar>     

      <jar

         jarfile="${build.client.dir}/client-test.jar"

      >

         <fileset

            dir="${build.classes.dir}"

            excludes="*Bean.*"

         >

         </fileset>

      </jar>           

   </target>  

  

   <!-- =================================================================== -->

   <!-- Creates the client binary                                           -->

   <!-- =================================================================== -->

  

   <target name="deploy-server" depends="jar">

      <copy todir="${jboss.deploy}">

         <fileset dir="${build.deploy.dir}" includes="*.jar,*.war,*.ear">

         </fileset>

      </copy>

   </target>

  

   <!-- =================================================================== -->

   <!-- Creates the client binary                                           -->

   <!-- =================================================================== -->

  

   <target name="create-client" depends="jar">

      <!-- Convert the given paths to Windows -->

      <pathconvert targetos="windows" property="jboss.home.on.windows" >

         <path>

            <pathelement location="${jboss.home}" />

         </path>

      </pathconvert>

      <pathconvert targetos="windows" property="java.home.on.windows" >

         <path>

            <pathelement location="${java.home}" />

         </path>

      </pathconvert>     

      <filter token="jboss.home" value="${jboss.home.on.windows}"/>

      <filter token="java.home" value="${java.home.on.windows}"/>

      <copy todir="${build.bin.dir}" filtering="yes">

         <fileset dir="${src.etc.dir}/bin" includes="run-*client.bat">

         </fileset>

      </copy>

      <copy file="${src.etc.dir}/bin/lcp.bat" todir="${build.bin.dir}"/>     

      <copy file="${src.etc.dir}/jndi.properties" todir="${build.bin.dir}"/>

   </target>

  

   <!-- =================================================================== -->

   <!-- Creates the binary structure                                        -->

   <!-- =================================================================== -->

  

   <target name="main" depends="deploy-server,create-client">

   </target>

 

   <!-- =================================================================== -->

   <!-- Cleans up the current build                                         -->

   <!-- =================================================================== -->

  

   <target name="clean" depends="init">

      <delete dir="${build.dir}"/>

   </target>

 

</project>

 

jboss.xml

<?xml version="1.0" encoding="UTF-8"?>

<jboss>

   <enterprise-beans>

      <session>

         <ejb-name>Hello</ejb-name>

         <jndi-name>HelloHomeRemote</jndi-name>

      </session>     

   </enterprise-beans>

</jboss>

 

ejb-jar.xml

<!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN" "http://java.sun.com/dtd/ejb-jar_2_0.dtd">

 

<ejb-jar>

 <enterprise-beans>

  <session>

   <ejb-name>Hello</ejb-name>

   <home>HelloHome</home>

   <remote>Hello</remote>  

   <ejb-class>HelloBean</ejb-class>

   <session-type>Stateless</session-type>

   <transaction-type>Container</transaction-type>

  </session>

 </enterprise-beans>

</ejb-jar>

.ant.properties

# ATTENTION: this is an example file how to overwrite settings in this project

# Please rename it to ".ant.properties" and adjust the settings to your needs

 

# Set the path to the runtime JBoss directory containing the JBoss application server

# ATTENTION: the one containing directories like "bin", "client", "server" etc.

jboss.home=f:/jboss

ant.home=f:/ant1.5.2

# Set the configuration name that must have a corresponding directory under

# <jboss.home>/server

jboss.configuration=default

 

目录结构:

 

    

 

resources\ejb-jar\META-INF下:

src\etc\bin下:

src\etc\WEB-INF下:

template下:

备注:1.如果要在.xml文件中加中文注释,把encoding=”GB2312”.

2.关于ant的用法可参看《Java深度历险》

3.关于.xml文件中属性的意义可参看:《MasteringEJB 2》

4. JBoss的模板http://sourceforge.net/project/showfiles.php?group_id=22866&release_id=111113

5.

本文地址:http://com.8s8s.com/it/it17912.htm