Archive for September, 2013

Java, SQL with Maven, HSQLDB and Eclipse

Keep it simple, but not too simple [Einstein]


It’s not trivial to create a simple java project, at least there are these dependencies to manage. Further you may need an database server to do the first steps to an serious application (and testing)

A Solution

My solution uses

  • maven to create the application structure. Mind that a lot of open source applications use that structure
  • maven to manage the dependencies of the application
  • hsqldb as a lightweight database server
  • eclipse to code and test

Create the application structure

Here is what I use [theReference]

mvn archetype:generate \
  -DgroupId=your_tld.your_groupId \
  -DartifactId=your_app_name \
  -DarchetypeArtifactId=maven-archetype-quickstart \

(remember to replace: „your_tld.your_groupId“ and „your_app_name“)

So I get, what I want, an simple java project:

$ find your_app_name/

So maven created an source tree, an file „pom.xml“ an application file and it’s test pendant, too.

Now create the eclipse files, that’s simple, merely call:

$ cd your_app_name
$ mvn eclipse:eclipse

and get the files (.classpath and .project):

$ ls -alh
. .. .classpath .project pom.xml src

You may run this application file now with mvn package, but that’s not my focus here.

Handle an dependency

Let’s explain how to add an dependency, wisely that for [hsqldb]. To find the latest version use the product homepage or one of the repository viewers [example]. Mind: there is hsqldb/hsqldb and org.hsqldb/hsqldb, use the later one for current releases.

Dependencies with maven are managed within the file „pom.xml“

Due to the created test file there already is an dependency to JUNIT, so place the one we want to need beside of it:


Now it’s time to retrieve the dependency from the (default) repository „maven-central“. You may do this implictly by building the application (mvn package) but there’s an explicit command as you may expect:

mvn dependency:resolve

See what you’ve done:

$ ls -1 $HOME/.m2/repository/org/hsqldb/hsqldb/2.2.4/

Now again you may update your eclipse file (mvn eclipse:eclipse):

<classpathentry kind="var" path="M2_REPO/org/hsqldb/hsqldb/2.2.4/hsqldb-2.2.4.jar"/>

It created an Eclipse-Classpath-Variable called M2_REPO, which abstractly define the real path under your home-directory (see above).

Manage HSQLDB through Eclipse

I prefer to create one or more run configurations  in eclipse to run the hsqldb and it’s manager application.

First, for the server

  • Name: hsqldb-server
  • Main class: org.hsqldb.Server
  • Programm arguments: --database.0 file:your_db_name --dbname.0 your_db_name
  • Mind: replace „your_db_name“ (twice)

Second, for the Swing-Manager

  • Name: hsqldb-manager
  • Main class: org.hsqldb.util.DatabaseManagerSwing
  • Programm arguments: --driver org.hsqldb.jdbcDriver --url jdbc:hsqldb:hsql://localhost/your_db_name --user "SA" --password ""
  • Mind: replace „your_db_name“ again and realize that works only if you provide this configuration by the first time you start the manager and providing your settings.

Mind, that creates files in your „.metadata“ which you may want to backup:

$ ls -1 ..\.metadata\.plugins\org.eclipse.debug.core\.launches | grep hsqldb

Schreibe einen Kommentar