几种Maven自动替换配置插件


话不多说,直接进入正题,简单介绍几种常用的Maven打包时配置文件替换的插件:

1.portable-config-maven-plugin

这个使用起来特别简单,首先添加Maven依赖(在<plugins>...</plugins>中添加):

<!-- portable config -->
<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>
    <configuration>
        <portableConfig>${portableConfig}</portableConfig>
    </configuration>
</plugin>

然后在<profiles>...<profils>中添加配置:

<profiles>
    <!-- 测试配置文件所在路径 -->
    <profile>
        <id>test</id>
        <properties>
            <portableConfig>src/main/portable/test.xml</portableConfig>
        </properties>
    </profile>
    
    <!--生产配置文件所在路径 -->
    <profile>
        <id>production</id>
        <properties>
            <portableConfig>src/main/portable/production.xml</portableConfig>
        </properties>
    </profile>
</profiles>

至此pom.xml文件中的配置都已经完成了。

下面我们看看,production.xml中的配置文件格式:

<?xml version="1.0" encoding="utf-8" ?>
<!-- 测试环境配置。节点特定配置通过JVM参数配置,如-Dserver.node_name=test-app0 -->
<portable-config>
    <config-file path="WEB-INF/classes/application.properties">
        <replace key="jdbc.driver">数据库驱动类</replace>
        <replace key="jdbc.url">数据库链接的url</replace>
        <replace key="jdbc.username">数据库用户名</replace>
        <replace key="jdbc.password">用户密码</replace>
        <replace key="jdbc.pool.maxIdle">10</replace>
        <replace key="jdbc.pool.maxActive">150</replace>
        <replace key="profile">test</replace>
    </config-file>

    <!--替换日志级别 logback.xml-->
    <config-file path="WEB-INF/classes/logback.xml">
        <replace xpath="//appender[@class='ch.qos.logback.core.ConsoleAppender']/filter[@class='ch.qos.logback.classic.filter.ThresholdFilter']/level">OFF</replace>
        <replace xpath="//appender[@class='ch.qos.logback.core.rolling.RollingFileAppender']/filter[@class='ch.qos.logback.classic.filter.ThresholdFilter']/level">WARN</replace>
        <replace xpath="//logger[@name='com.june.life']/@level">WARN</replace>
        <replace xpath="//logger[@name='org.apache.shiro.authc.pam.ModularRealmAuthenticator']/@level">WARN</replace>
        <replace xpath="//logger[@name='com.june.life.shiro.session']/@level">WARN</replace>
        <replace xpath="//logger[@name='com.june.life.shiro.cache']/@level">WARN</replace>
        <replace xpath="//logger[@name='org.springframework.web']/@level">WARN</replace>
        <replace xpath="//root/@level">WARN</replace>
    </config-file>

</portable-config>

具体xpath的语法参考:http://www.aichengxu.com/view/598615

上面这些配置完成后,就可以直接使用maven命令进行打包了。如我想在测试环境下打一个war包,就可以使用下面的命令:
maven clean package -Ptest -DskipTests

2.autoconfig-maven-plugin

这是alibaba的一个打包插件,这个相对来说配置复杂一些,先看看maven中的配置:
同样在<plugs>...</plugs>中添加一个maven插件:

<!-- Maven 打包自动替换配置-->
<plugin>
    <groupId>com.alibaba.citrus.tool</groupId>
    <artifactId>autoconfig-maven-plugin</artifactId>
    <version>1.2</version>
    <configuration>
        <userProperties>${env.properties}</userProperties>
    </configuration>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>autoconfig</goal>
            </goals>
        </execution>
    </executions>
</plugin>

同样在<profiles></profiles>中添加:

<profiles>
    <profile>
        <id>dev</id>
        <properties>
            <env.properties>src/main/conf/dev.properties</env.properties>
        </properties>
    </profile>

    <profile>
        <id>test</id>
        <properties>
            <env.properties>src/main/conf/test.properties</env.properties>
        </properties>
    </profile>
</profiles>

这是时候的dev.properties和test.properties的格式和application.properties的格式完全相同,只是参数值不同
至此,pom.xml中的配置就完成了

autoconfig这个插件打包时会自动找META_INF/autoconfig/auto-config.xml文件,下面贴出auto-config中的内容:

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <group name="env">
        <property name="log.path"  defaultValue="../logs" description="日志存放目录" />
        <property name="db.url"    />
        <property name="db.username"    />
        <property name="db.password"    />
        <property name="db.driver"    />
        <property name="db.initialPoolSize"    />
        <property name="db.minPoolSize"    />
        <property name="db.maxPoolSize"    />
    </group>


    <script>
        <generate template="application.properties.vm" destfile="WEB-INF/classes/application.properties" charset="UTF-8"  />
    </script>
</config>

这个文件会替换模板文件指定的参数值,下面是模板文件application.properties.vm

db.url=${db.url}
db.username=${db.username}
db.password=${db.password}
db.driver=${db.driver}
db.initialPoolSize=${db.initialPoolSize}
db.minPoolSize=${db.minPoolSize}
db.maxPoolSize=${db.maxPoolSize}

至此所有替换配置都已经完成,打包时直接使用下面的maven命令就可以:
mvn clean package -Pdev -DskipTests

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,087评论 19 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,989评论 6 342
  • 所有项目的构建都是有生命周期的,这个生命周期包括:项目清理、初始化、编译、测试、打包、集成测试、验证、部署、站点生...
    zlcook阅读 2,856评论 0 21
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,952评论 25 709
  • 第四章 我多希望一切都是我做的梦,醒来之后你还在 那天离别后,两人互相留了手机号、扣扣号、微信号,到房间之后,周语...
    小小京同学阅读 512评论 0 6