Although I am pretty familiar 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
- 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
- modify the ‘persistence.xml’
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:
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:
/usr/local/jboss
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.
Hi Pascal
This is very interesting article. I am a newbie and trying to make this work.
Can you please update step 1, what are the groupId, artifactId, version and package that you entered?
In step 3, when I try to add in my settings.xml as
C:jboss-5.0.1.GA
I am getting the error
Error reading settings.xml: Unrecognised tag: ‘jboss.home’ (position: START_TAG
seen rnt… @2:14)
Line: 2
Column: 14
I am using Maven version: 2.0.10. Is there anything that needs to be done?
Thanks
Kevin
My local settings.xml is as follows
C:jboss5.0.1.GA
There is a problem in posting xml tags.
My local settings.xml file is something similar to this one.
settings
jboss.home C:jboss-5.0.1.GA jboss.home
settings
Hi Kevin,
I am looking how to allow XML tags in the comments but haven’t found out yet.
About your questions:
The properties in your settings.xml must be surrounded with a ‘properties’ tag.
ABout the parameters in step 1: this is up to you what you fill in. This will be used by Maven to create your project with these values. If this ‘generate’ archetype isn’t working for you you can use the ‘old’ version: ‘create’ archetype as described here: http://maven.apache.org/plugins/maven-archetype-plugin-1.0-alpha-7/examples/simple.html
Hi Kevin,
I am looking how to allow XML tags in the comments but haven’t found out yet.
About your questions:
The properties in your settings.xml must be surrounded with a ‘properties’ tag.
ABout the parameters in step 1: this is up to you what you fill in. This will be used by Maven to create your project with these values. If this ‘generate’ archetype isn’t working for you you can use the ‘old’ version: ‘create’ archetype as described here: http://maven.apache.org/plugins/maven-archetype-plugin-1.0-alpha-7/examples/simple.html
Thank you Pascal. That helped me.
Now I have another problem. when I try to compile with command
mvn compile
I am getting the following error
annotations are not supported in -source 1.3 (use -source 5 or higher to enable annotations)
I am using jdk 1.6.
I am not that familiar with maven. It seems there is someplace where I need to set the jdk version. Can you please let me know the file name and what needs to be modified?
Thanks
Thank you Pascal. That helped me.
Now I have another problem. when I try to compile with command
mvn compile
I am getting the following error
annotations are not supported in -source 1.3 (use -source 5 or higher to enable annotations)
I am using jdk 1.6.
I am not that familiar with maven. It seems there is someplace where I need to set the jdk version. Can you please let me know the file name and what needs to be modified?
Thanks
Thank you very much. That helps.
Thank you very much. That helps.