暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

maven实现多环境配置

菜涛学Java 2019-01-20
1218

      

        开发一个项目,主要有开发,测试和最终部署上线几个阶段,每个阶段对配置(数据库,日志)都有不同的设置。如何能实现在生成不同的发布包时对资源进行不同的替换?maven已经提供方案了,通过在build节点中添加maven的resources、filter和profile实现不同环境使用不同配置文件

一、介绍下resources、filter和profile

1、profiles定义了各个环境的变量id

2 、filters中定义了变量配置文件的地址,其中地址中的环境变量就是上面profile中定义的值

3、resources中是定义哪些目录下的文件会被配置文件中定义的变量替换,一般把项目的配置文件放在src/main/resources下,像db,bean等,里面用到的变量在打包时就会根据filter中的变量配置替换成固定值

二、原理:

1、利用filter实现对资源文件(resouces)过滤

    maven filter可利用指定的xxx.properties中对应的key=value对资源文件中的{key}进行替换,最终把你的资源文件中的username={key}替换成username=value

2、利用profile来切换环境

    maven profile可使用操作系统信息,jdk信息,文件是否存在,属性值等作为依据,来激活相应的profile,也可在编译阶段,通过mvn命令加参数 -PprofileId 来手工激活使用对应的profile,结合filter和profile,可以在不同环境下使用不同的配制

三、实战

项目资源目录如下:

pom.xml文件

  1. <build>

  2.        <finalName>shiro</finalName>

  3.        <filters>

  4.            <filter>src/main/resources/profiles/${profiles.active}.properties</filter>

  5.        </filters>

  6.        <resources>

  7.            <resource>

  8.                <directory>src/main/resources</directory>

  9.                <filtering>true</filtering>

  10.            </resource>


  11.        </resources>

  12.        <plugins>

  13.            <plugin>

  14.                <groupId>org.apache.maven.plugins</groupId>

  15.                <artifactId>maven-compiler-plugin</artifactId>

  16.                <version>2.3.2</version>

  17.                <configuration>

  18.                    <source>${jdk.version}</source>

  19.                    <target>${jdk.version}</target>

  20.                    <encoding>${project.build.sourceEncoding}</encoding>

  21.                </configuration>

  22.            </plugin>

  23.            <!-- tomcat7:run -->

  24.            <plugin>

  25.                <groupId>org.apache.tomcat.maven</groupId>

  26.                <artifactId>tomcat7-maven-plugin</artifactId>

  27.                <version>2.2</version>

  28.                <configuration>

  29.                    <path>/</path>

  30.                    <uriEncoding>UTF-8</uriEncoding>

  31.                    <server>tomcat7</server>

  32.                </configuration>

  33.            </plugin>

  34.        </plugins>

  35.    </build>


  36.    <profiles>

  37.        <!-- 默认激活 dev 开发环境 -->

  38.        <!-- 线上使用 mvn 打包添加 -Pproduction 变量 -->

  39.        <profile>

  40.            <!-- 开发环境 -->

  41.            <id>dev</id>

  42.            <properties>

  43.                <profiles.active>dev</profiles.active>

  44.            </properties>

  45.               <activation>

  46.                <!-- 设置默认激活该配置 -->

  47.                <activeByDefault>true</activeByDefault>

  48.            </activation>

  49.        </profile>


  50.        <profile>

  51.            <!-- 测试环境  -->

  52.            <id>test</id>

  53.            <properties>

  54.                <profiles.active>test</profiles.active>

  55.            </properties>

  56.        </profile>


  57.        <profile>

  58.            <!-- 生产环境 production -->

  59.            <id>production</id>

  60.            <properties>

  61.                <profiles.active>production</profiles.active>

  62.            </properties>

  63.        </profile>

  64.    </profiles>

src/main/resources/profiles/dev.properties

  1. app.name=shiro

  2. profile.env=dev


  3. #--------- database -----------

  4. db.master.url=jdbc\:mysql\://127.0.0.1\:3306/shiro?useUnicode\=true&characterEncoding\=utf-8&zeroDateTimeBehavior\=convertToNull&transformedBitIsBoolean\=true&useSSL\=false

  5. db.master.user=root

  6. db.master.password=root

src/main/resources/jdbc.properties

  1. db.master.url=${db.master.url}

  2. db.master.user=${db.master.user}

  3. db.master.password=${db.master.password}

maven命令:

  1. mvn package -Pproduction

  2. mvn package -Ptest  

  3. mvn package -Pdev

运行打包命令maven会将src/main/resources/profiles/${profiles.active}.properties对应文件里的key的值替换到src/main/resources下所有文件

四、结果


你点的每个赞,我都认真当成了喜欢




文章转载自菜涛学Java,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论