如何将 Spring 引导配置文件与 Maven 配置文件集成?

How to integrate a Spring Boot profile with Maven profile?

我正在尝试将 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);
    }

}

根据文档 profilesjava.lang.String[]

所以相信它应该被定义:

    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <profiles>
                    dev,prod
                </profiles>
            </configuration>
        </plugin>
    </plugins>

还没有测试过,但我认为是这样,你定义所有你想要激活的配置文件(不是我认为你想要开发和生产同时激活)。

Spring boot plugin profiles

有两种方式:

  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>

在spring启动应用程序中,有几种设置配置文件的方法(dev、uat、prod等)

例如:您可以在 属性 文件中添加:

spring.profiles.active=dev

以编程方式:

SpringApplication.setAdditionalProfiles("dev");

要指定哪些配置文件处于活动状态,请使用此行

@ActiveProfiles("dev")

在 Unix 环境中

export spring_profiles_active=dev

运行 带有开发配置文件的 jar 文件。

java -jar -Dspring.profiles.active=dev JARNAME.jar

此处JARNAME.jar表示您的应用程序的jar名称