In this EJB3 Maven tutorial, we will see how to build and deploy EJB3 in JBoss AS 7 using m2eclipse plugin which provides Maven integration in Eclipse IDE. This tutorial is split into 2 parts with this part containing the steps to develop and deploy the EJB in JBoss AS using Maven whereas the next part shows how to create and run the EJB3 remote client using Maven.
Contents
1 Environment Used2 Project Description3 Creating New Maven Project in Eclipse4 Creating Session Bean and Bean Interface 4.1 Bean Interface4.2 Bean Class 5 Configuring Maven pom.xml for EJB3 5.1 Complete pom.xml5.2 Maven Plugins used5.3 Required Maven Dependencies for EJB3 6 Install EJB3 in local Maven repository7 Deploy EJB3 JAR in JBoss AS8 Project Folder StructureSetting up development environment: Read this page for installing and setting up the environment for creating Maven projects in Eclipse using m2eclipse plugin.
This screen prompts for project workspace location. Make sure “Use default Workspace location” and “Create a simple project (skip archetype selection)” are selected and click Next.
We skip the archetype selection because you can copy paste the pom.xml provided below. Otherwise you can select a “maven quickstart” or any “java ee” archetypes.
Now we have to configure the project by enter the following details and clickFinish. Enter Group Id: as “com.theopentutorials.ejb3“ Enter Artifact Id as “ejbmavendemo“ Enter Version: as “1.0-SNAPSHOT“ Packaging: jar Name: EJB3 Maven DemoRight-click the project, select Properties. Click on “Project Facets” and click “Convert to faceted form”. Check the “EJB Module” and select the version as 3.0 or 3.1 Put Eclipse IDE in Java EE perspective. (Window menu -> Open Perspective)
Copy the following code in the bean interface and save it.
package com.theopentutorials.ejb3; import javax.ejb.Remote; @Remote public interface HelloWorld { public String sayHello(); }Copy the following code in the bean class and save it.
package com.theopentutorials.ejb3; import javax.ejb.Stateless; @Stateless public class HelloWorldBean implements HelloWorld { public HelloWorldBean() { } public String sayHello() { return "Hello World !!!"; } }Complete pom.xml is shown below. Open your pom.xml and copy-paste the following content.
<?xml version="1.0"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.theopentutorials.ejb3</groupId> <artifactId>ejbmavendemo</artifactId> <version>1.0-SNAPSHOT</version> <name>EJB3 Maven Demo</name> <packaging>ejb</packaging> <properties> <!-- Explicitly declaring the source encoding eliminates the following message: --> <!-- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! --> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <!-- JBoss dependency versions --> <version.org.jboss.as.plugins.maven.plugin>7.3.Final</version.org.jboss.as.plugins.maven.plugin> <version.org.jboss.spec.jboss.javaee.6.0>3.0.0.Final</version.org.jboss.spec.jboss.javaee.6.0> <!-- other plugin versions --> <version.compiler.plugin>2.3.1</version.compiler.plugin> <version.ejb.plugin>2.3</version.ejb.plugin> <!-- maven-compiler-plugin --> <maven.compiler.target>1.6</maven.compiler.target> <maven.compiler.source>1.6</maven.compiler.source> <!-- Optional: to use jboss-as:run goal --> <!--<jboss-as.home>C:\Users\iByteCode\Desktop\jboss-as-7.1.0.Final</jboss-as.home> --> </properties> <dependencyManagement> <dependencies> <!-- Define the version of JBoss' Java EE 6 APIs we want to use --> <!-- JBoss distributes a complete set of Java EE 6 APIs including a Bill of Materials (BOM). A BOM specifies the versions of a "stack" (or a collection) of artifacts. We use this here so that we always get the correct versions of artifacts. Here we use the jboss-javaee-6.0 stack (you can read this as the JBoss stack of the Java EE 6 APIs). You can actually use this stack with any version of JBoss AS that implements Java EE 6, not just JBoss AS 7! --> <dependency> <groupId>org.jboss.spec</groupId> <artifactId>jboss-javaee-6.0</artifactId> <version>${version.org.jboss.spec.jboss.javaee.6.0}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <!-- Import the Common Annotations API (JSR-250), we use provided scope as the API is included in JBoss AS 7 --> <dependency> <groupId>org.jboss.spec.javax.annotation</groupId> <artifactId>jboss-annotations-api_1.1_spec</artifactId> <scope>provided</scope> </dependency> <!-- Import the EJB 3.1 API, we use provided scope as the API is included in JBoss AS 7 --> <dependency> <groupId>org.jboss.spec.javax.ejb</groupId> <artifactId>jboss-ejb-api_3.1_spec</artifactId> <scope>provided</scope> </dependency> </dependencies> <build> <!-- Set the name of the deployment --> <plugins> <!-- JBoss AS plugin to deploy the application --> <plugin> <groupId>org.jboss.as.plugins</groupId> <artifactId>jboss-as-maven-plugin</artifactId> <version>${version.org.jboss.as.plugins.maven.plugin}</version> <configuration> <filename>${project.build.finalName}.jar</filename> </configuration> </plugin> <!-- Compiler plugin enforces Java 1.6 compatibility and activates annotation processors --> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>${version.compiler.plugin}</version> <configuration> <source>${maven.compiler.source}</source> <target>${maven.compiler.target}</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-ejb-plugin</artifactId> <version>${version.ejb.plugin}</version> <configuration> <ejbVersion>3.1</ejbVersion> <!-- this is false by default --> <generateClient>true</generateClient> </configuration> </plugin> </plugins> </build> </project>1. maven-compiler-plugin The maven-compiler-plugin is used to compile the sources of your project. In the above pom.xml we have used the version 2.3.1 of the plugin with the source and target JDK set to 1.6 under configuration.
We have defined these settings as properties inside <properties> tag and referring it through ${property}.
<version.compiler.plugin>2.3.1</version.compiler.plugin> <!-- maven-compiler-plugin --> <maven.compiler.target>1.6</maven.compiler.target> <maven.compiler.source>1.6</maven.compiler.source>2. maven-ejb-plugin This plugin generates J2EE Enterprise Javabean (EJB) file as well as the associated client jar. We specify the ejb version as 3.1 and request the plugin to generate the client by setting the “generateClient” property to true.
3. jboss-as-maven-plugin The jboss-as-maven-plugin is used to deploy, redeploy, undeploy or run your application in JBoss AS. Under the configuration we specify the build file name same as the project build filename which is by default of the form “artifactid-version” in our case “ejbmavendemo-1.0-SNAPSHOT”.
1. jboss-javaee-6.0 Defines the version of JBoss’ Java EE 6 APIs we want to use. JBoss distributes a complete set of Java EE 6 APIs including a Bill of Materials (BOM). A BOM specifies the versions of a “stack” (or a collection) of artifacts. We specify this in <dependencyManagement> tag so that we always get the correct versions of artifacts. The type of this dependency itself a pom which contains the required dependencies.
<dependency> <groupId>org.jboss.spec</groupId> <artifactId>jboss-javaee-6.0</artifactId> <version>${version.org.jboss.spec.jboss.javaee.6.0}</version> <type>pom</type> <scope>import</scope> </dependency>2. jboss-annotations-api_1.1_spec
<dependency> <groupId>org.jboss.spec.javax.annotation</groupId> <artifactId>jboss-annotations-api_1.1_spec</artifactId> <scope>provided</scope> </dependency>3. jboss-ejb-api_3.1_spec
<dependency> <groupId>org.jboss.spec.javax.ejb</groupId> <artifactId>jboss-ejb-api_3.1_spec</artifactId> <scope>provided</scope> </dependency>Whenever the pom.xml is changed, update the project to reflect the changes made. To update, Right click on your project in Package Explorer -> Maven -> Update Project … as shown below.
The next step is to build the EJB and client interfaces JARs and install them in your local Maven repository. To do that we use maven’s clean and install phase.
Right-click your pom.xml and select “Run As -> Maven build…” and type the goal as “clean install” as shown below.
You can check your local maven repository (for example, username/.m2/repository/com/theopentutorials) folder which should contain the ejbmavendemo-1.0-SNAPSHOT.jar and client jar file.
When executing this “clean install” if you face any problem such as
Unable to locate the Javac Compiler in: C:\Program Files\Java\jre6\..\lib\tools.jar. Please ensure you are using JDK 1.4 or above and not a JRE (the com.sun.tools.javac.Main class is required) OR Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.1:compile (default-compile) on project ejbmavendemo: Compilation failure
then we need to specify the JRE directory to point to JDK instead of JRE directory. To do that, go to Eclipse menu Windows menu -> Preferences -> Java -> Installed JREs. Select the Installed JRE from right pane and click on Edit. For JRE home: select JDK directory and click Finish and OK.
Method 1: using jboss-as:deploy maven goal The JBoss AS server should be running before we can deploy. “jboss-as:deploy” maven goal deploys the application in JBoss server.
Start JBoss Application Server either using command line or inside Eclipse Servers. Right click your pom.xml -> Run as -> Maven Build …. In the Goals: typejboss-as:deploy as shown below. Click on Apply and Run.
Method 2: using jboss-as:run maven goal You can also use “jboss-as:run” maven goal which starts the application server and deploys your application. But for this “run” goal, we need to add a property (jboss-as.home) in <properties> tag mentioning your “JBOSS_HOME” directory. This is already mentioned in the complete pom.xml (provided above) under <properties> tag. Just uncomment the below given line in that file and mention your JBOSS_HOME directory. If you did not specify the jboss-as.home property then the plugin will download the entire JBoss AS from their site.
<!-- Optional: to use jboss-as:run goal --> <jboss-as.home>C:\Users\iByteCode\Desktop\jboss-as-7.1.0.Final</jboss-as.home> Right click your pom.xml -> Run as -> Maven Build … In the Goals: type jboss-as:run. Click on Apply and Run.You can use Web Console to check whether the EJB3 application JAR has been installed in JBoss. Use the URL http://localhost:9990/ and select Manage Deployments under Deployments. You may need a management realm username/password to login. To do so refer this link.
The complete folder structure of this project is shown below.
In the next part of this tutorial we will see how to create a ejb3 client and run the application using Maven in Eclipse.
Contents [hide]
1 Create New Maven Project in Eclipse2 Create EJB3 client3 EJB3 client pom.xml 3.1 Maven Plugins used3.2 Maven Dependencies for EJB3 client 4 JBoss EJB Client properties5 Project Structure6 Run the project 6.1 OutputRight click on Project Explorer -> New -> Other… -> Maven -> Maven Project File menu -> New -> Other… -> Maven -> Maven Project Click on the down arrow on New icon on toolbar -> Other… -> Maven -> Maven Project Select Maven Project and click Next.
This screen prompts for project workspace location. Make sure “Use default Workspace location” and “Create a simple project (skip archetype selection)” are selected and click Next.
Now we have to configure the project by entering the following details and clickFinish. Enter Group Id: as “com.theopentutorials.ejb3.client“ Enter Artifact Id as “ejbclientmavendemo“ Enter Version: as “1.0-SNAPSHOT“ Packaging: jar Name: EJB3 Client Maven
Copy paste the following in your pom.xml file.
001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 050 051 052 053 054 055 056 057 058 059 060 061 062 063 064 065 066 067 068 069 070 071 072 073 074 075 076 077 078 079 080 081 082 083 084 085 086 087 088 089 090 091 092 093 094 095 096 097 098 099 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 <? xml version = "1.0" ?> < project xmlns = "http://maven.apache.org/POM/4.0.0" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd" > < modelVersion >4.0.0</ modelVersion > < groupId >com.theopentutorials.ejb3.client</ groupId > < artifactId >ejbclientmavendemo</ artifactId > < version >1.0-SNAPSHOT</ version > < name >EJB3 Client Maven</ name > < packaging >jar</ packaging > < properties > <!-- Explicitly declaring the source encoding eliminates the following message: --> <!-- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! --> < project.build.sourceEncoding >UTF-8</ project.build.sourceEncoding > <!-- JBoss dependency versions --> < version.org.jboss.as >7.1.1.Final</ version.org.jboss.as > < version.org.jboss.as.plugins.maven.plugin >7.3.Final</ version.org.jboss.as.plugins.maven.plugin > < version.org.jboss.spec.jboss.javaee.6.0 >3.0.0.Final</ version.org.jboss.spec.jboss.javaee.6.0 > <!-- other plugin versions --> < version.compiler.plugin >2.3.1</ version.compiler.plugin > < version.exec.plugin >1.2.1</ version.exec.plugin > <!-- maven-compiler-plugin --> < maven.compiler.target >1.6</ maven.compiler.target > < maven.compiler.source >1.6</ maven.compiler.source > </ properties > < dependencyManagement > < dependencies > <!-- Define the version of JBoss' Java EE 6 APIs we want to use --> <!-- JBoss distributes a complete set of Java EE 6 APIs including a Bill of Materials (BOM). A BOM specifies the versions of a "stack" (or a collection) of artifacts. We use this here so that we always get the correct versions of artifacts. Here we use the jboss-javaee-6.0 stack (you can read this as the JBoss stack of the Java EE 6 APIs). You can actually use this stack with any version of JBoss AS that implements Java EE 6, not just JBoss AS 7! --> < dependency > < groupId >org.jboss.spec</ groupId > < artifactId >jboss-javaee-6.0</ artifactId > < version >${version.org.jboss.spec.jboss.javaee.6.0}</ version > < type >pom</ type > < scope >import</ scope > </ dependency > < dependency > < groupId >org.jboss.as</ groupId > < artifactId >jboss-as-ejb-client-bom</ artifactId > < version >${version.org.jboss.as}</ version > < type >pom</ type > < scope >import</ scope > </ dependency > </ dependencies > </ dependencyManagement > < dependencies > <!-- Import the transaction spec API, we use runtime scope because we aren't using any direct reference to the spec API in our client code --> < dependency > < groupId >org.jboss.spec.javax.transaction</ groupId > < artifactId >jboss-transaction-api_1.1_spec</ artifactId > < scope >runtime</ scope > </ dependency > <!-- Import the EJB 3.1 API, we use runtime scope because we aren't using any direct reference to EJB spec API in our client code --> < dependency > < groupId >org.jboss.spec.javax.ejb</ groupId > < artifactId >jboss-ejb-api_3.1_spec</ artifactId > < scope >runtime</ scope > </ dependency > <!-- We depend on the EJB remote business interfaces of this application --> < dependency > < groupId >com.theopentutorials.ejb3</ groupId > < artifactId >ejbmavendemo</ artifactId > < type >ejb-client</ type > < version >${project.version}</ version > </ dependency > <!-- JBoss EJB client API jar. We use runtime scope because the EJB client API isn't directly used in this example. We just need it in our runtime classpath --> < dependency > < groupId >org.jboss</ groupId > < artifactId >jboss-ejb-client</ artifactId > < scope >runtime</ scope > </ dependency > <!-- client communications with the server use XNIO --> < dependency > < groupId >org.jboss.xnio</ groupId > < artifactId >xnio-api</ artifactId > < scope >runtime</ scope > </ dependency > < dependency > < groupId >org.jboss.xnio</ groupId > < artifactId >xnio-nio</ artifactId > < scope >runtime</ scope > </ dependency > <!-- The client needs JBoss remoting to access the server --> < dependency > < groupId >org.jboss.remoting3</ groupId > < artifactId >jboss-remoting</ artifactId > < scope >runtime</ scope > </ dependency > <!-- Remote EJB accesses can be secured --> < dependency > < groupId >org.jboss.sasl</ groupId > < artifactId >jboss-sasl</ artifactId > < scope >runtime</ scope > </ dependency > <!-- data serialization for invoking remote EJBs --> < dependency > < groupId >org.jboss.marshalling</ groupId > < artifactId >jboss-marshalling-river</ artifactId > < scope >runtime</ scope > </ dependency > </ dependencies > < build > < plugins > <!-- Enforce Java 1.6 --> < plugin > < artifactId >maven-compiler-plugin</ artifactId > < version >${version.compiler.plugin}</ version > < configuration > < source >${maven.compiler.source}</ source > < target >${maven.compiler.target}</ target > </ configuration > </ plugin > <!-- Add the maven exec plugin to allow us to run a java program via maven --> < plugin > < groupId >org.codehaus.mojo</ groupId > < artifactId >exec-maven-plugin</ artifactId > < version >${version.exec.plugin}</ version > < executions > < execution > < goals > < goal >exec</ goal > </ goals > </ execution > </ executions > < configuration > < executable >java</ executable > < workingDirectory >${project.build.directory}/exec-working-directory</ workingDirectory > < arguments > <!-- automatically creates the classpath using all project dependencies, also adding the project build directory --> < argument >-classpath</ argument > < classpath > </ classpath > < argument >com.theopentutorials.ejb3.Client</ argument > </ arguments > </ configuration > </ plugin > </ plugins > </ build > </ project >1. maven-compiler-plugin The maven-compiler-plugin is used to compile the sources of your project. We specified jdk 1.6 version for source and target classes.
2. exec-maven-plugin Since our ejb client is a Java program, to run it we use the exec-maven-plugin which helps to execute system and Java programs. We need to specify the executable (i.e. java), classpath, and the java class (com.theopentutorials.ejb3.Client). The classpath is left empty because the plugin includes the necessary classpath arguments based on the dependencies provided.
In order to run EJB3 client we need to include the following dependencies,
We depend on the EJB remote business interfaces of this application to run the client. So we need to specify the ejb client jar dependency. The <type> tag with value “ejb-client” is used to specify this project’s dependency on the EJB client jar. 1 2 3 4 5 6 < dependency > < groupId >com.theopentutorials.ejb3</ groupId > < artifactId >ejbmavendemo</ artifactId > < type >ejb-client</ type > < version >${project.version}</ version > </ dependency > The dependencies jboss-transaction-api_1.1_spec, jboss-ejb-api_3.1_spec, jboss-ejb-client, xnio-api, xnio-nio, jboss-remoting, jboss-sasl, jboss-marshalling-river havescope as runtime because these are all not required for compilation. The dependencies jboss-javaee-6.0 and jboss-as-ejb-client-bom underdependencyManagement have scope as import which is used to include dependency management information from a remote POM into the current project. These remote POMs are provided by JBoss which contains the necessary dependencies for running the client.remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED=false
remote.connections=default
remote.connection.default.host=localhost remote.connection.default.port = 4447 remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS=false
If there are no errors, you should see the following output.
