springboot动态化打包配置文件

最近打包的时候  在不同的环境需要频繁手动修改配置文件 于是查了下  感觉很多帖子的方法不是很满意  于是自己整理了下

 

第一步:在/src/main/resources/下新增配置文件,位置文件名随便定

springboot动态化打包配置文件

文件内容就是你要动态配置的那些东西  如下

springboot动态化打包配置文件

 

第二步:将配置文件中需要动态替换的改成 @[email protected],此处一定要注意  不要用${} 因为 springboot中默认的动态替换符号就是@

springboot动态化打包配置文件

 

第三步:在pom文件中新增如下配置

<profiles>
   <profile>
      <id>front</id>
      <properties>
         <!-- 需要读取的配置文件名 -->
         <env>talent_config_front</env>
         <!-- 自定义打包名  不需要去掉 -->
         <suffix></suffix>
      </properties>
      <activation>
         <!-- 默认情况下使用front开发配置 -->
         <activeByDefault>true</activeByDefault>
      </activation>
   </profile>

   <profile>
      <id>admin</id>
      <properties>
         <env>talent_config_admin</env>
         <suffix>_admin</suffix>
      </properties>
   </profile>
</profiles>

在build标签中新增如下

<filters>
   <!-- 读取配置文件 此处用${} env就是上面profile中配置的东西  -->
   <filter>../smartcity-talentApi-center/src/main/resources/config/${env}.properties</filter>
</filters>
<resources>
   <resource>
      <!-- 需要替换文件的位置  -->
      <directory>src/main/resources</directory>
      <!-- 确认替换 false不替换 -->
      <filtering>true</filtering>
   </resource>
</resources>
<!-- 指定包名 -->
<finalName>${project.artifactId}-${project.version}${suffix}</finalName>

 

第四步:打包命令

使用front :   mvn clean package

使用admin:mvn clean package -Padmin

-P后面的参数就是<profiles>中<id>的值