maven 参数化构建时 修改配置文件的值

maven构建出现版本号

mvn clean package -Dmaven.test.skip=true -P prod -DprofileServerVersion=$tag_version
参数讲解

跳过测试,这里的跳过测试,通常开发中junit单元测试
-Dmaven.test.skip=true
选择maven启动的类型,生产还是开发环境
-P prod
选择maven启动的版本号,这里的tag_version是jenkins参数化构建自定义的一个参数
-DprofileServerVersion=$tag_version
在这里插入图片描述
-DprofileServerVersion=$tag_version
这里的意思是在构建过程中,可以修改项目的配置文件。我下面的示例将会修改yml文件,同理修改properties文件与修改yml中是一致的。

pom的build中添加扫描路径

在pom的build中,添加需要修改文件的路径。<include>application.yml</include> 这里写死了application.yml文件路径,当然你也可以写成相对路径。

<build>
 <resources>
  <resource>
      <directory>src/main/resources</directory>
      <includes>
          <include>**/*</include>
      </includes>
  </resource>
  <resource>
      <directory>src/main/resources</directory>
      <includes>
          <include>application.yml</include>
      </includes>
      <filtering>true</filtering>
  </resource>
</resources>
</build>
在pom的profile接收构建参数

pom文件中,添加profile,这样在命令构建的过程中就可以指定参数。

<profiles>
 <profile>
     <id>prod</id>
     <properties>
         <profileActive>prod</profileActive>
         <!--这里的profileServerVersion就是,
         maven构建的-DprofileServerVersion参数,
         前面的-D后面的内容 -->
         <profileServerVersion>release_1.0.0</profileServerVersion>
     </properties>
 </profile>
</profiles>
yml文件中添加接收参数的参数和占位符

在application.yml添加此内容即可 server.version: @profileServerVersion@ 。值得注意 @profileServerVersion@ 就是接收 -DprofileServerVersion=$tag_version 的地方。

总结:

三步出发
第一步在pom的build中添加扫描路径,
第二步在pom的profile接收构建参数,
第三部在yml文件中添加接收参数的参数和占位符。

为了便于您的查看,该博客的源码地址在:https://gitee.com/cnhellorui/some_source_code/tree/master/serviceApiVersion

如果存在错误和建议,请给我发邮件或者留言 chenrui@marsdl.com

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值