1、需求背景

需要对线上环境进行切换成pro进行打包

image.png

2、Java项目中配置

1、pom.xml中配置

1、添加profiles配置

  1. <!--分别设置开发,测试,生产环境-->
  2. <profiles>
  3. <!-- 开发环境 -->
  4. <profile>
  5. <id>dev</id>
  6. <activation>
  7. <activeByDefault>true</activeByDefault>
  8. </activation>
  9. <properties>
  10. <environment>dev</environment>
  11. </properties>
  12. </profile>
  13. <!-- 线上环境 -->
  14. <profile>
  15. <id>pro</id>
  16. <activation>
  17. <activeByDefault>false</activeByDefault>
  18. </activation>
  19. <properties>
  20. <environment>pro</environment>
  21. </properties>
  22. </profile>
  23. </profiles>

2、添加build resource配置

    <build>
        <resources> <!-- 资源文件的打包和过滤替换配置 -->
            <resource>
                <!-- 指定配置文件所在的resource目录 -->
                <directory>src/main/resources</directory>
                <includes>
                    <include>application.yaml</include>
                    <include>application-${environment}.yaml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>

3、application.yaml中配置

  • 对原来的 active:dev 修改为一个动态的值 ,environment就是上面profiles中配置的名称 但是需要加上@@来动态读取

    spring:
    profiles:
      active: @environment@ #在Idea的maven Profiles中勾选你想要的环境
    

    3、maven打包使用

    1、配置完就可以自动勾选了

  • 只要这边勾选了你想要的环境,本地直接启动就是具体环境了

image.png

4、重点是Jenkins中打包pro环境

1、在Jenkins,顶层maven调用中

  • package -P pro 指定是打包pro环境

    clean package -P pro -f seat-user/pom.xml
    
  • 如图

image.png