Eclipse JSF JPA CDI Maven project

Last article update: Dec 2012

Get Eclipse

Download Eclipse Indigo for Java EE Developers. Do not download Eclipse Juno at this point (Oct 2012) since JBoss Tools is not yet compatible with it. Juno seems to be even slower than Indigo anyway. Unzip Eclipse to a directory of your choice, start it and choose a location for your workspace folder.

Configure Eclipse (can be skipped)

Personally, I don’t like some of Eclipse’s default settings and will change them right now. But this is completely up to you, feel free to skip this section.

Set Java save actions to format your code every time you save the file. This might feel a bit rude at the beginning, but once you get used to it, you will probably enjoy coding a lot more since you dont have to worry about basic code style any more.

I adjusted the default Eclipse code formatter, for example to cleaner format switch statements, and saved these settings. Therefore I import my Java formatter at this point. These settings took very little time, but I feel way more comfortable now. So let’s start.

Install Maven

Install the m2eclipse Maven plugin from the Indigo release website as shown in the screenshot on the left. You may want to configure Maven to download JavaDocs along with the libraries.

Install Subclipse and JavaHL

Install Subclipse, the Subversion Client Adapter and the Subversion JavaHL Native Library Adapter from the following URL:

http://subclipse.tigris.org/update_1.8.x. Have a look at the Screenshot on the left.

Setup the Server

Create the Project and add Facets

Create a simple Maven project with no archetype and select .war packaging

Right click your project, click Properties and select Project Facets. Enable project facets, select Java and set the version to 1.6 or 1.7. Check Dynamic Web Module version 3.0 and click on further configuration available.

Enter your context root, for example AppName. The context root will appear in your URLs later: http://localhost:8080/AppName/home.html. Change the content directory from WebContent to webapp for convenience. Check Generate web.xml deployment descriptor. Click OK and Apply. Click on Runtimes and select your previously created Tomcat runtime. Click Apply.

Check Java Server Faces and click on further configuration available. Select the User Library type and click on the download icon to download the MyFaces JSF library. The version that Eclipse offers is a bit outdated, but the library that you choose here is only used locally to add JSF capabilities to the project. We will later add a recent version of MyFaces in Maven’s pom.xml. Your application will then be shipped with this one. Change the /faces/* URL mapping pattern to .html, which will look a bit cleaner. Compare your settings to those in the screenshot on the left, then click OK and Apply.

Check CDI (Contexts and Dependency Injection and click on further configuration available. Check Generate beans.xml file. CDI requires this XML file, though it will be empty. Click OK and Apply.

Add Maven dependencies to your pom.xml

...
<!-- JSF 2.1 MyFaces -->
<dependency>
  <groupId>org.apache.myfaces.core</groupId>
  <artifactId>myfaces-api</artifactId>
  <version>2.1.10</version>
</dependency>
<dependency>
  <groupId>org.apache.myfaces.core</groupId>
  <artifactId>myfaces-impl</artifactId>
  <version>2.1.10</version>
</dependency>
<!-- Servlet 3.0 API provided by Tomcat -->
<dependency>
  <groupId>org.apache.tomcat</groupId>
  <artifactId>tomcat-servlet-api</artifactId>
  <version>7.0.37</version>
  <scope>provided</scope>
</dependency>
<!-- Hibernate (the entitymanager will pull all required JPA dependencies for us) -->
<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-entitymanager</artifactId>
  <version>4.1.7.Final</version>
</dependency>
<!-- CDI Weld -->
<dependency>
  <groupId>org.jboss.weld.servlet</groupId>
  <artifactId>weld-servlet</artifactId>
  <version>1.1.10.Final</version>
</dependency>
...

Project facets