spring - 如何将 Spring Boot 配置文件与 Maven 配置文件集成?
问题描述
我正在尝试将Spring Boot
配置文件与Maven
配置文件集成,但由于某种原因,default
配置文件总是被拾取。
mvn clean test -Dspring.profiles.active=prod (工作)
日志:
2019-07-01 17:02:15.013 INFO 21872 --- [ main] com.example.BrowserTest : The following profiles are active: prod
2019-07-01 17:02:15.405 INFO 21872 --- [ main] com.example.BrowserTest : Started BrowserTest in 0.743 seconds (JVM running for 1.757)
************************************************************************
************************************************************************
Site: https://www.yahoo.com
************************************************************************
************************************************************************
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.509 s - in com.example.BrowserTest
mvn clean test -Prod(不起作用,总是选择默认配置文件)
日志:
2019-07-01 17:03:20.136 INFO 17532 --- [ main] com.example.BrowserTest : The following profiles are active: @activatedProperties@
2019-07-01 17:03:20.535 INFO 17532 --- [ main] com.example.BrowserTest : Started BrowserTest in 0.727 seconds (JVM running for 1.706)
************************************************************************
************************************************************************
Site: http://www.default.com
************************************************************************
************************************************************************
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.456 s - in com.example.BrowserTest
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>profiles-junit-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>profiles-junit-demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<profiles>
<profile>dev</profile>
<profile>prod</profile>
</profiles>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>dev</id>
<properties>
<activatedProperties>dev</activatedProperties>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>prod</id>
<properties>
<activatedProperties>prod</activatedProperties>
</properties>
</profile>
</profiles>
</project>
src/test/resources/application.properties
spring.profiles.active=@activatedProperties@
site=http://www.default.com
src/test/resources/application-dev.properties
site=https://www.google.com
src/test/resources/application-prod.properties
site=https://www.yahoo.com
BrowserTest.java
public class BrowserTest extends ProfilesJunitDemoApplicationTests {
@Value("${site}")
private String site;
//mvn clean test
//mvn clean test -Dspring.profiles.active=dev
//mvn clean test -Dspring.profiles.active=prod
//mvn clean test -Pprod - NOT WORKING :(
@Test
public void homePage() {
System.out.println("************************************************************************");
System.out.println("************************************************************************");
System.out.println("Site: " + site);
System.out.println("************************************************************************");
System.out.println("************************************************************************");
assertNotNull(site);
}
}
解决方案
有两种方式:</p>
移至。
src/test/resources/application.properties
_src/main/resources/application.properties
添加
testResources
:
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<testResources>
<testResource>
<directory>src/test/resources</directory>
<filtering>true</filtering>
</testResource>
</testResources>
推荐阅读
- selenium - selenium-如何在 driver.back() 之后使用同一页面的元素
- c# - C# DateTimePicker,如何在单击上下按钮时检测事件?
- javascript - 如何解析具有更新 DOM 的 html?迅速
- reactjs - 捕获表单中的任何更改 - React
- python - 无法通过 CodeSignal 隐藏测试
- python - 如何更改 pip 版本
- oracle - OBIEE12c 配置失败,Oracle 数据库 19c
- javascript - 在 JavaScript 中将数字转换为特定大小的整数(复制 Solidity 功能)
- asp.net-core - 用户登录 Asp.Net Identity Core 时将动态数据返回到用户配置文件
- flutter - 如何在文本字段中添加两个十进制数字