DevOps Zone is brought to you in partnership with:

Manuel has posted 1 posts at DZone. View Full User Profile

Fast Setup for a Lightweight Maven Development Environment

09.15.2013
| 2002 views |
  • submit to reddit

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:

<plugin>
    <groupId>org.eclipse.jetty</groupId>
    <artifactId>jetty-maven-plugin</artifactId>
    <version>9.0.5.v20130815</version>
    <configuration>
        <scanIntervalSeconds>0</scanIntervalSeconds>
        <jettyXml>jetty/jetty.xml</jettyXml>
        <webApp>
             <extraClasspath>${basedir}/jetty/classpath</extraClasspath>
             </webApp>
     </configuration>
     <dependencies>
         <dependency>
            <groupId>org.hsqldb</groupId>
            <artifactId>hsqldb</artifactId>
            <version>2.2.9</version>
            <scope>runtime</scope>
         </dependency>
    </dependencies>
</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">
         <Arg></arg>
         <Arg>jdbc/myAppDataSource</Arg>
         <Arg>
             <New class="org.hsqldb.jdbc.JDBCDataSource">
                 <Set name="DatabaseName">file:database/application</Set>
                 <Set name="User">user</Set>
                 <Set name="Password">user</Set>
             </New>
         </Arg>
     </New>
</Configure>

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

Why 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!


HSQLDB GUI

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:

jdbc:hsqldb:file:${projectHome}/database/${dbname};lock_file=false;readonly=true

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!

Published at DZone with permission of its author, Manuel Rascioni.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Danny Trieu replied on Mon, 2013/09/16 - 10:47am

SBT will get you there earlier...:)

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.