在开发中,当激活特定的 Maven 配置文件时激活 Spring Boot 配置文件是直接的。您应该在 Maven 配置文件中使用 spring-boot-maven-plugin 中的 the profiles property,例如:
<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 映像。