Starting an H2 Database Server from Maven?

roufamatic picture roufamatic · Feb 5, 2010 · Viewed 44.3k times · Source

Suppose I want to create and use an H2 database for my integration tests.

Maven has a command to run tests: mvn test.

Is there a way to tell maven to start an H2 database server for the tests and stop it when it's done?

I imagine this working similar to how I can run tomcat via a Maven command (mvn tomcat:run).

Sorry if this question is nonsensical, I'm still wrapping my head around new concepts.

Answer

roufamatic picture roufamatic · Feb 8, 2010

I was able to get it to work without using an external server just by adding the dependency to H2 via Maven and then using this bean:

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="org.h2.Driver"/>
    <property name="url" value="jdbc:h2:file:h2\db"/>
    <property name="username" value="sa"/>
    <property name="password" value=""/>        
</bean>

Then again, this required that I use a file-based DB instead of in-memory. But it does the trick.