Starting an H2 Database Server from Maven?

后端 未结 9 1700
不思量自难忘°
不思量自难忘° 2021-01-30 07:09

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 t

9条回答
  •  悲哀的现实
    2021-01-30 07:36

    I create a file-based H2 database before unit tests are run. The file lives in the target directory and can be removed at any time using mvn clean.

    I use the maven-sql-plugin as follows:

    
      org.codehaus.mojo
      sql-maven-plugin
      1.5
      
        
          com.h2database
          h2 
          1.3.166
        
      
      
        org.h2.Driver
        jdbc:h2:file:target/db/testdb
        sa
        
        true
        ${maven.test.skip}
      
      
        
          create-db
          process-test-resources
          
            execute
          
          
            
              ${sql.dir}/drop_db.sql
              ${sql.dir}/tables.sql
              ${sql.dir}/constraints.sql
              ... etc ...
            
          
        
      
    
    

    The database can be created by running mvn process-test-resources. When tests are run, make sure you connect to the database in target/db/testdb via hibernate properties.

    
    

    You will also need to a dependency on com.h2database.h2 in maven's dependencies.

提交回复
热议问题