Note: See the blog post How I Created a Standard pom.xml file for Eclipse and NetBeans for the final version of the pom.xml
This is an update to the blog post of just a few days ago. Never to leave well enough alone I have continued examining how to use Maven effectively in Eclipse. As such I have discovered some unneeded steps and some new details.
Without the excess wording this time here are the instructions.
Download and install (unzip into the folder of your choice) the Eclipse IDE for Java EE Developers from http://eclipse.org . The version, as of this writing, is 4.3.1. Since the introduction of version 4.3, also known as Kepler, the m2eclipse plugin for Maven is part of the distribution.
You must choose between a 32 and 64 bit version of this download. Here is a recent article on this subject: http://www.javacodegeeks.com/2012/12/should-i-use-a-32-or-a-64-bit-jvm.html . I use the 64 bit JVM.
Download and install (unzip into the folder of your choice) TomEE from http://tomee.apache.org/downloads.html . There are three versions but it does not matter which one you choose. I use the web profile version.
Run Eclipse and create a new workspace. Eclipse stores its settings inside of the workspace. This is why you need to reconfigure Eclipse whenever you create a new workspace. Since we must create a specialized configuration we need to create a new workspace. We will then be able to create as many web profile projects as we want in this workspace.
Inform Eclipse of the existence of TomEE. You do this by selecting Window -> Preferences -> Server -> Runtime Environment.
Select Add and in the list box select Apache Tomcat v7.0 and click on Next.
Browse to the directory into which you install TomEE. I also change the name of the server to Apache TomEE 1.6.0. Click on Finish and then OK.
The server we just defined now must be added to the workspace. Look for the Servers view usually found on a tab in a panel below the editor window. There is a link that reads “No servers are available. Click this link to create a new server…” Click on this link.
In the dialog that appears select the Apache Tomcat v7.0 Server. The server name and runtime should refer to the TomEE server you defined in the previous step. Click on Finish.
Now you are ready to create a Maven Web Project
Use File -> New -> Maven Project. If you don’t see Maven Project then select Other and you will find the Maven choices. Eclipse will eventually place Maven Project on the New listing.
At the first dialog make sure you check “Create a simple project (skip archetype selection)” and click on Next.
Now you come to the Configure project dialog. This defines the basic information in the pom.xml file and the directory structure of the project.
Group Id: This is typically the root package name. I require that you use com.yourname such as com.kenfogel. I need you to do this so I know whose project I am looking at when I am grading. If you are reading this and not a student use whatever you feel is appropriate.
Artifact Id: This becomes the project name. When using Maven to package your code into the repository it will use the Group Id and Artifact Id as the location of your jar file. In this example I am using WebProfileExample so the full package style name will be com.kenfogel.WebProfileExample.
The package names you use for your classes does not have to be the same as the Groups Id and Artifact Id but most programmers choose to do so.
You can ignore Version.
Packaging cannot be ignored. The packaging you choose determines the directory structure of the project. If it says jar then pull down the combo box and change it to war.
Name and Description are field added to the pom.xml for informative purposes.
Click on Finish.
Here is what the Project Explorer should look like:
Step 7 – UPDATE:
The default Maven pom.xml file is minimal. It must have additional elements to allow you to successfully create code for the web profile of Java EE 6.
Currently it looks like:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.kenfogel</groupId> <artifactId>WebProfileExample</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>Web Profile Example</name> <description>Tutorial Eclipse and TomEE project</description> </project>
Minimally it should look like this assuming you want to use Java 1.7 and JUnit 4:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.kenfogel</groupId> <artifactId>WebProfileExample</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>Web Profile Example</name> <description>Tutorial Eclipse and TomEE project</description> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.openejb.maven</groupId> <artifactId>tomee-maven-plugin</artifactId> <version>1.6.0</version> <scope>provided</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.4</version> <configuration> <failOnMissingWebXml>false</failOnMissingWebXml> <webXml>src/main/webapp/WEB-INF/web.xml</webXml> </configuration> </plugin> </plugins> </build> </project>
After updating the pom.xml file you must update the project. Save the updated pom.xml file. Right mouse click on the project and select Maven -> Update Project. Your project should appear with a check next to it in the dialog so all you need to do is click OK.
I am also working with NetBeans for the same purpose. Since NetBeans interacts with the server differently from Eclipse its pom.xml file requires additional lines that allows NetBeans to deploy the application. I will test to determine if the same Maven pom.xml can be used in both IDEs.
The old step 8 is no required. So there is now one less step.
There are more steps necessary for coding Java Server Faces and Servlets but this is the starting point. A quick test is to create a simple HTML file in the src/main/webapp folder.
Right mouse click on the file and select Run As… -> Run on Server.
Click on Finish and you should see:
Now you are ready for the good stuff!
WebContent becomes src/main/webapp
When using Eclipse without Maven you use the Dynamic Web Project to create a web profile application. Such a project has a WebContent folder that contains WEB-INF, META-INF and other items found in this part of a site. A Maven war project requires that such parts of the application are placed in src/main/webapp. This folder will be created but it will be empty. You will need to create the WEB-INF and META-INF folders in this directory. If you right mouse click on Deployment Descriptor in the project and choose Generate Deployment Descriptor Stub it will create the WEB-INF and a web.xml file for you.
This is still an area that I am fuzzy on. In Eclipse the Run As command does not invoke a Maven build. This means that it is necessary to first do a Maven Build and then a Run As. When doing this you are asked for the Goals. I came across the following that seems to do the trick:
Now I perform a Maven Build and then Run As and the project executes.