开发的流程是本地>测试>预发布>正式,所以不同的环境,肯定是不同的配置文件,所以我们需要针对不同的环境做不同的配置切换.

下面我们来说说 springboot 是怎么来切换的:

1、package 方式使用 war,应用部署到 tomcat

先来看一下文件结构,

可以看到,这里我们有 3 个 properties 的配置文件,还有 1 个 application.yml文件,这个文件里有一个参数 spring.profiles.active 的选项,这个用来激活不同环境的配置.

然后重点是看下面 pom.xml 的配置,如下:

<profiles>
<profile>
<id>dev</id>
<properties>
<profileActive>dev</profileActive>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>test</id>
<properties>
<profileActive>test</profileActive>
</properties>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
</profile>
<profile>
<id>pro</id>
<properties>
<profileActive>pro</profileActive>
</properties>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
</profile>
</profiles> <build>
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>application.yml</exclude>
<exclude>application.properties</exclude>
<exclude>application-dev.properties</exclude>
<exclude>application-test.properties</exclude>
<exclude>application-pro.properties</exclude>
</excludes>
</resource> <resource>
<directory>src/main/resources</directory>
<includes>
<include>application-${profileActive}.properties</include>
<include>application.yml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<delimiters>@</delimiters>
<encoding>utf-8</encoding>
<useDefaultDelimiters>false</useDefaultDelimiters>
</configuration>
</plugin>
</plugins>

然后当你在构建项目的时候可以这样来写命令 mvn clean package -Dmaven.test.skip=true -P dev/test/pro(根据不同的环境来切换不同的变量值)

2、package 方式是 jar,服务器上使用 java -jar 命令来启动

pom.xml 里的 profiles 配置可以完全删除,还有 build 里这段配置也可以删除.

然后在项目启动的时候通过指定spring.profiles.active 参数来激活配置

nohup java -jar *.jar -dprocesName=templateDecoration --spring.profiles.active=test > /xxx/xxx/xxx.log &

springboot 不同环境切换不同的配置文件的更多相关文章

  1. SpringBoot配置文件-多环境切换

    profile是Spring对不同环境提供不同配置功能的支持,可以通过激活不同的环境版本,实现快速切换环境: 多个文件-配置多环境: 需要多个配置文件,文件名可以是 application-{prof ...

  2. SpringBoot 多环境配置文件切换

    背景 很多时候,我们项目在开发环境和生成环境的环境配置是不一样的,例如,数据库配置,在开发的时候,我们一般用测试数据库,而在生产环境的时候,我们是用正式的数据,这时候,我们可以利用profile在不同 ...

  3. Java开发学习(三十七)----SpringBoot多环境配置及配置文件分类

    一.多环境配置 在工作中,对于开发环境.测试环境.生产环境的配置肯定都不相同,比如我们开发阶段会在自己的电脑上安装 mysql ,连接自己电脑上的 mysql 即可,但是项目开发完毕后要上线就需要该配 ...

  4. spring-boot多环境配置文件

    spring-boot多环境配置文件 目录 配置 多环境配置文件名称要遵循格式 application-{profile}.yml application.yml spring: profiles: ...

  5. 微服务-springboot多环境配置(开发生产测试环境切换)

    springboot根据spring.profiles.active会去寻找应该加载开发环境配置还是生产环境配置 application.properties #生产环境,开发环境,测试环境切换 pr ...

  6. SpringBoot学习笔记(八):SpringBoot启动端口+访问路、SpringBoot配置文件yml、SpringBoot多环境区分、SpringBoot打包发布

    SpringBoot启动端口+访问路径 配置文件: server.port=9090 server.context-path=/springboot 现在只能用http://127.0.0.1:909 ...

  7. 「快学springboot」SpringBoot多环境配置文件

    前言 我们都知道springboot的配置卸载application.properties配置文件上(或者application.yml).但是,如果想要把不同的环境(如开发环境,测试环境,生产环境) ...

  8. spring boot--日志、开发和生产环境切换、自定义配置(环境变量)

    Spring Boot日志常用配置: # 日志输出的地址:Spring Boot默认并没有进行文件输出,只在控制台中进行了打印 logging.file=/home/zhou # 日志级别 debug ...

  9. SpringBoot-多环境切换相关(六)

    多环境切换 profile是Spring对不同环境提供不同配置功能的支持,可以通过激活不同的环境版本,实现快速切换环境: 方式一:多配置文件 我们在主配置文件编写的时候,文件名可以是 applicat ...

随机推荐

  1. PRD是什么

    产品需求文档(Product Requirement Document,PRD)的英文简称.是将商业需求文档(BRD)和市场需求文档(MRD)用更加专业的语言进行描述

  2. mybatis中foreach使用

    mybatis中的<foreach collection="list" item="item" index="index" open= ...

  3. [程序员代码面试指南]数组和矩阵问题-找到无序数组中最小的k个数(堆排序)

    题目链接 https://www.nowcoder.com/practice/6a296eb82cf844ca8539b57c23e6e9bf?tpId=13&tqId=11182&t ...

  4. css与dom的渲染与解析

    js阻塞文档渲染与解析那么css呢? 结论一.css:阻塞渲染,不阻塞dom解析 <head> <script> document.addEventListener('DOMC ...

  5. 十七、Java中数组常见的几种排序方法!

    转载自:https://www.cnblogs.com/bekeyuan123/p/6891875.html 数组的定义: // 3种定义方式 int[] arr = new int[5]; int[ ...

  6. yum 安装时遇到“UnicodeDecodeError: 'ascii' codec”的问题

    今天新安装了一个6.9系统,配置好本地yum源后,用yum安装时报了以上的错误信息,在/etc/yum.repos.d/目录下多出了TTT的一个目录 (手动问号),在百度上查了一些文档. 解决方法:1 ...

  7. MPLAB IDE 细节点问题不定期更新ing

    问题1.如何找到MPLAB IDE 隐藏的项目.输出的窗口 答:在菜单栏的 视图 中 “Project”.“Output”. 问题2.mplab  c文件为什么不能添加到工程中的source file ...

  8. 创建ResultUtils类

    1.在pom.xml中增加maven资源 <dependency> <groupId>com.alibaba</groupId> <artifactId> ...

  9. Rsync未授权访问漏洞的修复

    转自:https://www.cnblogs.com/KevinGeorge/p/9648028.html 配置文件/etc/rsync.conf解析: motd file -> motd文件位 ...

  10. odoo8资料

    官网: https://www.odoo.com/documentation/8.0/ 官方文档: http://odoo-master.readthedocs.io/en/8.0/howtos/we ...