从 Maven 启动 H2 数据库服务器?
假设我想创建并使用 H2 数据库进行集成测试。
Maven 有一个运行测试的命令:mvn test
。
有没有办法告诉maven启动H2数据库服务器进行测试并在完成后停止它?
我想这与我通过 Maven 命令运行 tomcat 的方式类似 (mvn tomcat:run
)。
抱歉,如果这个问题毫无意义,我仍在思考新概念。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
我能够在不使用外部服务器的情况下让它工作,只需通过 Maven 添加对 H2 的依赖,然后使用这个 bean:
话又说回来,这要求我使用基于文件的数据库而不是内存中的数据库。但它确实有效。
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:
Then again, this required that I use a file-based DB instead of in-memory. But it does the trick.
您可以使用启动和停止数据库的主要方法创建 2 个小类。这个想法是在运行集成测试之前运行 StartServer 类,然后在测试运行后运行 StopServer 类。
您应该按照 本文档 (描述用于在集成测试中启动和停止 Jetty)
在 pom.xml 中,您应该定义 maven-exec-plugin 来运行 exec:java 目标并创建 2 个执行(1 个用于调用 StartServer,1 个用于 StopServer):
希望这就是您想要的
you can create 2 small classes with main methods that start and stop the database. the idea is to run the StartServer class before the integration tests are run and then class StopServer after the tests have run.
you should do the same for your DB server as described somewhere in this document (description is for starting and stopping Jetty in integration tests)
in your pom.xml you should define the maven-exec-plugin to run the exec:java goal and create 2 executions (1 for calling StartServer and 1 for StopServer):
hope that's what you want
该插件可以很好地在集成测试(默认插件阶段)之前生成一个具有 tcp 模式的新 H2 DB: github 上的 h2-maven-plugin
它没有很好的文档记录,但您可以检查 Mojo 源代码以了解配置选项。它发布在 Maven Central 上。
基本上,对于集成测试,您可能希望 Maven:
这可以通过如下所示的 Maven 配置来实现。
假设您的集成测试使用自定义接口 JUnit 类别进行注释:
此 Maven 配置对我来说效果很好:
您可能希望在 tomcat 上下文文件上使用 Maven 过滤器,以便替换端口:
文件内容为:
或者如果您不这样做不需要 JNDI 数据源,您可以使用 Spring 声明的数据源,使用相同的属性...
如果您希望能够设置集成测试 tomcat,并从 IDE 运行集成测试,则需要额外一趟:
您可以使用属性来分叉或不分叉 Tomcat 服务器:
当您设置 fork=false 时,服务器将阻塞并且 Maven 将不会继续,因此集成测试将不会运行,但您将能够从您的IDE。
This plugin works fine to spawn a new H2 DB with tcp mode before integration tests (the default plugin phase): h2-maven-plugin on github
It is not well documented but you can check the Mojo sources to know the configuration options. It is published on maven central.
Basically, for integration tests, you may want Maven to:
This can be achieved with a Maven configuration looking like this.
Assuming your integration tests are annoted with a custom interface JUnit Category:
This Maven configuration works fine for me:
You may want to use maven filters on the tomcat context file so that the port is replaced:
With the file content being:
Or if you don't want a JNDI datasource, you can use a Spring declared dataSource, using the the same property...
One extra trip if you want to be able to setup your integration tests tomcat, and run the integration tests from your IDE:
You can use use a property to fork or not the Tomcat server:
When you set fork=false, the server will block and maven won't continue, so the integration tests won't be run, but you will be able to run them from your ide.
在运行单元测试之前,我创建了一个基于文件的 H2 数据库。该文件位于
target
目录中,可以随时使用mvn clean
删除。我使用 maven-sql-plugin 如下:
可以通过运行 mvn process-test-resources 来创建数据库。运行测试时,请确保通过 hibernate 属性连接到
target/db/testdb
中的数据库。您还需要在 maven 的依赖项中依赖 com.h2database.h2。
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 usingmvn clean
.I use the maven-sql-plugin as follows:
The database can be created by running
mvn process-test-resources
. When tests are run, make sure you connect to the database intarget/db/testdb
via hibernate properties.You will also need to a dependency on com.h2database.h2 in maven's dependencies.
在我的项目中,为了进行单元测试,我要求 Spring 处理数据库创建和初始化。正如H2文档中所述,您可以为此创建一个bean:
您只需当您开始单元测试时,需要使用此配置启动 Spring 上下文。
In my project, for unit testing, I asked Spring to handle this database creation and initialization. As stated in the H2 documentation, you can create a bean for that:
You simply need to start the Spring context with this configuration when you start your unit tests.
我刚刚开始为 maven @ bitbucket 开发 H2 插件项目。我将不胜感激任何帮助。
是
希望它会 有帮助。
I've just started project for H2 plugin for maven @ bitbucket. I will appreciate any help with it.
https://bitbucket.org/dohque/maven-h2-plugin
Hope it will be helpful.
如果您想将其保存在内存中,则只需使用不同的 URL:
您可以提供其他选项,例如: ;DB_CLOSE_DELAY=-1
参见: http://www.h2database.com/html/features.html#in_memory_databases
If you want to make it in memory, then just use a different URL:
You can give additional options, such as: ;DB_CLOSE_DELAY=-1
see: http://www.h2database.com/html/features.html#in_memory_databases
以下工作适合我(仅使用
h2
依赖项和exec-maven-plugin
):请注意,在我的
pom.xml
中com.h2database:h2
不是项目依赖项。如果您拥有它,您可能不需要将其显式命名为插件依赖项。
following does the job for me (just using
h2
dependency and theexec-maven-plugin
):please note, in my
pom.xml
thecom.h2database:h2
was not project dependency.In case you'd have it you might don't need to explicitly name it as a plugin dependency.
由于 H2 不提供 Maven 插件,您应该使用 maven-antrun-plugin 启动它。在 ant 任务中编写启动和停止 h2 引擎的代码,并在集成测试启动和停止时调用它。
请参阅 http://docs.codehaus.org/display/MAVENUSER 的详细信息/Maven+和+集成+测试
Since H2 doesn't provide Maven plugin you should start it using maven-antrun-plugin. Write code for start and stop h2 engine in ant task and call it when your integration test starts and stop.
See details on http://docs.codehaus.org/display/MAVENUSER/Maven+and+Integration+Testing