通过maven的profile注入去解决Spring/SpringBoot的profile的问题。不强依赖监控系统
Spring
profile配置
<profiles>
<profile>
<!-- 本地开发环境 -->
<id>dev</id>
<properties>
<profiles.active>dev</profiles.active>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<!-- 测试环境 -->
<id>test</id>
<properties>
<profiles.active>test</profiles.active>
</properties>
</profile>
<profile>
<!-- 生产环境 -->
<id>prod</id>
<properties>
<profiles.active>prod</profiles.active>
</properties>
</profile>
</profiles>
Plugin配置
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<!-- 激活spring profile -->
<webResources>
<resource>
<filtering>true</filtering>
<directory>src/main/webapp</directory>
<includes>
<include>**/web.xml</include>
</includes>
</resource>
</webResources>
<warSourceDirectory>src/main/webapp</warSourceDirectory>
<webXml>src/main/webapp/WEB-INF/web.xml</webXml>
</configuration>
</plugin>
web.xml 配置
<context-param>
<param-name>spring.profiles.active</param-name>
<param-value>${profiles.active}</param-value>
</context-param>
启动注入
@HandlesTypes(WebApplicationInitializer.class)
public class FuckConfig implements ServletContainerInitializer {
@Override
public void onStartup(Set<Class<?>> c, ServletContext ctx) throws ServletException {
//todo 这里可以做个默认的.根据自己的选择了
System.setProperty("spring.profiles.active", ctx.getInitParameter("spring.profiles.active"));
}
}
SPI
maven编译
mvn clean compile package -Dmaven.test.skip=true -Pdev
SpringBoot
profile配置
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<activatedProperties>dev</activatedProperties>
</properties>
<build>
<finalName>user-chat-operating</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>application*.properties</exclude>
</excludes>
</resource>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>application.properties</include>
<include>application-${activatedProperties}.properties</include>
</includes>
</resource>
</resources>
</build>
</profile>
这里只是一个环境,多个环境按照这个配置即可
properties配置
application.properties
spring.profiles.active=@activatedProperties@
maven编译
mvn clean compile package -Dmaven.test.skip=true -Pdev
编译完成之后target如下所示:
➜ target git:(master) ✗ tree classes
classes
├── META-INF
│ └── chat-web.kotlin_module
├── application-dev.properties
├── application.properties
├── com