java工程使用maven进行差异化环境配置

项目分为测试环境、预发环境、生产环境等等不同环境,每个环境有每个环境的配置,下面使用maven进行差异化环境配置。

1.项目目录结构为

           

其中application.properties指定公共配置和引入哪个环境的配置,在其他环境配置差异化的配置,其中 配置文件里的 ${env-flag} 是打包时替换的内容,名字随便写,但要与pom里的配置对应上,后续会介绍

配置文件的内容就配置完成,下面在pom文件里配置相关内容

1.在build标签下添加如下节点,这段配置是最常规的配置,resource的filtering属性用来表示资源文件中的占位符是否需要被替换,true为需要替换。则整段配置的大致意思是在 src/main/resources的目录下,所有.properties文件中的EL表达式占位符都会在打包时动态替换,所有的.xml文件则不会在打包时动态替换占位符。

<resources>
    <resource>
        <directory>src/main/resources</directory>
        <filtering>true</filtering>
        <includes>
            <include>**/*.properties</include>
        </includes>
    </resource>
        <resource>
        <directory>src/main/resources</directory>
        <filtering>false</filtering>
        <includes>
            <include>**/*.xml</include>
        </includes>
        </resource>
 </resources>

2.插件配置,在plugins标签下添加以下插件配置

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    <version>3.1.0</version>
    <!--使用默认的变量分割符即${}-->
    <configuration>
        <useDefaultDelimiters>true</useDefaultDelimiters>
    </configuration>
</plugin>

3. 环境配置,在project标签下添加以下配置  ,这里指定了三个环境  test、 beta 、 production 。其中 <env-flag>标签对应上面配置文件中${env-flag}的内容,标签体的内容会替换它

<profiles>
    <profile>
        <id>test</id>
        <properties>
            <!-- 这里的标签env-flag对应的就是 properties配置文件里 ${env-flag} ,标签内容会替换配置文件里的内容,达到动态指定的目的 -->
            <env-flag>test</env-flag>
            <!-- 这里指定jvm启动参数 -->
            <jvmOpts>-server -Xms1024m -Xmx1024m</jvmOpts>
        </properties>
    </profile>
    <profile>
        <id>beta</id>
        <properties>
            <env-flag>beta</env-flag>
            <jvmOpts>-server -Xms1024m -Xmx1024m</jvmOpts>
        </properties>
     </profile>
     <profile>
         <id>production</id>
         <properties>
            <env-flag>production</env-flag>
            <jvmOpts>-server -Xms1024m -Xmx1024m -XX:MaxPermSize=512m -XX:+UnlockExperimentalVMOptions -Xmn512m -XX:PermSize=512m -XX:+UseConcMarkSweepGC -XX:CMSInitiatingOccupancyFraction=70</jvmOpts>
         </properties>
     </profile>
</profiles>

4.打包观察下效果,使用maven的命令  mvn clean install -Pbeta -DskipTests   ,-P 指定环境,后面写需要打包哪个环境,-Pbeta 代表指定beta环境配置,-DskipTests 表示跳过测试

可以看出,${env-flag} 里的内容已经替换成beta了。也可以根据项目实际情况进行不同的配置,总之使用这种方式可以将配置文件中的内容动态替换掉,更方便我们部署项目

 

 

还可以分目录配置环境

      

<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>

        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <excludes>
                    <!-- 排除这三个环境下的所有配置 -->
                    <exclude>test/*</exclude>
                    <exclude>beta/*</exclude>
                    <exclude>production/*</exclude>
                </excludes>
            </resource>
            <resource>
                <!-- 指定一个环境,最终就会只留这个配置在项目里 -->
                <directory>src/main/resources/${env-flag}</directory>
            </resource>
        </resources>
    </build>

执行命令  mvn clean install -Ptest -DskipTests

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值