2uluaa
Last Updated: April 15, 2017
·
3.424K
· rascio

Fast setup for a lightweight Maven dev environment

Since I started my carrieer I always hated wasting time setting up my dev environment. It happened that I had to install an entire Oracle Server on my (poor) computer, just because who set up that project didn't know that for dev environment also mysql will fit good. Slowly down my computer, having problem during the installations, setting up an SSO system on my machine, or worse without installing the database, using one in a server for all the developers, that when someone break something, or there is some network problem, I can't do my job!

And in the end, installing stuffs and solving related problems, take 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) it's good to me, using Maven, Jetty and HSQLDB is possibile to start an application without have to install the database and the application server, having a code that will work indipendently from the environment (or use some trick to hack little pieces of code), and you can prepare a clean computer installing on it just the VCS (SVN, git, etc..) and Maven.
The only requirements installed on the machine are:

  • Java 1.7+ (jetty require it)
  • Maven 3+

Maven configuration

So, let's start configure 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 have the minimum 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 add 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 put externalized resources (I use it for the properties file of the project).

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 connect 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 nothing more than Maven.

Did you notice the dependency in the Maven plugin configuration? Thank to it, we don't have to do nothing with HSQLDB to make it works!

Using the jdbc url file:database/{dbname}, we are saying 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, now?

Now, the environment it's ready, we have both the servlet container and the database for free, what we miss to do is retrieve the database in our application.

The internet it's full of articles on how retrieve a javax.sql.DataSource from JNDI.

What I like is Spring, so put in a sping 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 db is created inside ${projectHome}/database, so, don't forget to make your VCS (SVN, git, etc..) to ignore it!

HSQLDB GUI

If you want to see the database HSQLDB offer a GUI to do it, to start it launch the 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 will 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 also if the application is using it.

Little consideration

All this can be very good if you use some ORM (JPA, Hibernate, etc..) that create 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!

Say Thanks
Respond

1 Response
Add your response

28806

is this for only webprojects?.. i am trying to start in a simple java project and it complains about web..as jetty is a webserver.

over 1 year ago ·