一
这是很简单的事,我都不想写
但是用了Maven Profile之后,我打包就不用每次都改application.yml文件了
所以特意记录一下
二
首先你的Spring Profile要有多环境配置文件
在pom.xml添加
#识别多环境配置 <profiles> <profile> <id>dev</id> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <profile.active>dev</profile.active> </properties> </profile> .....test就不写了 <profile> <id>prod</id> <properties> <profile.active>prod</profile.active> </properties> </profile> </profiles>
此时打包就是这样了
三 追求完美
在进行打包的时候,我们并不需要把dev或者test等配置文件打包进行,所有我们在打包的时候进行如下配置
所以在pom.xml添加
<build> <resources> <resource> <directory>src/main/resources</directory> <!--先排除所有的配置文件--> <excludes> <exclude>application*.yml</exclude> </excludes> </resource> <resource> <directory>src/main/resources</directory> <!--引入所需环境的配置文件--> <filtering>true</filtering> <includes> <include>application.yml</include> <include>application-${profile.active}.yml</include> </includes> </resource> </resources> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <classifier>exec</classifier> </configuration> </plugin> </plugins> </build>
修改application.yml
spring:
profiles:
active: @profile.active@ #此处由maven的环境选择决定