用一个简单的ejb3.0示例记录ant打ear包的过程
源码下载
环境:
apache-ant-1.6.5
jboss-4.2.1.GA
jboss-annotations-ejb3.jar
jboss-ejb3.jar
jboss-ejb3x.jar
最后三个包在jboss里有.
1,建ejb3.0项目
session bean接口:HelloWorld
public interface HelloWorld {
public String sayHello(String name);
}
实现类:HelloWorldBean
@Stateless
@Remote ({HelloWorld.class})
public class HelloWorldBean implements HelloWorld {
public String sayHello(String name) {
//org.apache.commons.collections,这里假设用到了其它包中的类
CollectionUtils c=new CollectionUtils();
//org.apache.commons.lang
StringUtils s=new StringUtils();
return "hello,i am "+name;
}
}
调用session bean的jsp:
<%
//org.apache.commons.collections,这里假设用到了其它包中的类
CollectionUtils c=new CollectionUtils();
//org.apache.commons.lang
StringUtils s=new StringUtils();
%>
<%
Properties props = new Properties();
props.setProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
props.setProperty("java.naming.provider.url", "localhost:1099");
props.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming");
InitialContext ctx;
try {
ctx = new InitialContext(props);
HelloWorld helloworld = (HelloWorld) ctx.lookup("myproject/HelloWorldBean/remote");
out.println(helloworld.sayHello("mashiguang"));
} catch (NamingException e) {
out.println(e.getMessage());
}
%>
2,ant示例
把一些公用的jar包都放到ear下,而不是分别在ejb jar包和war包里,减少jar包的冗余.
<project name="ear" default="deploy" basedir=".">
<property environment="env" />
<property name="app.dir" value="${basedir}" />
<property name="src.dir" value="${app.dir}\src" />
<property name="classes.dir" value="${app.dir}\build\classes" />
<property name="webcontent.dir" value="${app.dir}\WebContent" />
<property name="jboss.home" value="${env.JBOSS_HOME}" />
<property name="jboss.server.config" value="default" />
<path id="build.classpath" description="编译时classpath">
<fileset dir="${webcontent.dir}\WEB-INF\lib">
<include name="*.jar" />
</fileset>
<pathelement location="${classes.dir}" />
</path>
<target name="prepare" depends="clean" description="创建classes文件夹">
<mkdir dir="${classes.dir}" />
</target>
<target name="compile" depends="prepare" description="编绎">
<javac srcdir="${src.dir}" destdir="${classes.dir}" debug="on" deprecation="on" optimize="off" includes="**">
<classpath refid="build.classpath" />
</javac>
</target>
<target name="sessionInterface" depends="compile" description="创建session bean接口包">
<jar destfile="${app.dir}/interface.jar">
<fileset dir="${classes.dir}">
<include name="com/mashiguang/ejb3/*.class" />
</fileset>
</jar>
</target>
<target name="ejb_jar" depends="sessionInterface" description="创建ejb jar包">
<jar destfile="${app.dir}/ejb.jar">
<fileset dir="${classes.dir}">
<include name="com/mashiguang/ejb3/impl/**" />
</fileset>
<manifest>
<attribute name="Class-Path" value="interface.jar commons-collections-3.2.jar commons-lang-2.3.jar"/>
</manifest>
</jar>
</target>
<target name="web-war" depends="ejb_jar" description="创建WEB发布包">
<war destfile="${app.dir}/web.war" webxml="${webcontent.dir}/WEB-INF/web.xml">
<fileset dir="${webcontent.dir}">
<include name="*.jsp"/>
<exclude name="WEB-INF/web.xml"/>
<exclude name="WEB-INF/lib/**"/>
</fileset>
<manifest>
<attribute name="Class-Path" value="interface.jar commons-collections-3.2.jar commons-lang-2.3.jar"/>
</manifest>
</war>
</target>
<target name="ear" depends="web-war" description="创建WEB发布包">
<ear destfile="${app.dir}/myproject.ear" appxml="${src.dir}/META-INF/application.xml">
<fileset dir="${app.dir}">
<include name="ejb.jar"/>
<include name="web.war"/>
<include name="interface.jar"/>
</fileset>
<fileset dir="${webcontent.dir}/WEB-INF/lib" >
<include name="commons*.jar"/>
</fileset>
</ear>
</target>
<target name="deploy" depends="ear">
<copy file="${app.dir}/myproject.ear" todir="${jboss.home}/server/${jboss.server.config}/deploy" />
</target>
<target name="clean">
<delete>
<fileset dir="${app.dir}">
<include name="ejb.jar"/>
<include name="web.war"/>
<include name="interface.jar"/>
</fileset>
</delete>
<delete dir="${classes.dir}" />
<!-- -->
<delete file="${jboss.home}/server/${jboss.server.config}/deploy/myproject.ear" />
</target>
</project>