Although I am pretty familair with EJB3 I decided to get prepared for my next certificate SCBCD by going through the book ‘Enterprise JavaBeans 3.0‘ by Burke and Monson-Haefel. A good thing about the book is that they added a ‘workbook’ in which exercises and coding examples are described. However, there is also a ‘disadvantage’ and that is that they use Ant to do the building, deploying and running of the code. And they use JBoss4.0 as an application server, which was the logical choice when the book came out, I guess, but is outdated nowadays. So I decided to rewrite the example a little as an exercise by making use of Maven2 for the building and deploying and do this on JBoss5.0.1 that has been released a few weeks ago.
While creating the Maven builds for the first chapter I discovered there are actually several ways to do this, depending on the extend you want to use Maven for this:
- Minimal use of Maven
- Medium use of Maven
- Optimal use of Maven
In this option I only use Maven to create the ejb-jar. The deployment and the execution of the client to test the EJB is done manually.
I use Maven to create the ejb-jar file, a separate ear file and I deploy it with Maven at a running JBoss container. Running the client is still done ‘manually’. Ear project can be reused to deploy ejb jars of other chapters.
I extend the setup by performing the unit test in Maven by making use of OpenEJB before it is deployed to JBoss. This way I am sure the bean is working correctly when it is deployed on JBoss.
In this post I describe the steps for option 1.
Steps to make this work:
- create a Maven project
- copy code to maven code dir
Next step is to copy the (re)sources to the place where Maven2 expects it (also a good point to remove the default generated classes App.java and AppTest.java. You will get a setup like this:
- modify pom
This steps is obvious. Just create a standard Maven project with the command
mvn archetype:generate like described in detail here.
Next step is to enrich the pom with all dependencies necessary to compile the code. And these are a lot of dependencies when using JBoss to compile against. The complete pom can be found
here. I have added this property to my settings.xml as it is used in the pom:
By using the new JBoss version it appears that the original persisitence.xml isn’t working anymore. I had to add the following to it:
xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0"
If you don’t add this I got the error:
DEPLOYMENTS IN ERROR:
Deployment “vfszip:/usr/local/jboss-5.0.1.GA/server/default/deploy/ch4.jar/” is in error due to the following reason(s): org.jboss.xb.binding.JBossXBRuntimeException: Failed to resolve schema nsURI= location=persistence
That’s it. You can now build an ejb-jar file and copy it to the JBoss instance by hand. And you can run a client program to call your bean.
In the next post I will show you how you can use Maven to perform the deployment for you and how you can use a ‘standalone’ client to test your ejb deployment in Netbeans.