Maven Profiles 打包动态修改配置文件

Maven Profiles 打包动态修改配置文件

需求

maven 项目中的配置文件例如:src/main/java/resource/jdbc.properties

他看起来是这样的;


jdbc.test13.username=LIFELINEDEV
jdbc.test13.password=LIFELINEDEV
jdbc.test15.username=SCDPLATFORM
jdbc.test15.password=SCDPLATFORM
  • 我要在项目打测试环境包的时候动态的修改jdbc.test13.username=Hellotest
  • 我要在项目打包生产的时候动态的修改jdbc.test13.username=helloprod

解决方案

使用maven profile +com.juvenxu.portable-config-maven-plugin插件实现

  • 第一步定义好 profile
<!--定义变量profiles 支持打包自动切换-->
<profiles>
    <profile>
        <!--全局id 使用mvn clean package -P dev指定使用此profile-->
        <id>test</id>
        <activation>
            <!--默认开启使用此profile-->
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <!--定义环境变量 可以使用${package.environment}引用该定义的值-->
            <package.environment>test</package.environment>
        </properties>
    </profile>
    <profile>
        <!--全局id 使用mvn clean package -P prod指定使用此profile-->
        <id>prod</id>
        <properties>
            <!--定义环境变量 可以使用${package.environment}引用该定义的值-->
            <package.environment>prod</package.environment>
        </properties>
    </profile>
</profiles>
  • 第二步引入插件并进行配置
<plugins>
    <!--动态替换文件打包进行差异化环境配置-->
    <plugin>
        <groupId>com.juvenxu.portable-config-maven-plugin</groupId>
        <artifactId>portable-config-maven-plugin</artifactId>
        <version>1.1.5</version>
        <executions>
            <execution>
                <goals>
                    <goal>replace-package</goal>
                </goals>
            </execution>
        </executions>
        <!--执行的portableConfig位置-->
        <configuration>
		<portableConfig>
    	src/main/resources/portableConfig/${package.environment}.xml</portableConfig>
        </configuration>
    </plugin>
</plugins>
  • 第三步编写插件需要的portableConfig配置文件

    我在src/main/resources目录下新建portableConfig文件夹

    根据需求新建test.xml

    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    
    <portable-config>
        <!--需要修改并进行替换的文件路径-->
        <config-file path="WEB-INF/classes/jdbc.properties">
            <!--需要替换的properties Key-->
            <replace key="jdbc.test13.username">Hello test</replace>
        </config-file>
        <config-file path="WEB-INF/classes/spring-config-bean.xml">
            <!--支持Xml文件的替换 需要写Xpath语法进行定位-->
            <replace xpath="//bean[@id='kafkaData']/property[@name='groupId']/@vaule"> helloword</replace>
        </config-file>
    </portable-config>
    

    根据需求新建pord.xml

    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    
    <portable-config>
    <!--需要修改并进行替换的文件路径-->
    <config-file path="WEB-INF/classes/jdbc.properties">
        <!--需要替换的properties Key-->
        <replace key="jdbc.test13.username">Hello prod</replace>
    </config-file>
    <config-file path="WEB-INF/classes/spring-config-bean.xml">
        <!--支持Xml文件的替换 需要写Xpath语法进行定位-->
        <replace xpath="//bean[@id='kafkaData']/property[@name='groupId']/@vaule"> helloword</replace>
    </config-file>
    </portable-config>
    
  • 第四步 测试

    进入项目根目录

    执行 mvn clean package -P test

    注意观察命令行

    [INFO] --- portable-config-maven-plugin:1.1.5:replace-package (default) @ Bridge_test ---
    [INFO] Replacing: F:\WORKDEV\Bridge_test\target\Bridge
    [INFO] Replacing file:
    F:\WORKDEV\Bridge_test\target\Bridge\WEB-INF\classes\jdbc.properties
    

    检查F:\WORKDEV\Bridge_test\target\Bridge\WEB-INF\classes\jdbc.properties中的值是否是我们期望的值;

    恭喜你已经完成动态打包修改参数 再也不用为了打不同环境的包而发愁了!

maven Profiles 知识

(1) 针对于特定项目的profile配置我们可以定义在该项目的pom.xml中。

​ 你可以配置以下标签

  • <repositories>

  • <pluginRepositories>

  • <dependencies>

  • <plugins>

  • <properties>

  • <modules>

  • <reporting>

  • <dependencyManagement>

  • <distributionManagement>

    `

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

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

​ 当Profiles 写在Settting.xml文件中,此时的Setting如果是${MAVEN_HOME}\conf\settings.xml中 例如下

​ 您只能修改<repositories><pluginRepositories>部分以及额外的<properties>部分

<?xml version="1.0" encoding="UTF-8"?>

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">

  <pluginGroups>

  </pluginGroups>

  <proxies>

  </proxies>


  <servers>

  </servers>


  <mirrors>

	 <mirror>
     <id>alimaven</id>
     <name>aliyun maven</name>
     <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
     <mirrorOf>central</mirrorOf>
    </mirror>
  </mirrors>

 
  <profiles>
    <profile>
      <id>appserverConfig</id>
      <properties>
        <appserver.home>dev</appserver.home>
      </properties>
    </profile>
  </profiles>
 
  <activeProfiles>
    <activeProfile>appserverConfig</activeProfile>
  </activeProfiles>

  
</settings>

那么此时在项目的Pom.xml文件中是可以使用${appserver.home} 引用到该值的

如果定义在特定用户的profile配置中 经过测试 项目的Pom.xml文件 不能应用定义的值!!

官网文档链接

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值