0
点赞
收藏
分享

微信扫一扫

Maven 多环境指定 Profile 环境编译打包 & Spring Boot 动态选择配置文件

Maven 多环境指定 Profile 环境编译打包

问题描述:

通过mvn –P参数指定 profile,只对当前指定的生效。

实例:

<profiles>
<profile>
<id>lazada</id>
<dependencies>
...
</dependencies>
</profile>
<profile>
<id>ae</id>
<dependencies>
...
</dependencies>
</profile>
</profiles>

指定 lazada 这个Profile 进行编译、打包:

编译:

mvn clean install -Plazada

打包:

mvn clean install  -Plazada

Spring Boot 动态选择配置文件

一、背景

在开发过程中,我们的软件会面对不同的运行环境,比如开发环境、测试环境、生产环境,而我们的软件在不同的环境中,有的配置可能会不一样,比如数据源配置、日志文件配置、以及一些软件运行过程中的基本配置,那每次我们将软件部署到不同的环境时,都需要修改相应的配置文件,这样来回修改,很容易出错,而且浪费劳动力。

maven提供了一种方便的解决这种问题的方案,就是profile功能。

二、profile简介

profile可以让我们定义一系列的配置信息,然后指定其激活条件。这样我们就可以定义多个profile,然后每个profile对应不同的激活条件和配置信息,从而达到不同环境使用不同配置信息的效果。

profile定义的位置

(1) 针对于特定项目的profile配置我们可以定义在该项目的pom.xml中。(下面举例是这种方式)

(2) 针对于特定用户的profile配置,我们可以在用户的settings.xml文件中定义profile。该文件在用户家目录下的“.m2”目录下。

(3) 全局的profile配置。全局的profile是定义在Maven安装目录下的“conf/settings.xml”文件中的。

Spring Boot Profile

Spring Boot Profile 有许多的功能,这里只说管理配置的内容。Spring 加载配置的顺序如下:

  1. Jar 包外的​​application-{profile}.properties​
  2. Jar 包内的​​application-{profile}.properties​
  3. Jar 包外的​​application.properties​
  4. Jar 包内的​​application.properties​

例如,如果我们在 ​​application.properties​​ 中指定

spring.profiles.active = dev

则 spring 会使用 ​​application-dev.properties​​​ 文件中的配置来覆盖 ​​application.properties​​ 文件中的相应配置。


举报

相关推荐

0 条评论