通过 Maven 在 SpringBoot 中配置活动配置文件

我正在尝试使用 Maven3在 SpringBoot 应用程序中设置一个活动配置文件。
在 pom.xml 中,我将 默认活动配置文件和属性 Spring.profile 激活设置为 发展:

<profiles>
<profile>
<id>development</id>
<properties>
<spring.profiles.active>development</spring.profiles.active>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
</profiles>

但是每次运行我的应用程序时,我都会在日志中收到以下消息:

No active profile set, falling back to default profiles: default

SpringBoot 配置文件被设置为 default (读取 application.properties 而不是 application-development. properties)

要使用 Maven 配置文件设置 SpringBoot 活动配置文件,我还应该做些什么?
非常感谢你的帮助。

195723 次浏览

你应使用 Spring Boot Maven 插件:

<project>
...
<build>
...
<plugins>
...
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.5.1.RELEASE</version>
<configuration>
<profiles>
<profile>foo</profile>
<profile>bar</profile>
</profiles>
</configuration>
...
</plugin>
...
</plugins>
...
</build>
...
</project>

Maven 配置文件和 Spring 配置文件是两个完全不同的东西。Xml 定义的 spring.profiles.active变量在构建过程中可用,但在运行时不可用。这就是为什么只激活默认配置文件。

如何用 Spring 绑定 Maven 配置文件?

您需要将构建变量传递给应用程序,以便它在启动时可用。

  1. application.properties中定义一个占位符:

    spring.profiles.active=@spring.profiles.active@
    

    @spring.profiles.active@变量必须与 Maven 配置文件中声明的属性匹配。

  2. 在 pom.xml 中启用资源过滤:

    <build>
    <resources>
    <resource>
    <directory>src/main/resources</directory>
    <filtering>true</filtering>
    </resource>
    </resources>
    …
    </build>
    

    执行构建时,Maven 将处理 src/main/resources目录中的所有文件,application.properties中的占位符将被替换为 Maven 配置文件中定义的变量。

要了解更多细节,您可以在我描述这个用例的地方使用 去我的岗位

您可以使用以下命令运行:

spring-boot:run -Drun.jvmArguments="-Dspring.profiles.active=local"

或者说很简单:

mvn spring-boot:run -Dspring-boot.run.profiles={profile_name}

有多种方法可以设置 Springboot 应用程序的配置文件。

  1. 您可以在属性文件中添加以下内容:

    spring.profiles.active=dev
    
  2. Programmatic way:

    SpringApplication.setAdditionalProfiles("dev");
    
  3. Tests make it very easy to specify what profiles are active

    @ActiveProfiles("dev")
    
  4. In a Unix environment

    export spring_profiles_active=dev
    
  5. JVM System Parameter

    -Dspring.profiles.active=dev
    

Example: Running a springboot jar file with profile.

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

在开发中,激活一个特定的 Maven 配置文件时激活一个 SpringBoot 配置文件是直接的。您应该在 Maven 配置文件中使用 Spring-boot-maven-pluginprofiles资产,例如:

<project>
<...>
<profiles>
<profile>
<id>development</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<profiles>
<profile>development</profile>
</profiles>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profiles>
</...>
</project>

可以运行以下命令来使用 Spring Boot 和 Maven development配置文件:

mvn spring-boot:run -Pdevelopment

如果您希望能够将任何 Spring Boot 配置文件映射到具有相同配置文件名的 Maven 配置文件,则可以定义单个 Maven 配置文件,并且在检测到 Maven 属性时启用该配置文件。这个属性是运行 mvn命令时需要指定的唯一属性。
这个简介看起来像是:

    <profile>
<id>spring-profile-active</id>
<activation>
<property>
<name>my.active.spring.profiles</name>
</property>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<profiles>
<profile>${my.active.spring.profiles}</profile>
</profiles>
</configuration>
</plugin>
</plugins>
</build>
</profile>

您可以运行以下命令来使用 Spring Boot 和 Maven development配置文件:

mvn spring-boot:run -Dmy.active.spring.profiles=development

或:

mvn spring-boot:run -Dmy.active.spring.profiles=integration

或:

 mvn spring-boot:run -Dmy.active.spring.profiles=production

所以..。

这种配置是有意义的,因为在通用的 Maven 配置文件中,您依赖于传递来执行某些任务或赋值某些内容的 my.active.spring.profiles属性。
例如,我使用这种方法来配置一个通用的 Maven 配置文件,该配置文件对应用程序进行打包,并构建特定于所选环境的 docker 映像。

我想在不同的环境中运行一个自动化测试。
因此,我将这个命令添加到 command maven command:

spring-boot:run -Drun.jvmArguments="-Dspring.profiles.active=productionEnv1"

下面是我找到解决方案的链接: [1] https://github.com/spring-projects/spring-boot/issues/1095

我想澄清的优秀答案 https://stackoverflow.com/a/42391322/13134499从丹尼尔 Olszewski 如果你想使用它只是为了测试。

如何为 测试绑定 Maven 配置文件和 Spring?

  1. 像您所做的那样,在 pom.xml中定义变量
...
<properties>
<spring.profiles.active>development</spring.profiles.active>
</properties>
  1. src/test/resourcesapplication-test.properties中定义一个占位符:
spring.profiles.active=@spring.profiles.active@
  1. 启用资源过滤 pom.xml:
<build>
...
<testResources>
<testResource>
<directory>src/test/resources</directory>
<filtering>true</filtering>
</testResource>
<testResources>
</build>

文件结构:

/src/main/resources

= >

应用性能:

spring.profiles.active:@spring.profiles.active@'

Application-dev. properties

Application-prod. properties

IDE-Eclipse:

Right click on the project=>Run As=>Run Configuration=>Arguments=>VM Arguments:-Dspring.profiles.active=dev

CMD:

mvn spring-boot:run -Dspring.profiles.active=dev
mvn clean install -Dspring.profiles.active=dev