Over a million developers have joined DZone.

Fast Setup for a Lightweight Maven Development Environment

DZone's Guide to

Fast Setup for a Lightweight Maven Development Environment

· DevOps Zone ·
Free Resource

Open source vulnerabilities are on the rise. Read here how to tackle them effectively.

Since I started my career, I always hated wasting time setting up my development environment. It just so happened that I had to install an entire Oracle Server on my (poor) computer, just because whoever set up that project didn't know that MySQL would also be a good fit.

The common problems would result in my computer slowing down, installation problems, having to set up an SSO system on my machine, or worse, having to use a database in a server for all the developers if I didn't install my own.  Then, when someone breaks something, or when there is at network problem, I can't do my job!

In the end, installing stuff and solving related problems takes a long time (I'm a programmer I want to code!) and it's always tedious!

I ended up with this solution that (for now, and for little projects) works great for me. I'm using MavenJetty and HSQLDB. In this setup it's possible to start an application without having to install the database and the application server.  It uses code that will work independently from the environment (or use some trick to hack little pieces of code), and you can prepare a clean computer by installing just Maven and the version control system on it. The only requirements installed on the machine are:

  • Java 1.7+ (Jetty requires it)
  • Maven 3+

Maven Configuration

Let's start by configuring Maven to help us to deploy and start our application.

First of all, we have to configure the jetty-maven-plugin:

Why Jetty?

Because it has bare minimum features needed by my project.  If you need something more complex, you can also use the Tomcat plugin for Maven, but I don't have a guide for that!

In our pom.xml adds the following plugin:


With this fragment we are setting up a Jetty instance with the configuration file (jetty.xml) and a classpath folder where we put externalized resources (I use it for the properties file).

Jetty Configuration

Continue creating the file jetty.xml in your project at ${projectHome}/jetty/jetty.xml.

Now we can use our jetty.xml to configure the JNDI datasource:

<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd">

<Configure id="Server" class="org.eclipse.jetty.server.Server">
     <New id="myApplicationDs" class="org.eclipse.jetty.plus.jndi.Resource">
             <New class="org.hsqldb.jdbc.JDBCDataSource">
                 <Set name="DatabaseName">file:database/application</Set>
                 <Set name="User">user</Set>
                 <Set name="Password">user</Set>

With this we are configuring a JNDI datasource that connects to a HSQLDB.


It can create the database both in-memory and in a file. In this way, we don't have to install anything other than Maven.

Did you notice the dependency in the Maven plugin configuration? Thanks to that, we don't have to do anything with HSQLDB to make it work!

Using the JDBC URL file:database/{dbname}, we are telling to HSQLDB to use a file to store the database (in the integration test configuration, we can also use it to start up an in-memory database to use just for the tests).

Using the Database

So what now?

Now, the environment is ready. We have both the servlet container and the database for free. What we still need to do is retrieve the database in our application.

The internet is full of articles on how retrieve a javax.sql.DataSource from JNDI.

I like using Spring, so I put it in a Spring context:

 <jee:jndi-lookup jndi-name="jdbc/myAppDataSource" id="dataSource" />

In the end, to start up our application server with the application let's run:

mvn jetty:run

Wait...where is the database?

Once the application is started, the database is created inside ${projectHome}/database, so don't forget to make your version control system ignore it!


If you want to use the HSQLDB GUI, run this command:

java -cp hsqldb-2.2.9.jar org.hsqldb.util.DatabaseManager

You can find the jar in your Maven repository folder ($USER_HOME/.m2/repository).

When you are asked for the database path, put:


Using lock_file and readonly, you can connect to your database even if the application is using it.

A Little Consideration

All this can be very useful if you use an ORM (JPA, Hibernate, etc.) that creates the database for you at the start of the application. Otherwise, you have to setup a script or create it manually.

The End

Hope that you enjoyed it!

Learn about the ins and outs of open source security and management.


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}