[英]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=...
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 purposes在
test/resources
下创建一个application.properties
以进行测试
Option 2 ( Spring Test fine-tuning a particular Test class alone )选项 2 ( Spring 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 )选项 3 ( Spring 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
运行。
mvn test -Dspring.profiles.active=dev
mvn test -Dspring.profiles.active=dev
You can add as many profiles as needed.您可以根据需要添加任意数量的配置文件。
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.