我正在尝试使用 Maven 3 在 Spring Boot 应用程序中设置活动配置文件; 我在 pom.xml 中设置了 默认活动配置文件,并将属性 spring.profiles.active 设为 开发:
<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 配置文件设置为默认(读取 application.properties,而非 application-development.properties)
<br>;
我还应该怎么做才能使用 Maven 配置文件设置 SpringBoot 的活动配置文件?
如蒙帮助,不胜感激。
Maven 配置文件和 Spring 配置文件是两个完全不同的东西。您的 pom.xml 定义了 spring.profiles.active
变量,该变量在构建过程中可用,但在运行时不可用。这就是为什么只有默认配置文件被激活的原因。
**如何将 Maven 配置文件与 Spring 绑定?
您需要将构建变量传递给应用程序,使其在启动时可用。
1.在application.properties
中定义一个占位符:
[email protected]@
@spring.profiles.active@
变量必须与 Maven 配置文件中声明的属性相匹配。
2.在 pom.xml 中启用资源过滤:
在 pom.xml 中启用资源过滤: <build>;
<resources>;
<resource>;
<目录>src/main/resources</目录>;
<过滤>true</过滤>;
</resource>;
</resources>;
...
</build>;
执行构建时,Maven 将处理 src/main/resources
目录中的所有文件,并用 Maven 配置文件中定义的变量替换 application.properties
中的占位符。
更多详情,请参阅我的帖子]1,我在其中描述了这一用例。
您可以使用以下命令运行。在这里,我要使用 Spring profile local
运行:
spring-boot:run -Drun.jvmArguments="-Dspring.profiles.active=local"
您应该使用 Spring Boot Maven Plugin:
<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>