Spring boot Maven 项目打包

使用Maven 实现多环境 test dev prod 打包

项目的结构

在下图中可用看出,我们打包时各个环境需要分开,采用 application-环境.yml 的方式命名

环境配置开始

首先我们需要在application.yml文件中配置 spring.profiles.active = @profileActive@这里的proofileAction 是我们下面要用到的变量

spring:
profiles:
active: @profileActive@

pom.xml

我们需要在pom中指定finalname的名称,当然对于打包来说名称并不影响。
实际上 不配置 resources节点也是可以的,在pom添加只是为了在打包中少几个配置文件。
当然你可以通过include来选择需要导入哪个文件。

 <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<!-- 打包后文件名称:项目名-环境-版本 -->
<finalName>${project.artifactId}-${profileActive}-${project.version}</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<!-- 开启过滤替换功能-->
<filtering>true</filtering>
<includes>
<!-- 项目打包完成的包中只包含当前环境文件 -->
<include>application.yml</include>
<include>application-${profileActive}.yml</include>
</includes>
</resource>
</resources>
</build> <!-- 多环境配置方案 -->
<profiles>
<profile>
<id>dev</id>
<properties>
<profileActive>dev</profileActive>
</properties>
<activation>
<!-- 默认情况下使用dev开发配置 如 打包时不包含 -p 参数-->
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<!-- 打包命令package -P test -->
<profile>
<id>test</id>
<properties>
<profileActive>test</profileActive>
</properties>
</profile>
<!-- 打包命令package -P prod -->
<profile>
<id>prod</id>
<properties>
<profileActive>prod</profileActive>
</properties>
</profile>
</profiles>

在pom中profiles节点才是分项目打包的关键,通过profile节点可以在打包时动态的去更新我们在application.xml中的配置值

在编译项目时,可以使用 -P 参数指定需要使用的 profile 的 id,比如下面命令将会使用 dev profile:

mvn clean package -P dev

如果想使用test,只需要改为以下即可

mvn clean package -Ptest

假如不指定 -P 参数的话,则会使用 activeByDefault=true 的一项(即 dev)。

原文:https://blog.csdn.net/qq_22327273/article/details/83548138
参考文档:

LuckyZhouStar Maven中profile和filtering实现多个环境下的属性过滤

Spring boot项目分环境Maven打包,动态配置文件,动态配置项目的更多相关文章

  1. [DEBUG] spring boot在eclipse中用maven打包成jar访问templates报500错误

    更新:打war包的话只要把html文件放在resources/templates下即可,根本不需要放外面. 配置application.yml和templates放外面这种做法,打war包确实不行. ...

  2. 部署项目问题(maven打包jar不对应,导致启动时一直找不到某个类)

    项目是springboot+maven  打包用maven的插件package 下面是打包后的目录结构  project-1.0 和project-1.0.tar.gz是一样的  区别就是一个是压缩包 ...

  3. 快速搭建Spring Boot + Apache Shiro 环境

    个人博客网:https://wushaopei.github.io/    (你想要这里多有) 一.Apache Shiro 介绍及概念 概念:Apache Shiro是一个强大且易用的Java安全框 ...

  4. 【转】Spring Boot干货系列:(二)配置文件解析

    转自:Spring Boot干货系列:(二)配置文件解析 前言 上一篇介绍了Spring Boot的入门,知道了Spring Boot使用"习惯优于配置"(项目中存在大量的配置,此 ...

  5. Spring Boot干货系列:(二)配置文件解析

    Spring Boot干货系列:(二)配置文件解析 2017-02-28 嘟嘟MD 嘟爷java超神学堂   前言 上一篇介绍了Spring Boot的入门,知道了Spring Boot使用“习惯优于 ...

  6. maven打包 tomcat运行pom配置 或 打成jar包

    maven打包 tomcat运行pom配置,同时还需要配置org.apache.tomcat.maven插件,这里省略. <groupId>com.company</groupId& ...

  7. Spring Boot 揭秘与实战(四) 配置文件篇 - 有哪些很棒的特性

    文章目录 1. 使用属性文件2. YAML文件 1.1. 自定义属性 1.2. 参数引用 1.3. 随机数属性 1.4. application-{profile}.properties参数加载 3. ...

  8. 【spring boot】12.spring boot对多种不同类型数据库,多数据源配置使用

    2天时间,终于把spring boot下配置连接多种不同类型数据库,配置多数据源实现! ======================================================== ...

  9. Spring Boot中的缓存支持(一)注解配置与EhCache使用

    Spring Boot中的缓存支持(一)注解配置与EhCache使用 随着时间的积累,应用的使用用户不断增加,数据规模也越来越大,往往数据库查询操作会成为影响用户使用体验的瓶颈,此时使用缓存往往是解决 ...

随机推荐

  1. Qt开发经验小技巧51-60

    在某些http post数据的时候,如果采用的是&字符串连接的数据发送,中文解析乱码的话,需要将中文进行URL转码. QString content = "测试中文"; Q ...

  2. Manytasking optimization MATP

    Manytasking Jmetal代码反向解析1_MATP测试函数集 觉得有用的话,欢迎一起讨论相互学习~Follow Me 这是我在写Manytask optimization时的笔记,代码地址可 ...

  3. [转]Oringin 2016 安装教程

    觉得有用的话,欢迎一起讨论相互学习~Follow Me 原文ll链接 http://www.downza.cn/soft/282296.html 打开setup.exe 一路Next和Yes,任意输入 ...

  4. numtodsinterval 函数用法

    numtodsinterval(<x>,<c>) ,x是一个数字,c是一个字符串,表明x的单位,这个函数把x转为interval day to second数据类型 常用的单位 ...

  5. django:删除表后怎么重新数据迁移生成表

    1.将对应app下的migrations文件夹下面的除了__init__.py文件外全部删除 2.delete from django_migrations where app='app_name' ...

  6. mysql8.0 caching_sha2_password的坑

    先来解释一下为什么会报这个错,在mysql8之前的版本使用的密码加密规则是mysql_native_password,但是在mysql8则是caching_sha2_password. 解决方案有很多 ...

  7. 直击JavaScript之面向对象

    有时候,我们使用电脑的时候,并不知道它的原理是什么,但是就是会使用他,这就是面向对象.同样的,在JavaScript中也可使用这种原理,接下来就随我一起探索一下关于JavaScript面向对象的内容吧 ...

  8. [转帖]OLTP、OLAP与HTAP

    OLTP.OLAP与HTAP https://blog.csdn.net/ZG_24/article/details/87854982   OLTP On-Line Transaction Proce ...

  9. Codeforces Round #604

    Beautiful Regional Contest 题意 题解 代码 Beautiful Sequence 题意 题解 代码 一个思路不够清晰的代码 Beautiful Mirrors with q ...

  10. Appium移动端测试--基础预热

    目录 Android自动化环境准备 需要安装的软件: Appium多端架构与自动化 Android自动化前提依赖: 获取App的信息: Android常用命令 adb shell 常用命令列表: An ...