用到maven进行打包
1.在pom文件加入build和 profiles配置
<build> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> <includes> <include>application.yml</include> <include>application-${environment}.yml</include> </includes> </resource> </resources> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <mainClass>com.ly.mk.BlindApplication</mainClass><!--项目的启动类 --> </configuration> <executions> <execution> <goals> <goal>repackage</goal><!--可以把依赖的包都打包到生成的Jar包中--> </goals> </execution> </executions> </plugin> </plugins> </build> <!--与build标签同级别,放在project标签中--> <profiles> <profile> <id>dev</id> <properties> <environment>dev</environment> </properties> <activation> <!-- 默认环境 --> <activeByDefault>true</activeByDefault> </activation> </profile> <profile> <id>prod</id> <properties> <environment>prod</environment> </properties> </profile> </profiles>
2.spring的yml配置
active配置变量名称environment
3.选择要打包的环境package
这样就可以根据你选择的环境打包不同的yml文件了
打包完成,需要手动maven reload项目才能正常启动
注意:本地切换启动环境只要在相应环境标签下加
<activation> <activeByDefault>true</activeByDefault> </activation>就可以