通过maven的profile注入去解决Spring/SpringBoot的profile的问题。不强依赖监控系统

Spring

profile配置

  1. <profiles>
  2. <profile>
  3. <!-- 本地开发环境 -->
  4. <id>dev</id>
  5. <properties>
  6. <profiles.active>dev</profiles.active>
  7. </properties>
  8. <activation>
  9. <activeByDefault>true</activeByDefault>
  10. </activation>
  11. </profile>
  12. <profile>
  13. <!-- 测试环境 -->
  14. <id>test</id>
  15. <properties>
  16. <profiles.active>test</profiles.active>
  17. </properties>
  18. </profile>
  19. <profile>
  20. <!-- 生产环境 -->
  21. <id>prod</id>
  22. <properties>
  23. <profiles.active>prod</profiles.active>
  24. </properties>
  25. </profile>
  26. </profiles>

Plugin配置

  1. <plugin>
  2. <groupId>org.apache.maven.plugins</groupId>
  3. <artifactId>maven-war-plugin</artifactId>
  4. <configuration>
  5. <!-- 激活spring profile -->
  6. <webResources>
  7. <resource>
  8. <filtering>true</filtering>
  9. <directory>src/main/webapp</directory>
  10. <includes>
  11. <include>**/web.xml</include>
  12. </includes>
  13. </resource>
  14. </webResources>
  15. <warSourceDirectory>src/main/webapp</warSourceDirectory>
  16. <webXml>src/main/webapp/WEB-INF/web.xml</webXml>
  17. </configuration>
  18. </plugin>

web.xml 配置

  1. <context-param>
  2. <param-name>spring.profiles.active</param-name>
  3. <param-value>${profiles.active}</param-value>
  4. </context-param>

启动注入

  1. @HandlesTypes(WebApplicationInitializer.class)
  2. public class FuckConfig implements ServletContainerInitializer {
  3. @Override
  4. public void onStartup(Set<Class<?>> c, ServletContext ctx) throws ServletException {
  5. //todo 这里可以做个默认的.根据自己的选择了
  6. System.setProperty("spring.profiles.active", ctx.getInitParameter("spring.profiles.active"));
  7. }
  8. }

SPI

image.png

maven编译

  1. mvn clean compile package -Dmaven.test.skip=true -Pdev

SpringBoot

profile配置

  1. <profile>
  2. <id>dev</id>
  3. <activation>
  4. <activeByDefault>true</activeByDefault>
  5. </activation>
  6. <properties>
  7. <activatedProperties>dev</activatedProperties>
  8. </properties>
  9. <build>
  10. <finalName>user-chat-operating</finalName>
  11. <resources>
  12. <resource>
  13. <directory>src/main/resources</directory>
  14. <excludes>
  15. <exclude>application*.properties</exclude>
  16. </excludes>
  17. </resource>
  18. <resource>
  19. <directory>src/main/resources</directory>
  20. <filtering>true</filtering>
  21. <includes>
  22. <include>application.properties</include>
  23. <include>application-${activatedProperties}.properties</include>
  24. </includes>
  25. </resource>
  26. </resources>
  27. </build>
  28. </profile>

这里只是一个环境,多个环境按照这个配置即可

properties配置

application.properties

  1. spring.profiles.active=@activatedProperties@

maven编译

  1. mvn clean compile package -Dmaven.test.skip=true -Pdev

编译完成之后target如下所示:

  1. target git:(master) tree classes
  2. classes
  3. ├── META-INF
  4. └── chat-web.kotlin_module
  5. ├── application-dev.properties
  6. ├── application.properties
  7. ├── com