同样使用前面的第一个EJB程序HelloWorld(无状态会话bean),如果我们每次都要打开jboss安装目录下的bin下的run来启动jboss,同时我们还要在myeclipse中使用自带的向导来打包程序,再切换窗口把jar包复制到jboss安装目录下的server\default\deploy,那样是不是真的很麻烦啊?是的,而且还要浪费我们的时间,大大降低了我们的开发效率,所以,为了提高我们的开发效率,降低成本,所以我们首先应该把JBoss配置到myeclipse中。
1)首先,打开myeclipse,通过window------>preference------>myeclipse------>serverce------->jboss,选中你的JBOSS版本,我在此使用的是5的,所以我就用5的作为例子。
配置好后,点击OK确认后,通过Servers视图,我们可以很好的操作JBOSS的运行和关闭,节省了切换窗口的时间。
如果你发现你找不到Servers视图,你可以通过window----->Show View ------->Others,在查询输入框中输入Servers,找到以后选中点击OK即可发现Servers出现在在myeclipse视图中啦。
弄好这些以后。我们需要在项目的根目录下新建一个build.xml,用以配置Ant。
其中build.xml的代码如下。
<?xml version="1.0" encoding="UTF-8"?> <!-- name中指的是项目名称。basedir指的是与build.xml的同级目录 --> <project name="EJB_HelloWorld" basedir="."> <property name="src.dir" value="${basedir}\src" /> <!-- 指向环境变量中系统变量 --> <property environment="env" /> <!-- 指向系统变量中的JBOSS_HOME变量 ,可以得知JBOSS_HOME的安装地址 --> <property name="jboss.home" value="${env.JBOSS_HOME}" /> <property name="jboss.server.config" value="default" /> <property name="build.dir" value="${basedir}\build" /> <path id="build.classpath"> <fileset dir="${jboss.home}\client"> <include name="*.jar" /> </fileset> <pathelement location="${build.dir}" /> </path> <target name="prepare"> <delete dir="${build.dir}" /> <mkdir dir="${build.dir}" /> </target> <target name="compile" depends="prepare" description="编译"> <javac srcdir="${src.dir}" destdir="${build.dir}" > <classpath refid="build.classpath" /> </javac> </target> <target name="ejbjar" depends="compile" description="创建EJB发布包"> <jar jarfile="${basedir}\${ant.project.name}.jar"> <fileset dir="${build.dir}"> <include name="**/*.class" /> </fileset> </jar> </target> <target name="deploy" depends="ejbjar" description="发布ejb"> <copy file="${basedir}\${ant.project.name}.jar" todir="${jboss.home}\server\${jboss.server.config}\deploy" /> </target> <target name="undeploy" description="卸载ejb"> <delete file="${jboss.home}\server\${jboss.server.config}\deploy\${ant.project.name}.jar" /> </target> </project>
打好以后。我们在outline视图中选择你要运行的步骤。右击------>Run As ----- --->Ant Build.
例如我要运行编译compile。则右击运行以后,
出现运行成功。
如果运行的是deploy,则在控制台上会出现部署信息(版本不同可能信息有点差别,这不是重点)。
如果在编译什么的过程中出现以下警告:
Buildfile: E:\Workspaces\EJB_HelloWorld\build.xml
prepare:
[delete] Deleting directory E:\Workspaces\EJB_HelloWorld\build
[mkdir] Created dir: E:\Workspaces\EJB_HelloWorld\build
compile:
[javac] E:\Workspaces\EJB_HelloWorld\build.xml:25: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds
[javac] Compiling 4 source files to E:\Workspaces\EJB_HelloWorld\build
BUILD SUCCESSFUL
Total time: 2 seconds
对于 includeantruntime,ANT官方文档对该属性的说明(该段摘自网络):
Whether to include the Ant run-time libraries in the classpath; defaults to yes
, unless build.sysclasspath
is set. It is usually best to set this to false so the script's behavior is not sensitive to the environment in which it is run.
所以应该在第25行(按找我上面的代码中的行数)添加上includeantruntime="on"(官方文档建议设置为false),再次运行就不会再有这个警告啦。
通过上面的说明,是不是发觉这种方式快速很多而且节约时间咧,呵呵,那就赶紧为你对电脑也这样配置一下吧。