• maven 插件之 AutoConfig 工具使用笔记


    AutoConfig 是一款 maven 插件,主要用于 Maven 项目打包使用。在我们的工作中,会将自己写的代码打成 jar 包或者 war 包发布到各种环境上。一般地,不用的环境所使用的数据库、缓存的配置是不同的。我们完全可以手工修改不用环境的配置,当然这种做法是非常耗费精力的。好在, maven 为我们提供了 profile 机制,但我在工作中还觉得它不够好,因为它把我的数据库用户名和密码配置在 pom.xml 文件中,pom 文件又被 git 所管理,其实就暴露了数据库连接的配置。于是我找到了一款非常好用的 maven 插件,实现不同环境使用不同的配置进行打包,同时又不会将 pom.xml 文件纳入 git 仓库管理。

    以下所写的均是基于下面的这篇资料整理的一份操作笔记,供自己查阅和别人参考。

    第 13 章 AutoConfig工具使用指南 
    http://openwebx.org/docs/autoconfig.html

    操作步骤:

    1、将项目的打包方式设置为 war ; 
    2、建立打包方式为 war 的文件夹和文件:

    这里写图片描述

    3、编写 auto-config.xml 文件,示例代码如下:

    <?xml version="1.0" encoding="UTF-8"?>
    <config>
        <group>
            <property name="datasource.slave.host" defaultValue="127.0.0.1" description="datasource slave host" />
            <property name="datasource.slave.port" defaultValue="3306" description="datasource slave port" />
            <property name="datasource.slave.db" defaultValue="read" description="datasource slave db" />
            <property name="datasource.slave.username" defaultValue="root" description="datasource slave username" />
            <property name="datasource.slave.password" defaultValue="123456" description=" datasource slave password" />
            <property name="datasource.slave.maxconn" defaultValue="50" description="datasource slave maxconn" />
            <property name="datasource.slave.minconn" defaultValue="25" description="datasource slave minconn" />
        </group>
        <group>
            <property name="datasource.master.host" defaultValue="127.0.0.1" description="datasource master host" />
            <property name="datasource.master.port" defaultValue="3306" description="datasource master port" />
            <property name="datasource.master.db" defaultValue="read" description="datasource master db" />
            <property name="datasource.master.username" defaultValue="root" description="datasource master username" />
            <property name="datasource.master.password" defaultValue="123456" description="datasource master password" />
            <property name="datasource.master.maxconn" defaultValue="50" description="datasource master maxconn" />
            <property name="datasource.master.minconn" defaultValue="25" description="datasource master minconn" />
        </group>
        <script>
            <generate template="application.properties.vm" destfile="WEB-INF/classes/application.properties" />
        </script>
    </config>
    • 注意:上面的 script 子标签,将根据 application.properties.vm 模板文件的内容去生成打包好的文件中的 WEB-INF/classes/application.properties 文件。 

    下面我们编写 application.properties.vm 模板文件。

    4、编写 application.properties.vm 模板文件

    datasource.slave.host=${datasource.slave.host}
    datasource.slave.port=${datasource.slave.port}
    datasource.slave.db=${datasource.slave.db}
    datasource.slave.username=${datasource.slave.username}
    datasource.slave.password=${datasource.slave.password}
    datasource.slave.maxconn=${datasource.slave.maxconn}
    datasource.slave.minconn=${datasource.slave.minconn}
    
    datasource.master.host=${datasource.master.host}
    datasource.master.port=${datasource.master.port}
    datasource.master.db=${datasource.master.db}
    datasource.master.username=${datasource.master.username}
    datasource.master.password=${datasource.master.password}
    datasource.master.maxconn=${datasource.master.maxconn}
    datasource.master.minconn=${datasource.master.minconn}
    • 注意:这里的 ${datasource.slave.host} 应该和第 3 步的 auto-config.xml 文件中配置的 property 子节点 name 属性对应

    5、编写 pom.xml 文件引入 autoconfig 插件

    (1)将该插件的 autoconfig 目标绑定到 maven 生命周期的 package 阶段

    <plugin>
        <groupId>com.alibaba.citrus.tool</groupId>
        <artifactId>autoconfig-maven-plugin</artifactId>
        <version>1.2</version>
        <configuration>
            <userProperties>${autoconfig.path}/${autoconfig.file}</userProperties>
        </configuration>
        <executions>
            <execution>
                <phase>package</phase>
                <goals>
                    <goal>autoconfig</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
    • (2)为了使得配置更加灵活,我们配置属性 autoconfig.path 和 autoconfig.file
    <autoconfig.path>${user.home}/antx-config/${artifactId}/${devModel}</autoconfig.path>
    <autoconfig.file>antx.properties</autoconfig.file>

    其中,${user.home} 是 maven 定义的系统属性,这里代表操作系统的宿主目录。

    (3)下面定义 profile

    <profiles>
        <!-- 开发环境:本机 -->
        <profile>
            <id>dev</id>
            <properties>
                <devModel>dev</devModel>
                <autoconfig.file>antx.properties</autoconfig.file>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
    
        <!-- 内测环境:内网 192.168.12.250 -->
        <profile>
            <id>beta</id>
            <properties>
                <devModel>beta</devModel>
                <autoconfig.file>antx.properties</autoconfig.file>
            </properties>
        </profile>
    
        <!-- 集成测试环境:lyced -->
        <profile>
            <id>inte</id>
            <properties>
                <devModel>inte</devModel>
                <autoconfig.file>antx.properties</autoconfig.file>
            </properties>
        </profile>
    
        <!-- 生产环境:17english -->
        <profile>
            <id>pro</id>
            <properties>
                <devModel>pro</devModel>
                <autoconfig.file>antx.properties</autoconfig.file>
            </properties>
        </profile>
    </profiles>

    以上就基本完成了 autoconfig 的配置。我们执行 maven 的 package 目标,就可以看到 autoconfig 生效了。

    如果你使用 IntelliJ IDEA 作为开发工具,你可以使用下面的方式执行 maven 的 package 目标,带上 profile 。

    这里写图片描述

    注意事项: 
    1、文件夹要自己建立,autoconfig 插件不会帮你创建文件夹;

  • 相关阅读:
    MySQL
    关于多因子选股模型的常见问题
    T100 GR 报表常见知识点 (含套版制作)
    Oracle Round 函式 (四捨五入)
    Oracle的字串處理
    改變帳款性質別以利排序沖帳才不會有問題,把19->17,把12->17
    財務月結結帳相關檢核程式
    t100 常用公用變數
    一、自动驾驶行业概述(第一课,新认识)
    第一章 软件体系结构概论
  • 原文地址:https://www.cnblogs.com/ceshi2016/p/8135073.html
Copyright © 2020-2023  润新知