简体   繁体   English

mvn test - 覆盖 application.properties 中的值

[英]mvn test - override values in application.properties

I have these properties in my application.properties :我的application.properties中有这些属性:

spring.datasource.url=jdbc:postgresql://localhsost:5432/myDatabase
spring.datasource.username=myUsername

I would like to run mvn test with other values than the above, for example:我想使用上述以外的其他值运行mvn test ,例如:

spring.datasource.url=jdbc:postgresql://my.test.server.com:5432/myDatabase
spring.datasource.username=anotherUsername

I tried the following我尝试了以下

mvn test -Drun.arguments='--spring.datasource.jdbc:postgresql://my.test.server.com:5432/myDatabase --spring.datasource.username=anotherUsername'

and without spring prefix:并且没有spring前缀:

mvn test -Drun.arguments='--datasource.jdbc:postgresql://my.test.server.com:5432/myDatabase --datasource.username=anotherUsername'

But this does not seem to work.但这似乎不起作用。 How can I override the values in the application.properties in context of running mvn test ?如何在运行mvn test的上下文中覆盖application.properties中的值?

Something like this should work:这样的事情应该工作:

  <plugin>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.20.1</version>
    <configuration>
      <systemPropertyVariables>
        <spring.datasource.jdbc>value</spring.datasource.jdbc>
      </systemPropertyVariables>
    </configuration>
  </plugin>

But more often we do this by placing a test version of application.properties into the src/test/resources .但更多情况下,我们通过将application.properties的测试版本放入src/test/resources做到这一点。 During testing, that file will have greater priority.在测试期间,该文件将具有更高的优先级。

When overriding parameters in the command line, use a comma as separator, not a space:在命令行中覆盖参数时,使用逗号作为分隔符,而不是空格:

mvn test -Drun.arguments='--spring.datasource.url=...,--spring.datasource.username=...'

This should work too:这也应该有效:

mvn test -Dspring.datasource.url=... -Dspring.datasource.username=...

Edit from april 2021 2021 年 4 月起编辑

The syntax above was valid for Spring Boot 1.X.上面的语法对 Spring Boot 1.X 有效。 With Spring Boot 2.0/2.1, use:使用 Spring Boot 2.0/2.1,使用:

mvn test -Dspring-boot.run.arguments='--spring.datasource.url=...,--spring.datasource.username=...'

And with Spring Boot 2.2, the syntax was changed again (use a whitespace as separator):在 Spring Boot 2.2 中,再次更改了语法(使用空格作为分隔符):

mvn test -Dspring-boot.run.arguments='--spring.datasource.url=... --spring.datasource.username=...'

Other answers and comments mention using profiles and put a custom application.properties in /src/test/resources , which is not a viable solution for you since you use different pipelines, but if I remember correctly, you can even use application-{profile}.properties in /src/test/resources .其他答案和评论提到使用配置文件并将自定义application.properties放在/src/test/resources ,这对您来说不是一个可行的解决方案,因为您使用不同的管道,但如果我没记错的话,您甚至可以使用application-{profile}.properties/src/test/resources This way you should be able to maintain one test profile per pipeline, where you put your custom parameters, and then test your pipeline with:通过这种方式,您应该能够为每个管道维护一个测试配置文件,您可以在其中放置自定义参数,然后使用以下命令测试您的管道:

mvn test -Dspring.profiles.active=foobar

Option 1 ( preferred as is Maven structure-specific )选项 1首选,因为 Maven 结构特定

Create an application.properties under the test/resources to be picked up for your testing purposestest/resources下创建一个application.properties以进行测试

Option 2 ( Spring Test fine-tuning a particular Test class alone )选项 2Spring Test 单独微调特定的测试类

Override your properties directly on the Test class by inlining the ones you want by using @TestPropertySource通过使用@TestPropertySource内联您想要的属性,直接在 Test 类上覆盖您的属性

Option 3 ( Spring Boot - multiple properties files or a single YAML file )选项 3Spring Boot - 多个属性文件或单个 YAML 文件

Group the props under a Spring Profile ( Example here ) and invoke it directly from maven: mvn test -Dspring.profiles.active="myOtherSpringProfile"在 Spring Profile 下将 props 分组( 此处的示例)并直接从 maven 调用它: mvn test -Dspring.profiles.active="myOtherSpringProfile"

Create another application-dev.properties file and paste:创建另一个application-dev.properties文件并粘贴:

spring.datasource.url=jdbc:postgresql://my.test.server.com:5432/myDatabase
spring.datasource.username=anotherUsername

Then run with the option -Dspring.profiles.active=dev in your mvn command.然后在mvn命令中使用选项-Dspring.profiles.active=dev运行。

  • Eg: mvn test -Dspring.profiles.active=dev例如: mvn test -Dspring.profiles.active=dev

You can add as many profiles as needed.您可以根据需要添加任意数量的配置文件。

  • syntax: application-<profile name>.properties语法: application-<profile name>.properties

I don't see many people using the environment variable option.我没有看到很多人使用环境变量选项。 If you set an environment variable for corresponding properties, then the value in the environment variable will be used.如果为相应的属性设置环境变量,则将使用环境变量中的值。 eg例如

Environment variables: SPRING_DATASOURCE_URL="jdbc:postgresql://my.test.server.com:5432/myDatabase" SPRING_DATASOURCE_USERNAME=anotherUsername环境变量:SPRING_DATASOURCE_URL="jdbc:postgresql://my.test.server.com:5432/myDatabase" SPRING_DATASOURCE_USERNAME=anotherUsername

Inside the properties file: spring.datasource.url=jdbc:postgresql://localhsost:5432/myDatabase spring.datasource.username=myUsername在属性文件中:spring.datasource.url=jdbc:postgresql://localhsost:5432/myDatabase spring。

The application will use the values in the environment variables.应用程序将使用环境变量中的值。 For this to work you'll need to follow the naming convention.为此,您需要遵循命名约定。 Use uppercase and replace "."使用大写并替换“.” with "_".和 ”_”。

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

相关问题 寻找一种在应用程序运行时覆盖 application.properties 值的方法 - Looking for a way to override application.properties values during application runtime 如何在Spring Boot集成测试中覆盖application.properties? - How to override application.properties in Spring Boot integration test? 覆盖application.properties中的值 - Override value in application.properties 从命令行覆盖application.properties文件中的值 - override values in application.properties files from command line Junit 5 带测试应用程序.properties - Junit 5 with test application.properties 在 Junit 测试中覆盖默认的 Spring-Boot application.properties 设置 - Override default Spring-Boot application.properties settings in Junit Test 使用动态值覆盖 Junit 测试中的默认 Spring-Boot application.properties 设置 - Override default Spring-Boot application.properties settings in Junit Test with dynamic value 子应用程序中的application.properties文件不会覆盖主应用程序中的application.properties文件 - application.properties file from child application not override application.properties file in main application 用application.properties覆盖默认的Spring-Boot application.properties - Override default Spring-Boot application.properties with application.properties 如何通过 application.properties 注入日期值 - How to inject Date values by application.properties
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM