利用maven的resources、filter和profile实现不同环境使用不同配置文件

来源:互联网 发布:淘宝花呗分期退货 编辑:程序博客网 时间:2024/05/21 06:24

基本概念说明(resources、filter和profile):
1. profiles定义了各个环境的变量id
2. filters中定义了变量配置文件的地址,其中地址中的环境变量就是上面profile中定义的值
3. resources中是定义哪些目录下的文件会被配置文件中定义的变量替换,一般我们会把项目的配置文件放在src/main/resources下,像db,bean等,里面用到的变量在打包时就会根据filter中的变量配置替换成固定值

在我们平常的java开发中,会经常使用到很多配制文件(xxx.properties,xxx.xml),而当我们在本地开发(dev),测试环境测试(test),线上生产使用(product)时,需要不停的去修改这些配制文件,次数一多,相当麻烦。现在,利用maven的filter和profile功能,我们可实现在编译阶段简单的指定一个参数就能切换配制,提高效率,还不容易出错,详解如下。

一,原理:

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

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

利用profile来切换环境

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

结合filter和profile,我们就可以方便的在不同环境下使用不同的配制

二,配置:

profiles配置多环境加载资源文件的几种方式
首先,依据maven的项目结构, 在src->main->filters建立不同环境资源文件目录, 放置资源文件

src/main/filters/dev/jdbc.properties – 开发环境
src/main/filters/product/jdbc.properties – 生产环境

项目结构

第一种方法

profiles->profile->build-filters-filter加载指定资源文件对源文件进行属性替换

<build><resources>    <!-- 先指定 src/main/resources下所有文件及文件夹为资源文件 -->    <resource>        <directory>src/main/resources</directory>        <includes>            <include>**/*</include>        </includes>    </resource>    <!-- 过滤指定文件,如spring-mybatis.xml,文件中的${key}会被替换掉为真正的值 -->    <resource>        <directory>src/main/resources</directory>        <includes>            <include>spring-mybatis.xml</include>        </includes>        <filtering>true</filtering>    </resource></resources></build><profiles><profile>    <id>dev</id>    <!-- 默认激活,使用dev目录中的属性文件来替换设置过滤的资源文件中的变量 -->    <activation>        <activeByDefault>true</activeByDefault>    </activation>    <build>        <filters>            <filter>src/main/filters/dev/jdbc.properties</filter>        </filters>    </build></profile><profile>    <id>product</id>    <build>        <filters>            <filter>src/main/filters/product/jdbc.properties</filter>        </filters>    </build></profile></profiles> 

第二种方法

profiles->profile->properties设置变量,build-filters-filter指定加载变量指定资源文件

<build><resources>    <resource>        <directory>src/main/resources</directory>        <!-- src/main/resources文件中全部开启过滤 -->        <includes>            <include>**/*</include>        </includes>        <filtering>true</filtering>    </resource>    <filters>        <filter>src/main/filters/${package.environment}/jdbc.properties</filter>    </filters></resources></build><profiles><profile>    <id>dev</id>    <!-- 默认激活,设置package.environment属性由filter引用 -->    <activation>        <activeByDefault>true</activeByDefault>    </activation>    <properties>        <package.environment>dev</package.environment>    </properties></profile><profile>    <id>product</id>    <properties>        <package.environment>product</package.environment>    </properties></profile></profiles> 

第三种方式

结合使用插件maven-war-plugin

<build><plugins>    <plugin>        <groupId>org.apache.maven.plugins</groupId>        <artifactId>maven-war-plugin</artifactId>        <version>3.2.0</version>        <configuration>            <archive>                <addMavenDescriptor>true</addMavenDescriptor>            </archive>            <warName>${project.build.finalName}</warName>            <webResources>                <resource>                    <directory>src/main/filters/${package.environment}</directory>                    <targetPath>WEB-INF/classes</targetPath>                </resource>            </webResources>        </configuration>        <!-- 也可以在插件中设置filtering直接替换属性        <configuration>            <filters>                <filter>src/main/filters/${package.environment}/jdbc.properties</filter>            </filters>            <archive>                <addMavenDescriptor>true</addMavenDescriptor>            </archive>            <warName>${project.build.finalName}</warName>            <webResources>                <resource>                    <directory>src/main/resources</directory>                    <targetPath>WEB-INF/classes</targetPath>                    <filtering>true</filtering>                </resource>                <resource>                    <directory>src/main/filters/${package.environment}</directory>                    <targetPath>WEB-INF/classes</targetPath>                    <filtering>true</filtering>                </resource>            </webResources>        </configuration>        -->    </plugin></plugins><resources>    <resource>        <directory>src/main/resources</directory>        <includes>            <include>**/*</include>        </includes>    </resource></resources></build><profiles><profile>    <id>dev</id>    <!-- 默认激活,设置package.environment属性由filter引用 -->    <activation>        <activeByDefault>true</activeByDefault>    </activation>    <properties>        <package.environment>dev</package.environment>    </properties></profile><profile>    <id>product</id>    <properties>        <package.environment>product</package.environment>    </properties></profile></profiles> 

说明:

  • warName :指定了打好包的名字;
  • webResources 下 resource 下 directory :配置真正使用的资源文件存放的位置,通常这个位置不是一个标准 Web 项目应该存放资源文件的位置;
  • webResources 下 resource 下 targetPath:将上一步 directory 中配置的文件目录下的文件都拷贝到这个目录下;
  • webResources 下 resource 下 filtering:开启打包之前将 directory 下的文件拷贝(覆盖)到 targetPath 下这种配置

三,使用和总结:

通过 -P 指定 profile

mvn clean package -DskipTests -Pproduct

不指定 profile 就默认打出的是开发环境的 war 包。

mvn package -DskipTests

前两种是在maven生命周期process-resources阶段,第三种是在package阶段,使用第三种方式应该注意src/main/resource目录下不能有被替换同名文件否则IDEA编译不通过;

也可以使用spring profile进行配置, 参考地址

阅读全文
0 0
原创粉丝点击