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 editor font to Courier New 10 pt
- Set all default encodings to UTF-8 (General – Workspace, Web – CSS Files, Web – HTML Files, Web – JSP Files, XML – XML Files)
- Enable Refresh using native hooks or polling and Refresh on access
- Show line numbers
- Go to XML – XML Files – Validation and set No grammar specified from Warning to Ignore.
- Go to Web – JavaServer Faces Tools – Validation, open Type Assignment Problems and set Method expression signature incompatibility from Warning to Ignore. I do this because most of my controller’s action methods have return type void and Eclipse wants to see String here instead. However, the spec does not demand bean methods to return String. It’s just not an error and not worth a warning.
Install Maven
Install Subclipse and JavaHL
http://subclipse.tigris.org/update_1.8.x. Have a look at the Screenshot on the left.
Setup the Server
- Download Tomcat 7.0.xx and unzip it somewhere
- Download mysql-connector-java-5.x.xx-bin.jar and put it into tomcat/lib
- Add a new Tomcat server to Eclipse
Create the Project and add Facets
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
- Go to project facets and enable the JPA 2.0 facet. If you added a JPA library for a previous project, you have to prevent eclipse from including those library with your new application, since Maven will do this for you. Go to project properties –> JPA –> untick the option Include libraries with this application. However, it seems to me that at least a JPA library has to exist to enable the JPA facet, even if we don’t need its jars.
- Go to project properties –> JPA –> tick the option Discover annotated classes automatically. If you don’t, you will get the following error for every @Entity annotated class in your application: Class “my.package.User” is mapped, but is not included in any persistence unit.
- Go to project facets –> Runtimes –> select Tomcat 7.x as runtime for this project.