tisdag 15 maj 2012

Java EE6 - Getting started with Java EE6 using Java 7, Eclipse, Maven, and GlassFish

When I was about to learn Java EE6 I found a lot of good information around about how Java EE6 works. Java EE6 is relay easy to get started with and understand, but there is a big BUT. It is not as easy to setup your development environment as it should. I have been spending some frustrating hours to be able to setup my Eclipse to work as I like. Problem after problem and hours of investigations to even be able to write a simple CDI bean and have it deployed on my local GlassFish instance. In this post we will have a look on my setup and how you could setup your Eclipse to get a fast turnaround and get started with your Java EE6 development.

To start i assume that you have Eclipse Indigo with EE support installed and a Java 7 JDK. I am using the latest Eclipse Indigo (EE package) release and Java 7 update 4. If you are using Java 7 i recommend you to run Eclipse with a Java 6 runtime environment insted. See Loop81 - Get Eclipse running with Java 7.

First off you will have to install the following pluings to get your Eclipse up and running with the tools we need:

When you have installed the plugins and restarted Eclipse we might need to create our GlassFish server. For some reason the server is created automatically and sometime it is not. If you do not have any server in the server view select "New" -> "Server". See the following image and press "Finish".

Select "GlasFish 3.1.2" and "Finish"

Your GlassFish server is now ready to use. You can try it out by starting it and access the admin console. You start the server from the "Servers" view. Right click on your server and select "Start". When the server is started you will be able to right click on the server and select "GlassFish" -> "View Admin Console".

Note! I have been spending hours to get my GlassFish instance to start with different problems like password not correct, no connection and so on. Most of all my problem has pointed my to the same directation that there are some problem with GlassFish and Java 7. Using the latest version of all plugins and binaries solves all the problem for me.

Before we continue I like to point out some places where you can find your GlassFish resources.
  • The GlassFish instance is installed in your Eclipse installation directory under: eclipse\plugins\oracle.eclipse.runtime.glassfish.build312_1.0.0\glassfish3\
  • The content of the server it self can be found in you workspace as a folder with the name glassfish312eclipsedefaultdomain or something like that. In this folder you will find the content copied from Eclipse.

When you have a running GlassFish instance it is time to create a new Java EE6 project. For this we will be using Maven. If you are not familiar with Maven, have a look on "Maven in 5 minutes" tutorial, http://maven.apache.org/guides/getting-started/maven-in-five-minutes.html.

Start by selecting "New Project" and select "Maven Project". Select the "maven-archetype-webapp" and give your project a nice group id and artifact id (I selected com.loop81 and files). Then click "Finish" and your project will be created.

The good thing with Maven is that it creates all the boring stuff for and the created web project is actually complete so we can try to deploy it on our GlassFish server. In the "Server"-view right click on your server and select "Add and Remove". In the dialog select your project and add it to the server. When you click "Finish" the application is automatically deployed. Right click on the project and select "Run on server" and you will see a nice "Hello World" text in the browser window.

Hello on you too!
With that up and running it is time to create a simple Java EE6 application. We will go for one simple applications which only shows today's date using CDI and JSF.

First of all we need to tell Maven to use Java 7. Open your pom.xml and add the following:
<build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
This will tell Maven that we like to use Java 1.7 for the source and the compiled results. After the change you need to update you project since you will get a error telling you that the project is not up to date. Right click your project and select "Maven" -> "Update project configuration". You should now have a project without problems. If you have any problems, make sure that you have setup you environment correctly. Under "Installed JREs" (Window -> Preferences -> Java) it should look like:

Installed JREs
Also make sure that you are using the JDK and not the JRE. Without the JDK you will not be able to run Maven as we like, since it requires the tools.jar which is only bundled with the JDK.

Next we like to add the Java EE6 dependency to the project so we can start writing our application. Add the following dependency to you pom.xml
<dependency>
  <groupId>org.glassfish.extras</groupId>
  <artifactId>glassfish-embedded-all</artifactId>
  <version>3.1.1</version>
  <scope>provided</scope>
</dependency>
This will add all the API which you will need. There are some problem with the Oracle provided Java EE6 API since they do not contain all code needed and since we are running on a Glassfish the best is to use the API provided from the container. Using provided as scope will tell Maven that the container running this application will provide the API for us, we only need them to be able to code or classes.

We do now have all the dependencies for our lite application and it is time to configure our JSF page which is supposed to show the current date. Open your web.xml which you will find under src/main/webapp/WEB-INF and add the flowing servet:
<servlet>
  <servlet-name>jsf</servlet-name>
  <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
  <servlet-name>jsf</servlet-name>
  <url-pattern>*.jsf</url-pattern>
</servlet-mapping>
This will configure the JSF servlet which by convension will be looking for .xhtml files in the root of you web application. So add a file named index.xhtml under src/main/webapp and add the folowing content to that file:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"   
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets">
    
  <h:head />
     
  <h:body>
   The current date is:
  </h:body>
</html>
If you now open you web-browser and type http://localhost:8080/files/index.jsf you should see a page with the text "The current date is:"

Next we will add a simple CDI bean which will provide us with the current date. The power of CDI instead of EJB is that CDI beans are added to the expression language (EL) scope so we can access them directly from the JSF pages. Create a new src folder and name it "src/main/java". In this folder create a new package where you like to create your bean (I created a package named com.loop81.files.jsf). In this package create a new file named "DateProvider". Implement the class as follows:
package com.loop81.files.jsf;
import java.util.Date;

import javax.enterprise.context.RequestScoped;
import javax.inject.Named;

@Named
@RequestScoped
public class DateProvider {

 public Date getCurrentDate() {
  return new Date();
 }
}
This is a simple bean which is defined as a CDI bean using the @Named annotation with the scope @RequestScope. Note! One common mistake one makes when learning Java EE6 is to mix the annotations. Make sure that you use the annotations from the javax.enterprise package when you are using the CDI annotations (the annotations from the javax.inject package). So what does this bean do? By using EL we can access this bean directly from the JSF page using #{dateProvider.currentDate}, but before we do that we need to add one important thing to our project. In the WEB-INF folder add a empty file which you name beans.xml. This will tell the container to enable CDI and scan for CDI beans. Without it CDI will not work. Lastly add the #{dateProvider.currentDate}  string after the "The current date is:" in you index.xhtml file.

When you reload your index.jsf page the date should show up and we are home.

The complete page. Not much for the world...

In this post we have created a development environment which lets us work on our Java EE6 project and instantly see the result in our web browser. By using GlassFish together with Maven, Eclipse and Java 7 we have all the tools we need to continue creating more advanced projects. I have been spending hours getting all required plugins sorted and version. Hopefully they way forward be a bit easier for you so you can spend your time coding instead of boring investigation.

Inga kommentarer:

Skicka en kommentar