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.
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.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.
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.
The EJB module pom.
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.
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.
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.
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.
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.
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.
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.
The tag in the MavenEnterpriseApplication-ejb dependencywill make sure that the Class-Path line in the MANIFEST.MF file willcontain the MavenEnterpriseApplication-ejb.jar file which is needed forthe Enterprise Containier of GlassFish to inject the DataLocal instancein the servlet that we are about to create. Before we can do that,however, we first need to resolve some conflicts that we haveintroduced. Maven is not able to resolve theMavenEnterpriseApplication-ejb dependency since we haven’t built anycode yet. Everything will work fine once we build the Parent POMProject, but for now Netbeans cannot resolve the classes in theMavenEnterpriseApplication-ejb project when we use them in the WARProject. So, right click the MavenEnterpriseApplication project andselect build. This may take a while since Maven will probably need todownload quite a buch of jar files.
Besides modifying the pom.
xsi:schemaLocation="http://java.sun.com/
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
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 is the path to the ear file which can be found in the targetdirectory of your ear project. In my case I issued this command
./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.
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. resources -> META-INF and mine now looks like this
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.