简体   繁体   English

在Spring Boot中的同一个pom.xml中管理H2和Postgres

[英]Manage H2 and Postgres in same pom.xml in Spring Boot

I am developing a micro-service application using Spring Boot. 我正在使用Spring Boot开发一个微服务应用程序。 My application will use for production configuration a Postgres DB and for Spring Boot auto-test a H2 DB. 我的应用程序将用于生产配置Postgres DB和Spring Boot自动测试H2 DB。 My pom.xml includes therefore both dependencies (H2 + Postgres). 因此,我的pom.xml包含两个依赖项(H2 + Postgres)。 I tried to associate H2 dependency with tes scope, and Postgres with runtime as following: 我尝试将H2依赖关系与tes范围相关联,Postgres与运行时关联如下:

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>test</scope>
        </dependency>

I can see when running mvn test that Spring Boot selects by default postgres database which is not present within my unit test environment. 我可以看到运行mvn测试时 ,Spring Boot默认选择postgres数据库,这在我的单元测试环境中不存在。 This is the reason why I prefer using H2 for running unit tests. 这就是为什么我更喜欢使用H2来运行单元测试的原因。

Is there a proper way to tell spring boot to use H2 for test and Postgres otherwise? 有没有一种正确的方法来告诉spring boot使用H2进行测试而Postgres使用其他方法?

I don't know if using different application.properties file (one in src/main/resources and the other in src/test/resources) would solve the issue. 我不知道是否使用不同的application.properties文件(一个在src / main / resources中,另一个在src / test / resources中)可以解决问题。

You should be aware that there are multiple class paths, such as: 您应该知道有多个类路径,例如:

  1. The compile-time classpath, 编译时类路径,
  2. The runtime classpath, 运行时类路径,
  3. The testing classpath. 测试类路径。

When you use <scope>runtime</scope> , the dependency will be available in both the runtime classpath as the testing classpath , as mentioned by the documentation : 当您使用<scope>runtime</scope> ,依赖项将在运行时类路径中作为测试类路径使用 ,如文档所述

This scope indicates that the dependency is not required for compilation, but is for execution. 此范围表示编译不需要依赖项,但是用于执行。 It is in the runtime and test classpaths, but not the compile classpath. 它位于运行时和测试类路径中,但不是编译类路径。

That means that even when you're executing your tests, Postgres would still be on your classpath if you use <scope>runtime</scope> . 这意味着即使在执行测试时,如果使用<scope>runtime</scope> ,Postgres仍会在您的类路径上。


The solution you mentioned, by providing two separate application.properties is the right choice. 通过提供两个单独的application.properties ,您提到的解决方案是正确的选择。

Within src/main/resources/application.properties , you could configure the datasource like this: src/main/resources/application.properties ,您可以像这样配置数据源:

spring.datasource.url=jdbc:postgresql://localhost:5432/mydatabase

Within src/test/resources/application.properties , you could configure the datasource like this: src/test/resources/application.properties ,您可以像这样配置数据源:

spring.datasource.url=jdbc:h2:mydatabase

If you need more fine-grained control, you can use Spring profiles. 如果需要更细粒度的控件,可以使用Spring配置文件。 For example, you could use a profile called "testdb", and then annotate your test using @ActiveProfiles("testdb") . 例如,您可以使用名为“testdb”的配置文件,然后使用@ActiveProfiles("testdb")注释您的测试。

Now you could create a file called application-testdb.properties and add the properties you need to set up your test database. 现在,您可以创建一个名为application-testdb.properties的文件,并添加设置测试数据库所需的属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM