Starting an H2 Database Server from Maven?

h2, maven, testing

Solution

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.

Problem

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.

Original source

Related problems