Eclipse, Maven and Tomcat - A Basic Set-Up

06 Aug 2022

Table of Contents


This was surprisingly fiddly. One small deviation off the happy path leads to failure - even if the deviation seems wholly reasonable at the time. There may be an easier way, but I wanted to build the most basic web app possible, avoiding Eclipse’s Dynamic Web Project set-up. I wanted a Maven-based project - and not one using a big, complex Maven archetype.

If you do want something more full-featured out of the gate, try Building Jakarta EE 9 Web application with Servlet Containers or something similar.

My approach is basically the same end result as the NetBeans-based Jakarta EE 9 on Tomcat 10 - A Basic Set-Up - and refers to that guide in a couple of places.

Using:

  • Java 17
  • Tomcat 10.0
  • Eclipse Enterprise
  • Maven
  • Jakarta EE9 (Servlet 5.0)

Eclipse

Download and install “Eclipse IDE for Enterprise Java and Web Developers”

We will use the Java EE perspective.

Java

Installed at C:\tomcat\eclipse\apache-tomcat-10.0.22

JAVA_HOME = C:\Program Files\Eclipse Adoptium\jdk-17.0.2.8-hotspot

In Eclipse:

Window > Preferences > Java > Installed JREs > Add… > Standard VM

JRE home: C:\Program Files\Eclipse Adoptium\jdk-17.0.2.8-hotspot

Save; select checkbox to make it the default; Apply and close.

Window > Preferences > Java > Compiler > compliance level: 17.

Tomcat

Tomcat 10 64-bit Windows zip: https://tomcat.apache.org/download-10.cgi

Downloaded and unzipped to here: C:\tomcat\eclipse\apache-tomcat-10.0.22

In Eclipse:

Window > Preferences > Server > Runtime Environments > Add…

Apache > Apache Tomcat v10.0

Check “Create a new local server”
(a server is created and added as an entry in the Servers view).

Name: Tomcat v10.0 for Eclipse
Tomcat installation dir: C:\tomcat\eclipse\apache-tomcat-10.0.22
JRE: jdk-17.0.2.8-hotspot

Finish. Apply and close.

The server should be listed in the “Servers” tab at the bottom of the screen. Otherwise, click on the “No servers are available” link.

In the “Publishing” section of the server overview page, make sure “Automatically publish after a build event” is selected.

In the “Server Locations” section leave the selection as “Use workspace metadata”. This is where your webapp artifacts are deployed - the Eclipse-controlled equivalent of Tomcat’s webapps directory.

Do not start Tomcat yet.

The Servlet

In Eclipse:

File > New > Maven project > Next

Filter: maven-archetype-webapp

The above artifact ID is the one for group ID org.apache.maven.archetypes. It provides a simple file structure.

1
2
3
4
5
6
7
8
project
|-- pom.xml
`-- src
   `-- main
       `-- webapp
           |-- WEB-INF
           |   `-- web.xml
           `-- index.jsp

We just use this as a convenience. We could create the directories manually. In fact we may still need to add a META-INF directory at some point if it becomes necessary.

Select the artifact and then click Next.

Group Id: org.northcoder Artifact Id: tomcatbasicdemo Package: org.northcoder.tomcatbasicdemo

Click Finish.

Delete the index.jsp file in WEB-INF - we are not using JSPs.

Delete the web.xml in WEB-INF - we will be using annotations not an XML deployment descriptor.

Replace the entire contents of the pom.xml with the same contents as shown in this article.

To access the “Project context” menu, right-click on project in the Project Explorer.

Project context > Maven > Update project… OK

This updates the project configuration from the pom.xml file.

Add a servlet - the same one as shown in this article.

To add the servlet, first create a package in Java Resources > src/main/java:

Right-click on src/main/java > New > package: org.northcoder.tomcatbasicdemo > Finish

In that package: New > Class > HelloWorld > Finish

Add in the HelloWorld code from the other article. Eclipse may warn you about a missing serialVersionUID. You can add that:

1
private static final long serialVersionUID = 1L;

File > Save (Ctrl+S) or File > Save All (Ctrl+Shift+S)

Project > Build All (Ctrl+B)

In the Servers tab at bottom of page, right-click on the server name and choose Add and Remove…

Make sure tomcatbasicdemo(basic_demo) is in the “Configured” list.

Start the server (Ctrl+Atl+R) or the green “go” button on the right hand side of the Servers tab.

Page is served at: http://localhost:8080/basic_demo/hello

As a test…

Make a change to the servlet. Ctrl+S and then Ctrl+B.

Wait for INFO: Reloading Context with name [/basic_demo] is completed

Eclipse deploys the web app artifacts to here:
EclipseWorkspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\tomcatbasicdemo