1.主要在pom.xml中做配置,如下:
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</pluginManagement>
<finalName>${project.artifactId}</finalName>
<filters>
<filter>src/main/resources/env/${env}.properties</filter>
</filters>
<resources>
<resource>
<!--拷贝并替换属性文件中的占位符,用profile中的环境变量 -->
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
<profiles>
<profile>
<!-- dev 本地开发测试环境 -->
<id>dev</id>
<properties>
<env>dev</env>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<!-- beta测试环境 -->
<id>beta</id>
<properties>
<env>beta</env>
</properties>
</profile>
<profile>
<!-- 发布环境 -->
<id>release</id>
<properties>
<env>release</env>
</properties>
</profile>
</profiles>
这是maven的功能,springboot中已经集成了maven的这项动态配置,所以引包时只需要
spring-boot-maven-plugin
。这个功能带来的好处是:1.配置文件位置灵活,在resources下可以另起一个包或者多个包。2.springboot总的配置文件中application.properties的变量值可以使用[@field_name@]占位符,其引用的是其他配置文件的属性内容。
2.如何操作?
执行maven编译命令,mvn package -Pdev,即可。-P后面紧跟环境的id。
详细的每个xml标签解释百度一下。