美文网首页SpringBootToolmaven
Maven 多环境打包以及聚合打包(一)

Maven 多环境打包以及聚合打包(一)

作者: 醉疯觞 | 来源:发表于2018-01-26 14:46 被阅读111次

    说明

    本文只是一个总结
    参考文章:
    Maven 插件 - 打包时多环境配置文件设置
    Maven 多环境打包
    maven 多环境打包发布的两种方式

    开发中碰到的问题1

    现在采用 spring boot 开发时,spring boot 开发的配置文件结构目录,如下图所示:

    配置结构

    这样有个好处是配置文件可以分开,公用的配置项可以放在 application.properties 文件里面,再通过 spring.profiles.active 配置项来激活 各个环境的配置项,在使用 spring cloud config 之后,可能还会多一个 bootstrap.properties , 这个文件无法支持多环境配置 , 所以每次在使用 maven 构建打包时,总会手动去改大量配置,这样打包时,可能忘记修改某个配置而导致最后发布的包出现问题。所以我们需要用到使用 maven 进行多环境打包。

    Maven 多环境打包

    1. 修改 pom.xml

    pom.xml 文件中 添加 <profiles></profiles> 标签,然后 在此标签中加入<profile></profile>标签进行多环境配置。

    <profiles>
            <!-- 测试环境 -->
            <profile>
                <id>test</id>
                <activation>
                    <activeByDefault>true</activeByDefault>
                </activation>
                <properties>
                    <spring.profiles.active>test</spring.profiles.active>
                </properties>
            </profile>
            <!-- 开发环境 -->
            <profile>
                <id>dev</id>
                <activation>
                    <activeByDefault>false</activeByDefault>
                </activation>
                <properties>
                    <spring.profiles.active>dev</spring.profiles.active>
                </properties>
            </profile>
            <!-- 生产环境 -->
            <profile>
                <id>pro</id>
                <activation>
                    <activeByDefault>false</activeByDefault>
                </activation>
                <properties>
                    <spring.profiles.active>pro</spring.profiles.active>
                </properties>
            </profile>
    </profiles>
    

    其中 <activeByDefault></activeByDefault> 标签为true 时表示默认配置,通过 eclipse 执行 run asmaven install 时,默认使用 activeByDefaulttrue 的配置。
    同时需要在 pom.xml 文件中的 <build></build> 标签中加入:

    <build>
            <resources>
                <resource><!-- 扫描替换 -->
                    <directory>${project.basedir}/src/main/resources</directory>
                    <filtering>true</filtering>
                </resource>
            </resources>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    

    用来指定打包时,去替换指定文件下的配置,这样 pom.xml 配置完成。

    修改 properties 文件

    修改 properties 文件就特别简单了,将在 pom.xmlprofileproperties 直接替换就行了。

    spring.profiles.active=@spring.profiles.active@
    
    打包

    此时如果执行 eclipse 中的 maven install 则会打包成默认的配置项。

    eclipse run as 面板

    但是通过 运行 maven build...

    maven build...

    Goals 中执行 clean install -P pro 则可打包其他环境。

    相关文章

      网友评论

      本文标题:Maven 多环境打包以及聚合打包(一)

      本文链接:https://www.haomeiwen.com/subject/qzxwaxtx.html