美文网首页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 多环境打包以及聚合打包(一)

    说明 本文只是一个总结参考文章:Maven 插件 - 打包时多环境配置文件设置 Maven 多环境打包 mav...

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

    说明 这是上篇文章,Maven 多环境打包以及聚合打包(一)的后续 开发中碰到的问题 2 在实际开发中,我们经常将...

  • Maven 打包常用命令

    maven command 打包 打包跳过测试 打包指定环境 maven 打包 启动jar指定环境

  • maven项目的聚合、SpringBoot

    1、maven聚合项目 1.1 maven项目的打包方式 maven项目的打包方式:jar、war、pom。默认的...

  • MAVEN 多环境打包

    这里的profile和Spring里面的profile是一样的道理,首先对默认的文件进行打包,然后读取默认激活的p...

  • Maven多环境打包

    场景 在应用部署的时候,往往遇到需要发布到不同环境的情况,而每个环境的数据库信息、密钥信息等可能会存在差异。举个例...

  • maven 多环境打包

    目录结构 在pom.xml中添加如下profile的配置: 当加入这些配置后就能在右侧看到目录如下:image.p...

  • Maven Profile多环境打包

    在项目管理中,对于一个项目或者产品,我们经常会有开发,测试,预发布,生产等多套环境。为避免每次打包发布有过多的人为...

  • maven profile 打包多环境

    说明:spring + maven 的项目结构图: 前提:spring-mvc.xml 引入.properties...

  • maven多环境打包配置

    1.在resources 目录中创建不同的项目启动配置文件。 resources目录结构如图: 2.修改项目pom...

网友评论

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

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