Skip to main content

A point of pride for the TomEE community is a large library of examples contained in the TomEE GitHub project. Whenever you clone or otherwise download the TomEE GitHub project you get nearly fourteen dozen examples you can review and run to learn more, not just about TomEE, but Java EE in general.

The examples are like a free, code-centric cookbook that covers everything from programming a simple Java web application to CDI interceptors, to WebSockets with TLS Security. With 166 coded examples to choose from it seems there is something for everyone.

Getting started with TomEE and running its examples requires six simple steps.

Step 1: Download or Clone the Apache TomEE Project

Go to the Apache TomEE project on GitHub and there you can either download or get a URL to clone the TomEE project. Unzip or clone the file to your Desktop.

Step 2: Open a console in the tomee directory

Once the Apache TomEE project is on your desktop, open a console window and go into the TomEE directory.

$ cd Desktop/tomee/

Step 3: Run the Maven Install command

With your console windows open in the Desktop/tomee directory run Maven install without running the full suite of tests as shown below.

$ MAVEN_OPTS="-Xmx2g" mvn -DskipTests -T 1C -Pquick clean install

TomEE, with all of its example files and Tomcat, will take a while to install so you might want to come back in a few minutes depending on the speed of your machine. The -DskipTests option will reduce the amount of time it takes considerably.

Step 4: Move into an Example Folder

When you are done with the Maven install your TomEE directory structure should look like the following.

The tomee/examples directory is where you’ll find all 14 dozen examples. You can do what comes next with any of the examples, but for this tutorial, I’m selecting the cdi-basic example. Move your console window into that directory.

$ cd examples/cdi-basic/

Step 5: Run the Example Code

With your console windows in the example folder, all that is left is to run the TomEE example using a Maven install command again like so.

$ mvn clean install

This Maven install will only take a few seconds. It will build the example and run the tests. You can see if it’s successful by checking the output. It should look like the following.

Step 6: Review the Source Code

With the example code compiled and working, you can take a deeper look at the source code to learn about whatever topic is of interest. Learning from other people’s code is the best way to improve your own skill set.

For example, if I look at the code for the CDI-Basic example in my text editor I can see how the example was implemented and tested. In addition, many tests include a Readme.adoc file which explains the purpose of the test and what it’s doing. Most, if not all, of the unit tests, utilize Arquillian and are designed to run independently of the other tests.

Wrapping Up

Reviewing other people’s code is one of the best ways to learn how to program. The 166 examples included in the Apache TomEE project provide a treasure trove of lessons in using all kinds of Java EE components and APIs. Have fun with it and if you want, contribute your own examples to the project by joining the community!

Richard Monson-Haefel

Richard Monson-Haefel

Richard has more the 24 years of experience as a professional software developer and architect. He has written five books on enterprise Java including EJB, JMS, web services, and software architecture. He has served on the JCP executive committee and multiple expert groups, is the co-founder of OpenEJB and Apache Geronimo, was a Sr. Analyst for Burton Group (aka Gartner), and is a celebrated public speaker.
rmonson

Leave a Reply