Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lightweight Java database with Maven plugin for starting/stopping?

For unit tests, demonstrations and Hibernate tasks I would like to use a small and simple Java database like Derby / Java DB or HSQLDB, which can be called from within Maven.

So far I have not found a Maven plugin which can download and launch Java DB (which is my favorite at the moment) or something similar.

like image 528
mjn Avatar asked Nov 17 '09 14:11

mjn


People also ask

Which plugin is needed to execute the project in maven?

Maven exec plugin allows us to execute system and Java programs from the maven command. There are two goals of the maven exec plugin: exec:exec - can be used to execute any program in a separate process. exec:java - can be used to run a Java program in the same VM.

What is Mvn exec Java?

mvn exec:java is a goal from the exec plugin for maven. It lets you specify a main class to execute (see pom. xml). This lets you avoid having to figure out the proper java command to run and classpath arguments and the like.

What is the correct syntax for executing a maven plugin?

Usage of a Maven Plugin xml you can use the shorthand notation to execute the plugin: mvn <prefix>:<goal> , commonly the “prefix” is the artifact ID minus the “-maven-plugin”. For example mvn example:version .


1 Answers

An simple setup for unit tests is to start hsqldb in memory:

db.connection.driver_class=org.hsqldb.jdbcDriver
db.connection.url=jdbc:hsqldb:mem:aname
db.connection.username=sa
db.connection.password=
hibernate.dialect=org.hibernate.dialect.HSQLDialect

No start and stop needed. The JDBC driver will "start" the database.

You could use that for demonstrations, too. If you're initializing the database while the applications starts.

The database setup can be done with hibernate.hbm2ddl.auto.


Edit by kdgregory:

To have Maven include HSQLDB in the dependencies for the test phase only, use this in your POM:

<dependency>
    <groupId>hsqldb</groupId>
    <artifactId>hsqldb</artifactId>
    <version>1.8.0.7</version>
    <scope>test</scope>
</dependency>
like image 178
Thomas Jung Avatar answered Sep 28 '22 02:09

Thomas Jung