maven auto-config 多环境自动打包

一般项目会涉及到三个环境:开发环境,测试环境,线上环境,每个环境中涉及到的配置都会不一样,比如在开发过程中一般数据库会链接开发的数据库,一些功能性的验证都会线下搭建相应的环境进行开发调试,而测试过程中又会链接跟线上环境比较真实的另一套配置等等。

Autoconfig,插件的作用就是将上面两点结合起来,一方面可以根据功能,职责将应用进行合理的划分,另一方面,又可以根据不同的环境进行配置项的更换,使得同一套代码适合不同的应用场景。

首先添加auto-config依赖:

<dependency>
     <groupId>com.alibaba.citrus.tool</groupId>
     <artifactId>autoconfig-maven-plugin</artifactId>
    <version>1.2</version>
</dependency>

 

1、在maven父工程的pom.xml文件中添加不同打包环境
<profiles>
        <profile>
            <id>dev</id>
            <activation>
                <!-- 默认启用的是dev环境配置 -->
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <devModel>dev</devModel>
                <autoconfig.properties>env-dev.properties</autoconfig.properties>
            </properties>
        </profile>
        <profile>
            <id>test</id>
            <properties>
                <devModel>test</devModel>
                <autoconfig.properties>env-test.properties</autoconfig.properties>
            </properties>
        </profile>
        <profile>
            <id>pro</id>
            <properties>
                <devModel>pro</devModel>
                <autoconfig.properties>env-pro.properties</autoconfig.properties>
            </properties>
        </profile>
    </profiles>

 

 

2、在maven父工程中添加配置文件properties
# 工程环境
application.environment.name=dev
 
###################### jdbc config start ######################
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1/sibu_wesale_base?characterEncoding=utf8
jdbc.username=root
jdbc.password=root
#数据库用户名密码加密因子
jdbc.encrypt.key=$)(#@Si&^%.Bu+=!
#连接池初始化大小
jdbc.initialSize=5
3、在web工程的pom.xml文件中添加打包插件
<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.2</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <includeEmptyDirs>true</includeEmptyDirs>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>**/Test*.java</exclude>
                    </excludes>
                    <skip>false</skip>
                </configuration>
            </plugin>
<plugin>
                <groupId>com.alibaba.citrus.tool</groupId>
                <artifactId>autoconfig-maven-plugin</artifactId>
                <version>1.2</version>
                <configuration>
                    <charset>${project.build.sourceEncoding}</charset>
                    <dest>${project.artifact.file}</dest>
                    <userProperties>${basedir}/../environment/${autoconfig.properties}        </userProperties>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>autoconfig</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

 

 
4、在src/main/resources目录下面新建properties文件夹,用来放打包的配置文件
5、在WEB-INF的同级目录下面新建META-INF
在autoconf文件夹下放置auto-config.xml以及所有的模板文件
6、配置auto-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<config>
    <!-- <group>
        <property name="application.environment.name" description="工程项目"/>
    </group> -->
    <script>
        <generate template="app.properties.vm" destfile="WEB-INF/classes/properties/app.properties" charset="UTF-8"/>
        <generate template="jdbc.properties.vm" destfile="WEB-INF/classes/properties/jdbc.properties" charset="UTF-8"/>
    </script>
</config>
7、新建模板app.properties.vm
# 项目名称
application.environment.name=${application_environment_name}
注意:${application_environment_name}是evn-XXX.properties文件中application.environment.name=dev的值。点号用_代替
posted @ 2019-05-23 17:27  我的1024  阅读(998)  评论(0编辑  收藏  举报