Setting up a Java EE project

I am preparing an article for some JEE problems where I stumbled into the question: how to set up a Java EE 7 project from zero? There were not much usable resources on the internet when I searched. So I think this is a great opportunity to share my setup with you.

The landscape

Instead jumping right ahead to creating a Java EE 7 project, let me introduce the development landscape. To have a fairly good JEE application, you need an application server which can handle the Java Enterprise Edition requirements (CDI for example).

For this article I’ll use the following tools to set up a basic Java EE 7 project:

  • Maven 3.2.3
  • Java EE 7 (with a Java 8 JDK)
  • JBoss Developer Studio 8
  • JBoss WildFly 8.1.0.Final
  • H2 database

Now we’re ready to install the required software.

I’ll skip the installation of Maven and the JDK. However I suggest you to have at least the JDK installed prior you start installing any of the remaining tools: this is a requirement both for WildFly and DevStudio.

Installing WildFly

This is the easiest one: just download WilfFly form its homepage and extract the ZIP / TAR archive where you want it. So this was it.

The default profile which starts when you execute the standalone.sh (or standalone.bat on Windows) is configured in the standalone.xml which contains a Java EE 7 web profile. Details you can see on the WildFly homepage.

For us it is sufficient, we do not need any features from a full profile. However if you want to start WildFly in standalone mode with another profile, just execute:

./standalone.sh --server-config=standalone-full.xml

Above the standalone-full.xml is the XML file you want to use as the configuration.

Installing JBoss DevStudio

The installation of JBoss Developer Studio is easy too — however not as easy as WildFly because you have to walk through an installation process. I won’t go into the details so do not expect a step-by-step installation guide. The wizard is intuitive so you can handle it.

The only thing I want t to mention here is step 5 where you can select Platforms and Servers. This is the stepp where you can Add… your installed WildFly Server. Just navigate to your installation directory and select it. JBoss DevStudio will recognize (and later use) that application Server.

So this was it too: now you have an IDE and a server you can use to write and run enterprise Java projects.

Setting up the database

But most of the time you cannot live without storing and manipulating data persistently. For this it is a good thing to set-up a database. We will use a H2 database — which does not need any installation. Nor any setup: so you do not need to create a database or so itself. The only thing we need is to configure our project where it can find the database — and tell it that it is an H2 database. So when starting and the database is not available H2 creates the file where it stores your data.

The configuration how to achieve this I’ll show you in the next section. However you can launch the H2 database as a server and so you can establish a TCP connection and do not need a file-based approach.

Creating the project

After installed all software needed to create the project, let’s dig right into the development.

Create the project with Maven

I’ve chosen Maven because it is a common and for me easy way to set up a Java EE project. And this is because there are some archetypes around which you can use. For this project I’ve created the example with the Java EE 7 archetype for WildFly: wildfly-javaee7-webapp-blank-archetype. All because it gives you some configuration to have an expanded and almost working Java EE application from the beginning. And the good thing with archetypes is: you can create one to customize your needs — or just take one existing and extend it to fit our preferences.

The WildFly Java EE 7 blank archetype is almost working because you do not get Java classes (entities, controllers and so on) and no websites to display. Those need to be created by you.

I provided the following data when asked:

Group Id: biz.hahamo.dev.enterprise

Artifact Id: example

What you get is a persistence.xml and a data-source with a configured H2 memory based database. So let’s go on to configure the project to have a simple application to deploy — and extend it further.

Update the POM

As of writing this article the Java EE WildFly Archetype comes with the org.wildfly.bom version 8.0.0 — for WildFly 8.0.0. Thus we’ll use WildFly 8.1.0 let’s change the configuration in the pom.xml:

Change this line:

<version.jboss.bom>8.0.0.Final</version.jboss.bom>

to this one:

<version.jboss.bom>8.1.0.Final</version.jboss.bom>

That’s it, we have all the dependencies from WildFly 8.1.0.

For convenience let’s change the value of the parameter version.arquillian.container from 8.0.0.Final to 8.1.0.Final too. This sets the version of an Arquilian-managed testing environment to be the same as that from WildFly.

The next thing we’ll need to change is the Java source and target version. It is set to 1.7 however we need it to be Java 8. So change these lines:

<maven.compiler.target>1.7</maven.compiler.target>
<maven.compiler.source>1.7</maven.compiler.source>

to these:

<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>

This sets the maven Java compiler version to Java 8. Naturally you need a Java 8 SDK installed and configured with JBoss DevStudio (and I assume you’ve chosen Java 8 when installing the IDE).

And for the beginning this is all we need to change.

Configure the database access

With the basic archetype we have an H2 database running and working in the server memory.

However if you want a database which is more durable I suggest to change to a file based version of H2. The easiest way to do this is to change the database configuration in the datasource. In this case it is located in the example-de.xml file. Here you need to change the connection URL from men to file. This tells H2 to persist the data in a file.

If you do not provide any path parameters, the db file is generated in the bin folder of your WildFly installation. So I suggest you add a path to your database configuration.

Naturally you can change the database from H2 to MySQL or any other database provider — even an H2 running in server mode. In a following article I’ll change to an Oracle database so I’ll show how to make this change.

Adding a page

If you start your site you want to see the application you’ve created.

However if you start this blank application you’ll end up with a Forbidden error message if you call the site. This is because there is no page to be displayed. For this we’ll simply add an HTML page with the following content to our project into the src/main/webapp folder and name it index.html:

<!DOCTYPE html>
<html>
   <head>
      <title>Start Page</title>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
   </head>
   <body>
      <h1>Hello World!</h1>
   </body>
</html>

This is simple and has almost no functionality.

The archetype generated a project with JSF included so we could use JSF too — however this I will show in a later article. For now it is ok to have a static HTML site.

Building, deploying and running the application

To build the application, the simplest way is to create a Maven execution with the clean install  goals. This compiles and packages your project into a WAR archive file and deploys it to your local maven repository too. Last step (the deployment to the local maven repository) is not needed. You could execute a clean compile or a clean package and it would result in the same: you can deploy your application. Eventually you can skip the Maven build too — because it doesn’t matter.

Since you’ve installed the WildFly server and configured JBoss DevStudio to use that runtime environment you can now open the Window -> Show View -> Servers view where you’ll see that WildFly instance.

To deploy the application just right-click the server and choose Add and Remove… and select the project and press Add > and confirm it with OK. From now you can access your version in your server — even if the server is running. So you do not need to recompile and redeploy your app or restart the server.

After the server is started (for example with right-click on the server and the chosen Start) you can access the website over http://localhost:8080/example

Now if you change the text in the index.html file from Hello World! to Hello Example! and save the file and refresh your browser you’ll see the result immediately.

Source code

I’ve added the source code for this project to my GitHub repository. In the later articles I’ll just take this project as a base and use / extend it for later use.

Advertisements

2 thoughts on “Setting up a Java EE project

  1. Pingback: Java EE 7: JMS with ActiveMQ | JaPy Software
  2. Pingback: JPA 2.1 and Table Inheritance | JaPy Software

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s