A few weeks ago an article appeared in the Netbeans Knowledge Baseabout how to get started with GlassFish V2, Maven2 and Netbeans 6.0. Itis a great article doing exactly what the title says: getting youstarted with Maven2 in Netbeans 6.0 a
A few weeks ago an article appeared in the Netbeans Knowledge Baseabout how to get started with GlassFish V2, Maven2 and Netbeans 6.0. Itis a great article doing exactly what the title says: getting youstarted with Maven2 in Netbeans 6.0 and deploying the project thuscreated to GlassFish V2. Despite the fact that I couldn’t get a fewsteps to work correctly, I was able to connect to the Remote SessionBean from my client app and see the log message appear in the GlassFishlog.
After having completed the instructions in the article Ifelt I wanted to do a bit more. Over the past year or so I have beendeveloping a few small Enterprise Applications. These are applicationscontaining an EJB jar file and a war file packed in and deployedthrough an ear file. The EJB jar contains Entities and Session Beansand the classes in the war file make use of the (Local) Session Beansto allow users of the web application to manage data. This articledescribes the steps you need to take to create such an application withNetbeans 6.0 (Beta 2) and Maven2.
Installing the software
To follow the steps in this article you’ll need Netbeans 6.0 and a database. Netbeans 6.0 Beta 2 can be downloaded from this link.The "Web & Java EE" download is sufficient for the purposes of thisarticle since it contains the GlassFish V2 application server. If youchoose to download another variant, please make sure to installGlassFish V2 and register your GlassFish installation with Netbeans.GlassFish can be downloaded here.
Besidesthat, you’ll need a database with a JDBC 3 compliant driver. I usedOracle XE but any other database should do. Make sure to create a JDBCConnection Pool and a JDBC Resource to your database so you can use itin the Enterprise Application. For instructions on how to create aConnection Pool and a JDBC Resource for an Oracle database, pleaserefer to this article of mine. The instructions in that article are for GlassFish V1 but the steps in V2 are similar.
Afterhaving installed Netbeans, make sure the MevenIDE plugin is downloadedand installed into Netbeans. To do so, start Netbeans and go to Tools-> Plugins. The second tab of the frame that pops up should listMaven under the Java Category. Select it and click the Install buttonto the lower left. That’s it, you’re all set up now.
Defining the project layout
Ourapplication needs to contain three projects. One project will hold theEJB content, one project the war content and one project will createthe ear file. Since these three projects will share some configurationsettings for Maven it makes sence to put all of them in a parent pomproject. this means that our application will contain this structure
Parent Pom Project
|
|— EJB Project
|
|— WAR Project
/
— EAR Project
Let’s have a look at how to create all of these projects.
The Parent POM project
Tocreate the parent pom project, select File -. New Project and selectMaven in the list to the left of the frame that pops up. Select MavenProject to the right and click the Next button.
In the next frame, select the "Maven Quickstart Archetype" archetype and click the Next button.
Inthe third frame specify the Project Name, Project Location, Group Id,Version and Package. For now, the version and package don’t mattermuch. But it’s nice to have some meaningful name like"MavenEnterpriseApplication" and a meaningful Group Id like"nl.amis.maven.enterprise".
Nowwe’ll have to edit the pom.xml file by hand to make it suit our needs.Some of the properties can be edited by right clicking the project nameand selecting properties. These properties can also directly be editedby opening the pom.xml file and modifying the XML. Using the built ineditor saves us a lot of typing, so right click theMavenEnterpriseApplication project name and select Properties. Sincethis is our parent project that we’ll only use for general Mavenconfiguration purposes, we need to change the packaging from jar to pom.
Next,select the Source category and set the Source/Binary format to 1.5.Click OK to exit the editor and save the changes you made. Most of theother changes we need can be made without actually editing the pom.xmlfile. First of all, by default Maven creates one source class and onetest class in every project that is created. We won’t need those inthis project. Go to the Files window right of the Projects window (ifit’s not visible select the Windows menu item and then Files) andexpand the project. Select the src directory and hit the Delete button.Next return to the Projects window.
By default Maven also generatesa JUnit dependency and we won’t need that either. so expand the TestLibraries node, right click the junit-3.8.1.jar library and selectRemove Dependency. That’s it for now.
The EJB Project
Nextwe’ll create the EJB Project. To do so, follow the same three steps aswhen creating the new Parent POM Project. This time make sure theProject Location is the root directory of the Parent POM Project. Thiswill make Maven recognise it as a sub-module managing the necessarychanges to the pom.xml files of both the parent and the sub-module. Ichose these settings for the EJB module:
Project Name: MavenEnterpriseApplication-ejb
Project Location: /home/wouter/NetBeansProjects/MavenEnterpriseApplication
Group Id: nl.amis.maven.enterprise
Package: nl.amis.maven.enterprise
Thepackage I specified is the same as the package I specified for theParent POM Project. Note that the Parent POM Project pom.xml file wasmodified to now include a reference to the EJB module:
MavenEnterpriseApplication-ejb
The EJB module pom.xml file was created with a reference to the Parent POM Project:
MavenEnterpriseApplication
nl.amis.maven.enterprise
1.0-SNAPSHOT
Rightclick the MavenEnterpriseApplication-ejb project and select Properties.This time change the packaging to "ejb". After clicking OK to save thechanges and reopening the Properties editor it is also possible to openthe Run Category and select the GlassFish V2 application server. Pleasedo NOT do this! It will generate errors in the Netbeans project. If youdo so and encounter problems, please remove the profile.xml file fromthe MavenEnterpriseApplication-ejb project. Next, remove these linesfrom the pom.xml file:
J2EE
Finally,you need to tell Maven to make use of the version 3.0 EJB Maven plugin.This can be done by adding these lines to the pom.xml file by hand
org.apache.maven.plugins
maven-ejb-plugin
3.0
Let’screate our Entities and Session Beans now. Since we didn’t connect ourEJB project to the GlassFish application server, and doing so causeserrors, we will need to use a Netbeans local database connection togenerate the beans. Later on, before deploying our project, we willneed to make adjustments to the Persistence Unit persistence.xml fileso our deployed application makes use of the datasource we defined inGlassfish. But first things first.
Creating the beans
Ifyou haven’t created a database connection in Netbeans yet, this wouldbe a good time to do it. If you don’t Netbeans will prompt you for itlater. However, for most databases you need to register your JDBCdriver first and you won’t be able to do it when Netbeans prompts youfor a database connection. After having created the databaseconnection, right click the MavenEnterpriseApplication-ejb project andselect New -> Entity Classes From Database. In the wizard that popsup, select your database connection and then select the tables you wantto create Entities for. Since I am using Oracle XE I have created adatabase connection to the HR schema. Therefore I selected all tablesin the HR schema.
Afterclicking next you get the chance to rename the Entity classes to yourliking and specify the package in which they will be created. Moreimportantly, here is where you can create a Persistence Unit. Click the"Create Persistence Unit" button and give the Unit a more sensiblename, e.g. "MavenEnterpriseApplication-ejbPU". Finish creating thePersistence Unit and Entities.
Besides the new Entity classes andPersistence Unit, some changes have been made to the pom.xml file aswell. Since the Entity classes make use of the Toplink Essentialsclasses, the dependencies on the toplink jars have been resolved forus. This has been done in two ways. First of all, a reference to the java.net Maven Repositoryhas been added. Second, the depenency entries for toplink-essentialsand toplink-essentials-agent have been added to the pom.xml file. Sinceour web project will need access to the java.net repository as well, itis wise to move the repository reference from the EJB pom.xml to theParent POM pom.xml file. Besides that, we need to tell Maven not toinclude the jar files for Toplink Essentials in the EJB jar file. Youneed to do this by adding this line to each dependency in the pom.xmlfile
provided
Sincethe app I am creating for this article only is a simple one, I am onlygoing to modify one Entity class to contain an additional NamedQuery.In the Employees Entity I am adding this NamedQuery which returns alist of all available Employees
@NamedQuery(name = "Employees.getAll", query = "SELECT e FROM Employees e")
Nextcreate a Local Session Bean by right clicking theMavenEnterpriseApplication-ejb project and selecting New -> SessionBean. Provide a name for your session bean, e.g. Data, and a package,e.g. nl.amis.maven.enterprise.ejb.session. Leave the defaults(Stateless and Local) be. Clicking the Finish button will generate anerror in Netbeans. The new session bean will have the @Local and@Stateless annotations and Maven is unable to resolve them. In my casethe session bean wizard even doesn’t close so I closed it by clickingthe Cancel button. Fortunately this doesn’t remove the session bean!
Theproblem is that the two annotations are provided by the ejb-api.jarfile which Maven doesn’t know of. So we need to add a dependency tothat jar file by adding these lines to the EJB pom.xml file
javax.ejb
ejb-api
3.0
provided
Next add this mehtod definition to the DataLocal interface:
public int countEmployees();
and this EntityManager injection and method implementation to the DataBean class
@PersistenceContext EntityManager em;
public int countEmployees() {
return em.createNamedQuery("Employees.getAll").getResultList().size();
}
That completes the EJB project!
The WAR Project
Forthis article, we will a simple Servlet which uses the DataLocal beanthat displays the number of Employees in your browser. First, create anew Project. This time, don’t create a Maven Quickstart Archetypeproject, but a Maven Webapp Archetype. I used these settings for it
Project Name: MavenEnterpriseApplication-war
Project Location: /home/wouter/NetBeansProjects/MavenEnterpriseApplication
Group Id: nl.amis.maven.enterprise
Package: nl.amis.maven.enterprise
Inspectthe project properties and make sure this time the packaging is set towar. In order to get a proper WAR file a few modifications need to bemade to the pom.xml file. First of all, these lines need to be addedinside the
maven-war-plugin
true
Thiswill make sure a Class-Path line will be added to the MANIFEST.MF file.Next we need to add dependencies for our EJB project, the ejb-api.jarand the servlet-api.jar by adding these lines to the pom.xml file inthe same div as the junit dependency
nl.amis.maven.enterprise
MavenEnterpriseApplication-ejb
1.0-SNAPSHOT
true
javax.servlet
servlet-api
2.5
provided
javax.ejb
ejb-api
3.0
provided
The
Besides modifying the pom.xmlfile, the web.xml file that was created also needs to be modified.Maven creates a web.xml file that GlassFish won’t like. The first threelines of the web.xml file (that are the lines up to and including theline with the
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
Oncethe build process has finished create a new servlet by right clikcingthe MavenEnterpriseApplication-war project and selecting New ->Servlet. Give it a name, e.g. DataServlet, and specify a package, e.g.nl.amis.maven.enterprise.web.servlets. Keep all the other default. Now,inject your DataLocal bean and call the countEmployees method like this
@EJB DataLocal dataLocal;
/**
* Processes requests for both HTTPGET
andPOST
methods.
* @param request servlet request
* @param response servlet response
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
out.println("Number of Employees in table is " + dataLocal.countEmployees());
} finally {
out.close();
}
}
The EAR Project
thefinal project we need is the one that will create the ear file usingthe MavenEnterpriseApplication-ejb and MavenEnterpriseApplication-warprojects. First create a new Maven Quickstart Archetype project. I usedthese settings:
Project Name: MavenEnterpriseApplication-ear
Project Location: /home/wouter/NetBeansProjects/MavenEnterpriseApplication
Group Id: nl.amis.maven.enterprise
Package: nl.amis.maven.enterprise
Modify the project properties and set the Packaging to ear. Remove srcdirectory by going to the Files window and deleting the src directory(duh). Remove the junit Test Library from the Projects window. Finally,add the dependencies on the MavenEnterpriseApplication-ejb andMavenEnterpriseApplication-war projects like this
nl.amis.maven.enterprise
MavenEnterpriseApplication-ejb
1.0-SNAPSHOT
ejb
nl.amis.maven.enterprise
MavenEnterpriseApplication-war
1.0-SNAPSHOT
war
Building the Parent POM Project should now be succesfull:
------------------------------------------------------------------------
Reactor Summary:
————————————————————————
MavenEnterpriseApplication ………………………. SUCCESS [2.374s]
MavenEnterpriseApplication-ejb …………………… SUCCESS [1.263s]
MavenEnterpriseApplication-war Maven Webapp ……….. SUCCESS [1.155s]
MavenEnterpriseApplication-ear …………………… SUCCESS [1.664s]
————————————————————————
————————————————————————
BUILD SUCCESSFUL
————————————————————————
Solving deployment issues
Nowthat the project can succesfully be built, it is time to deploy the earfile to GlassFish V2. For now use the asadmin utility that is shippedwith Glassfish V2. I am sure there is a way to deploy to GlassFish viaMaven but I haven’t found out how to do this yet. The asadmin utilitycan be found in the bin directory under the directory in whichGlassFish was installed by you. On my Linux system that isglassfish-v2-b58g/bin but you may need to look in C:/glassfish-v2-b58gor perhaps C:/Program Files/glassfish-v2-b58g if you are on Windows. Bythe way, on Windows the asadmin utility is called asadmin.bat.
To deploy the ear file, open a console and CD to the bin directory holding the asadmin utility. then issue this command
./asadmin deploy --user=admin
or if you’re on Windows
asadmin.bat deploy --user=admin
wherethe ".bat" part in the command is optinal. Here
./asadmin deploy --user=admin ./MavenEnterpriseApplication-ear/target/MavenEnterpriseApplication-ear-1.0-SNAPSHOT.ear
the server responds with this error
CLI171 Command deploy failed : Deploying application in domain failed; The persistence-context-
ref-name [nl.amis.maven.enterprise.ejb.session.DataBean/em] in module [] resolves to a
persistence unit called [MavenEnterpriseApplication-ejbPU] which is of type RESOURCE_LOCAL.
Only persistence units with transaction type JTA can be used as a container managed entity manager.
Please verify your application.
Rememberwhat I said about the Persistence Unit and that creating it will makeit use a Netbeans local database connection? Well, GlassFish doesn’tbuy that. You need to use a JDBC datasource. So it’s time to modify thePersistence Unit persistence.xml file, rebuild our project and try anddeploy it again.
All the way at the beginning of this article Iasked you to create a JDBC datasource in Glassfish. The one I createdis called jdbc/hr and I will mdoify my persistence.xml file to usethat. The file can be found in the EJB project under Other Sources-> resources -> META-INF and mine now looks like this
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">
jdbc/hr
Next rebuild the Parent POM Project and issue the asadmin command again. This time GlassFish should answer with
Command deploy executed successfully.
Browsing to the correct URL, in my case http://localhost:8280/MavenEnterpriseApplication-war/DataServlet
should reveal how many Employees are present in our EMPLOYEES table:
Number of Employees in table is 107
Pleasenote that using the Entity wizard again will modify thepersisntence.xml file so you need to change this file again afterwards.