Maven profile实现不同环境的配置管理实践

前言

目前,企业项目的开发过程中,往往会使用配置文件来做一些配置项来实现项目部署的灵活性,避免硬编码的方式在环境变化时需要对代码进行重新编译。但是往往在项目周期中存在着各种环境:如开发环境、测试环境以及生产环境等,而且在不同的运行环境中可能牵扯到大量的配置项变化。如果在不同的部署环境中切换,对配置文件的管理往往容易让程序员感觉非常的混乱。
为了避免这种换乱,研发过程中也有比较多的手段进行。比如,有公司就采用VPN的虚拟网络环境,让测试环境、生产环境的网络一致,让程序员在不同环境中对版本进行发布时只需要对VPN进行切换即可。以免发生网络配置项改错,漏改等现象的发生。这样个人觉得还不错,唯一有一点句是调整网络环境、设备环境的成本应该也比较高。
当然profile的方式应该算是比较经济的。我知道的比如spring-boot、maven都可以支持到profile的方式来对不同环境进行指定。本文希望介绍一下,我理解的使用maven的profile方式来进行不同环境切换。讲得不到位的地方希望看官嘴下留情,也多指定。

Maven 的 profile:

在maven的 pom.xml 文件中有一个配置项叫着profiles节点,如下:

 <profiles>
  <profile>
   <id>test</id>
   <properties>
    <active.profile>test</active.profile>
    <jdbc.url>127.0.0.1</jdbc.url>
   </properties>
   <activation>
    <activeByDefault>false</activeByDefault>
   </activation>
  </profile>
  <profile>
   <id>develop</id>
   <properties>
    <active.profile>develop</active.profile>
    <jdbc.url>192.168.1.102</jdbc.url>
   </properties>
   <activation>
    <activeByDefault>true</activeByDefault>
   </activation>
  </profile>
  <profile>
   <id>product</id>
   <properties>
    <actived.profile>product</actived.profile>
    <jdbc.url>10.21.41.100</jdbc.url>
   </properties>
   <activation>
    <activeByDefault>false</activeByDefault>
   </activation>
  </profile>
 </profiles>

其中profiles节点下可以填写多个profile 其中profile主要包含了三个属性id、properties 、activation 。

id 应该是为了区分profile用的。
properties 就是对应的属性。
activation 应该是主要用来指定是否被默认激活的,它还有一个子节点activeByDefault, 如果子节点activeByDefault内的值为true表示他会被激活。它还有一些子节点,但是不知道什么用。后续看看在学习下。

实践前期准备

我准备建立一个简单的maven功能来实践一下maven的profile实现不同的配置管理,所以首先需要建议一个maven工程。本文采用的是idea进行试验的。一下是我建立工程的结构图。

由于我只是需要对配置文件进行管理,所以是完全不需要建立任何java类就可以的。

  • 首先建立了三个profile相关的配置文件 develop.properties 、 product.properties 、 test.properties
  • 在三个文件中我就只写了一个属性app.name,三个文件中分别为 develop.maven.profile、 product.maven.profile、 test.maven.profile.
  • 为了验证在各类配置文件中都是可行的, 我建立了两个供测试的配置文件application.properties、 application.xml.

application.properties 的内容为:

application.xml的内容为:

实践一:

实践一主要采用profile + filter的方式实现内容的注入。
该方式的思想是通过 filter下的文件编写可变动的配置项,由filters标签引入不同的配置文件项,然后提取不同的配置文件中的配置项填充到resources下的配置文件中。
所以其中的关键标签包含了 profile filter resource

Maven的配置文件pom.xml的内容如下

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <parent>
  <artifactId>maven-test</artifactId>
  <groupId>com.maomao.maven</groupId>
  <version>1.0-SNAPSHOT</version>
 </parent>
 <modelVersion>4.0.0</modelVersion>
 <artifactId>maven-profile</artifactId>
 <version>1.0-SNAPSHOT</version>
 <profiles>
  <profile>
   <id>test</id>
   <properties>
    <active.profile>test</active.profile>
   </properties>
   <activation>
    <activeByDefault>false</activeByDefault>
   </activation>
  </profile>
  <profile>
   <id>develop</id>
   <properties>
    <active.profile>develop</active.profile>
   </properties>
   <activation>
    <activeByDefault>true</activeByDefault>
   </activation>
  </profile>
  <profile>
   <id>product</id>
   <properties>
    <actived.profile>product</actived.profile>
   </properties>
   <activation>
    <activeByDefault>false</activeByDefault>
   </activation>
  </profile>
 </profiles>
 <build>
  <filters>
   <filter>src/filters/${active.profile}.properties</filter>
  </filters>
  <resources>
   <resource>
   <!--一定要让filtering为true 否则无法对内容进行注入-->
    <filtering>true</filtering>
    <directory>src/main/resources</directory>
    <includes>
     <include>**/*.properties</include>
     <include>**/*.xml</include>
    </includes>
   </resource>
  </resources>
 </build>
</project>

执行maven命令进行打包:

mvn clean install 

执行后打包结果targets文件夹下的配置文件application.properties、 application.xml的占位置被填充。


当然如果切换profiles下的激活项,填充的内容自然也会发生变化。或者采用maven命令进行激活项的切换:

mvn clean package -Ptest # 指定激活项的ID

实践二:

实践二主要采用profile 直接将属性配置到了profile下的properties节点下。此方法就不在需要多余的filter properties文件配置了。
例如:

 <profile>
   <id>product</id>
   <properties>
    <actived.profile>product</actived.profile>
    <jdbc.url>10.21.41.100</jdbc.url>
   </properties>
   <activation>
    <activeByDefault>false</activeByDefault>
   </activation>
  </profile>

这里properites中多了一个jdbc.url属性。那我们的application.properties文件中同样适用两个占位符

app.name=${app.name}
jdbc.url=${jdbc.url}

同样执行maven命令进行打包:

mvn clean install -Pproduct

打包之后的 application.properties内容对应变为

结束语

整个内容写的有点乱,但是意思大概就是这个意思。主要想说maven可以搞这样一个事情。也给自己留个备忘录。如果有人来看到这个希望轻喷,我很少写东西。最近准备练习写东西,待改进的地方还是很多的,所以见谅了。

到此这篇关于Maven profile实现不同环境的配置管理实践的文章就介绍到这了,更多相关Maven profile配置管理内容请搜索呐喊教程以前的文章或继续浏览下面的相关文章希望大家以后多多支持呐喊教程!

声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:notice#nhooo.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。