首页 > 解决方案 > 如何将 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);
    }

}

标签: springmavenspring-bootspring-boot-maven-pluginmaven-profiles

解决方案


有两种方式:</p>

  1. 移至。src/test/resources/application.properties_src/main/resources/application.properties

  2. 添加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>

推荐阅读