在项目开发时,我们用的是本地搭建的开发dev环境,开发完成打包部署到服务器时,用到的是服务器prod环境。可以借用Mavenprofilesfiltersresources,在运行或打包时指定选用的环境,实现不同环境自动使用各自环境的配置文件或配置信息。

  • profiles:定义环境变量的id;
  • filters:定义了变量配置文件的地址,其中地址中的环境变量就是上面profile中定义的值;
  • resources:定义哪些目录下的文件会被配置文件中定义的变量替换,另外可以指定目录下的文件打包到classes目录下。

定义环境变量profiles

一般环境变量分:dev开发环境、prod发布环境,当然也可以类比添加其他的环境标志。
此处详细可参看:maven profile动态选择配置文件maven profile切换正式环境和测试环境

    <profiles>
        <!-- 开发测试环境 -->
        <profile>
            <id>dev</id>
            <activation>
                <!-- 设置默认激活dev环境的配置 -->
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <profile.env>dev</profile.env>
            </properties>
        </profile>
        <!-- 产品发布环境 -->
        <profile>
            <id>prod</id>
            <properties>
                <profile.env>prod</profile.env>
            </properties>
        </profile>
    </profiles>

配置打包的资源文件

    <build>
        <finalName>${project.artifactId}</finalName>
        
        <!-- 定义变量配置文件:各个环境的配置文件都放在项目的src/main/resources/env/目录下 -->
        <filters>
            <filter>src/main/resources/env/application-${profile.env}.properties</filter>
        </filters>
        
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <!-- 开启变量替换功能 将<properties></properties>里面的变量值替换类路径文件中的含有${var} -->
                <filtering>true</filtering>
                <excludes>
                    <!-- 该目录由于使用filter过滤用来取值,打包时就不需要,要排除! -->
                    <exclude>env/*</exclude>
                    <exclude>rebel.xml</exclude>
                </excludes>
            </resource>

            <!-- 打包时,各个环境的资源文件目录。 -->
            <resource>
                <directory>src/main/profiles/${profile.env}</directory>
            </resource>
        </resources>

        <!-- 以下省略配置plugins等内容 -->
    </build>

项目布局图如下:
maven_filters_resources.bmp

使用profilefilter合并配置文件

filter配置文件application.properties在打包时会根据环境参数确定配置文件application-dev.propertiesapplication-prod.properties,将文件中{key}进行替换,最终把你的资源文件中的param_key=${key}替换成param_key=value。
application.properties文件:

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=${env.jdbc.url}
jdbc.username=${env.jdbc.username}
jdbc.password=${env.jdbc.password}

application-dev.properties文件:

env.jdbc.url=jdbc:mysql://localhost:3306/db_dev?characterEncoding=utf-8&amp;rewriteBatchedStatements=true
env.jdbc.username=root
env.jdbc.password=123

application-prod.properties文件:

env.jdbc.url=jdbc:mysql://localhost:3306/db_prod?characterEncoding=utf-8&amp;rewriteBatchedStatements=true
env.jdbc.username=prod_db_username
env.jdbc.password=prod_db_password

使用filter需要注意事项

resource中使用filtering开启变量替换功能后,二进制文件默认也会经过filter处理,导致二进制文件内容被更改,需要设置排除不进行过滤。
例如下面配置扩展名为:xlsx p12 不应用filter。

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-resources-plugin</artifactId>
  <configuration>
    <encoding>${project.build.sourceEncoding}</encoding>
    <!-- 不应用过滤的附加文件扩展名 -->
    <!-- https://maven.apache.org/plugins/maven-resources-plugin/examples/binaries-filtering.html -->
    <nonFilteredFileExtensions>
      <nonFilteredFileExtension>xlsx</nonFilteredFileExtension>
      <nonFilteredFileExtension>p12</nonFilteredFileExtension>
    </nonFilteredFileExtensions>
  </configuration>
</plugin>

使用profileresource打包环境的资源文件

<resource>
    <directory>src/main/profiles/${profile.env}</directory>
</resource>

如果profile指定值为dev,目录src\main\profiles\dev下的文件会被打包;
如果profile指定值为prod,目录src\main\profiles\prod下的文件会被打包。

参考文章

标签: java, maven

添加新评论