【maven】5、Maven 多环境部署(filters、resources、profiles)

在项目开发过程中,我们会用到很多配置文件,如db.properties、application.properties、logback.xml等。大体目录结构如下:
资源配置

比如项目中datasource-context.xml中使用的连接mongodb配置

<property name="username" value="${mongo.username1}" />
                    <property name="password" value="${mongo.password1}" />
                    <property name="database" value="${mongo.database1}" />
                    <property name="mechanism" value="${mongo.mechanism1}" />

在connector.properties配置文件中配置具体参数值

mongo.username1=username
mongo.password1=password
mongo.database1=test
mongo.mechanism1=SHA-1

在部署多种环境(本地开发环境、测试环境、生产环境)时username、password值都需要改变。Maven提供了一种解决方案,即使用filters、resources、profiles功能来解决多种环境部署。

以下是在项目中实际使用:

首先在pom.xml文件,build元素下添加resources

   <build>
        <finalName>${project.artifactId}</finalName>
        <resources>
            <resource>
                <directory>${basedir}/src/main/resources</directory>
            </resource>
            <resource>
                <directory>${basedir}/src/main/resources-${environment}</directory>
            </resource>
        </resources>
    </build>

在build同级添加profiles

<profiles>
        <profile>
            <id>test</id>
            <properties>
                <environment>test</environment>
            </properties>
        </profile>
        <profile>
            <id>production</id>
            <properties>
                <environment>production</environment>
            </properties>
        </profile>
        <profile>
            <id>production-hk</id>
            <properties>
                <environment>production-hk</environment>
            </properties>
        </profile>
    </profiles>

Java项目创建默认的资源路径为${basedir}/src/main/resources
执行打包命令时添加 -P${environment}, Maven会使用对应的资源路径覆盖${basedir}/src/main/resources
mvn clean assembly:assembly -DskipTests -Pproduction 项目有子项目war、jar共存
mvn clean compile war:war -Pproduction 打war包

resources作用

项目中多环境配置文件存放在不同的production-${environment}下,resources中指明有哪些环境。

profiles作用

profiles定义了每个部署环境的标识,打包命令中-Pproduction,production就是对应的标识(profile id)

以上是自己在项目中具体使用方式,但是关于Maven多环境配置一般都是filters、resources、profiles一起组合使用,我们这里还差一个filters功能,那么我们就看看filters到底何方神圣。

filters配置
一般使用filters是在build元素下添加filters子元素,与resources同级。

<filters> 
  <filter>src/main/filters/connector-${env}.properties</filter>
  <filter>src/main/filters/application-${env}.properties</filter> 
</filters> 

filters作用

filters中指定需要进行替换的资源文件,打包时根据${profile id}替换对应的资源文件。

比如需要打test环境下的war包,那么我们项目中${basedir}/src/main/resources/connector.properties文件会被${basedir}/src/main/filters/connector-test.properties替换。

总结filters
对比一下使用filtes和没有使用filters区别
1、没有使用filters:Maven会根据profile id将对应resources-${profile id}资源文件整体覆盖到src/main/resources下,全局替换。
2、使用filters:Maven会根据指定filter文件进行替换,指定替换。

本章中介绍了项目多环境部署中涉及配置文件处理方法,使用到Maven的filters、resources、profiles。
使用filters多出一个平时很少使用到的文件夹filters,那么Maven建议我们项目应该创建哪些文件夹嗯?下章见!


Maven pom.xml主体结构

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                             http://maven.apache.org/xsd/maven-4.0.0.xsd">
  ...

 <build>
        <finalName>${project.artifactId}</finalName>
        <filters> <!-- 指定 filter -->
           <filter>src/main/filters/connector-${env}.properties</filter>
           <filter>src/main/filters/application-${env}.properties</filter> 
        </filters>
        <resources>
            <resource>
                <directory>${basedir}/src/main/resources</directory>
            </resource>
            <resource>
                <directory>${basedir}/src/main/resources-${environment}</directory>
            </resource>
        </resources>
        <pluginManagement>
            <plugins>
                <plugin>
                    ......
                </plugin>
                ......
            </plugins>
        </pluginManagement>
    </build>

    <profiles>
        <profile>
            <id>test</id>
            <properties>
                <environment>test</environment>
            </properties>
        </profile>
        <profile>
            <id>production</id>
            <properties>
                <environment>production</environment>
            </properties>
        </profile>
        <profile>
            <id>production-hk</id>
            <properties>
                <environment>production-hk</environment>
            </properties>
        </profile>
    </profiles>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值