EJB3 with JBoss5 and Maven2

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:

  1. Minimal use of Maven
  2. 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.

  3. Medium use of Maven
  4. 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.

  5. Optimal use of Maven
  6. 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
    • This steps is obvious. Just create a standard Maven project with the command mvn archetype:generate like described in detail here.

    • 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
    • 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
    • modify the ‘persistence.xml’
    • 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.

    Advertisement

About Pascal Alma

Pascal is a senior IT consultant and has been working in IT since 1997. He is monitoring the latest development in new technologies (Mobile, Cloud, Big Data) closely and particularly interested in Java open source tool stacks, cloud related technologies like AWS and mobile development like building iOS apps with Swift. Specialties: Java/JEE/Spring Amazon AWS API/REST Big Data Continuous Delivery Swift/iOS
This entry was posted in EJB3, JBoss, Maven and tagged , , . Bookmark the permalink.

11 Responses to EJB3 with JBoss5 and Maven2

  1. Kevin says:

    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

  2. Kevin says:

    My local settings.xml is as follows

    C:jboss5.0.1.GA

  3. Kevin says:

    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

  4. Pascal Alma says:

    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

  5. Pascal Alma says:

    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

  6. Kevin says:

    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

  7. Kevin says:

    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

  8. Kevin says:

    Thank you very much. That helps.

  9. Kevin says:

    Thank you very much. That helps.

Comments are closed.