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

如果存在错误和建议,请给我发邮件或者留言 [email protected]

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章