Welcome!

Java Authors: Pat Romanski, Kevin Benedict, Liz McMillan, Jerry Melnick, Elizabeth White

Blog Feed Post

Setting up a JEE 6 Web Profile Maven Project in Eclipse 4.3 using TomEE 1.6.0 as the Server – Part 1 The Basics

This is the first part of a series of basic tutorials I am writing for my students. I believe that programmers should program and not be forced to become an expert on their tool. I want my students to have an accurate set of instructions that delivers repeatable results. In this case I need them to quickly set up a web application managed by Maven.

It took a bit of time but I finally figured out how to use Maven in Eclipse to write server side Java. The problem is that most articles I read on the topic made assumptions about what the reader knew. Most expected you to copy the resulting WAR file into a running instance of TomEE. I wanted to be able to use Eclipse in a manner similar to creating a Dynamic Web Project.

If you see any mistakes or nonsense then please let me know. If this solves any problems let me know too. My thanks to all the other bloggers and Stack Overflow responders.

Step 1:

Download and install 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.

Step 2:

Download and install 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.

Side Story

In my early days of DOS there was no such thing as installing. You simply copied the program from a floppy disk to the computer. Some programs came with an installer but all that did was copy the files for you. When Windows came along the boys at Microsoft came down with Unix envy. Unix, like Linux today, is full of mysterious volumes meant to hold specific parts of complex applications, drivers and other spooky things. So Microsoft created mysterious directories inside the Windows folders and required that certain components of an application needed to be stored there. This is, I believe, the origin of what Windows old-timers know as DLL hell.

It wasn’t until Windows 7 and Visual Studio 2010 that Microsoft changed their tune. They now advise developers to place all the files related to an application in the same folder and to keep their stuff out of the Windows folder.

For the most part open source developers have developed software that installs just like it did in the DOS days. The only difference is that the software now comes in a Zip or gz file and you install by unzipping it into the folder of your choice. Both TomEE and Eclipse are delivered in a compressed file and you just un-compress it into the folder of your choice.

Step 3:

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.

Step 4:

Inform Eclipse of the existence of TomEE. You do this by selecting Window -> Preferences -> Server -> Runtime Environment.

image1

Select Add and in the list box select Apache Tomcat v7.0 and click on Next.

image2

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.

image3

Step 5:

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.

Step 6:

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.

image4

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.

image5

Here is what the Project Explorer should look like:

image6

Step 7:

One of the reasons I like IDEs instead of working with an editor and the command line is the automation provided by the IDE. Another is that I don’t have to remember (cause I’m too old or too lazy to remember) commands that read like spells in a Harry Potter novel. The problem is that while Eclipse has elevated Maven to its inner circle of tools it has not yet made Maven projects as easy to use as its native Dynamic Web Project. It can be but you have to do some work.

The first thing to do is to fix the pom.xml file. 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 assuming you want to use Java 1.7:

<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>javax</groupId>
         <artifactId>javaee-api</artifactId>
         <version>6.0</version>
         <!-- scope provided means the container delivers it -->
         <scope>provided</scope>
      </dependency>
   </dependencies>
   <build>
      <plugins>
         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.5.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>
            </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.

Step 8:

Almost done. This last step will allow you to use the Run or Run As commands to execute your project in TomEE without needing to move the war file to TomEE’s webapps folder. To accomplish this we have to set the Project Facets.

Right mouse click on the project and select Properties. In the list of properties on the left select Project Facets. Verify that the items checked off have version numbers that match the illustration. Click on Apply and OK.

image7

Doing this will add a new folder to your project. It is src/main/webapp. This is where certain files must be placed such as Facelet and static HTML files.

Step 9:

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.

image8

 

Right mouse click on the file and select Run As… -> Run on Server.

image9

 

Click on Finish and you should see:

image10

 

Now you are ready for the good stuff!

Read the original blog entry...

More Stories By Ken Fogel

In 1980 I bought for myself the most wonderful toy of the day, the Apple ][+. Obsession followed quickly and by 1983 I was writing software for small and medium sized businesses in Montreal for both the Apple and the IBM PC under the company name Omnibus Systems. In the evenings I taught continuing education courses that demystified the computer to the first generation of workers who found themselves with their typewriter on the scrap heap and a PC with WordStar taking its place. In 1990 I was invited to join the faculty at Dawson College in the Computer Science Technology program. When I joined the program the primary language was COBOL and my responsibility was to teach small systems languages such as BASIC and C/C++. Today I am now the chairperson and program coordinator of the Computer Science Technology program at Dawson. The program's primary language is Java and the focus is on enterprise programming. I like to write about the every day problems my students and I face in using various languages and platforms to get the job done. And from time to time I stray from the path and write about what I plan to do, what I actually get around to doing, and what I imagine I am doing.