使用maven管理不同环境的配置文件

1.使用maven中properties标签定义变量

        (1)引入

                  在pom.xml中添加依赖时语法如下:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>1.2.6</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aop</artifactId>
    <version>1.2.6</version>
</dependency>
             以上配置存在一个问题,就是在spring依赖中,我们需要引入一系列版本的spring,如版本1.2.6.每次都写不利于维护

        (2)解决

                  在pom.xml中定义properties标签

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <spring.version>1.2.6</spring.version>
    <developer.organization><![CDATA[xy公司]]></developer.organization>
</properties>
                 那么上面的配置就可以写成:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>${spring.version}</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aop</artifactId>
    <version>${spring.version}</version>
</dependency>

2.使用maven profile中的filter

        (1)总体概览

        实际上filter是表示存储属性值的文件在哪(相当于from);

      profile一般也就是个文件名,不同环境配置不同的;

     <resource>中表示哪些文件需要替换,可以是目录


        (2)profile

          一个项目可以部署在不同的环境中,maven的profile针对不同的环境指定各自的编译方法。在pom.xml的profile中,可以根据不同的环境定制以下内容:

<profiles>
  <profile>
    <id>dev</id>
    <activation> <!--默认激活的配置 -->
      <activeByDefault>true</activeByDefault>
    </activation>
    ...
  </profile>
</profiles>

         如果需要在执行的时候指定使用哪个profile,可以通过-P传入。如:maven package –P  dev ,就可以使用dev的配置来打包了


        (2)build配置项

                  build配置项可以出现在两处:

<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>...</build>

  <profiles>
    <profile>
      <!-- 特殊的构建 -->
      <build>...</build>
    </profile>
  </profiles>
</project>


        (3)filter规则

                  maven通过过滤器来修改部署时的不同配置。

                  部署时所有资源的配置,如果根据环境不同,有不同的配置,则需要在资源中加上以下形式的标记表示替换

${要替换的属性名称}
                 如在spring.xml中要配置上传文件的路径

<beans>
  <bean id="uploadService" class="com.oist.project.service.UploadServiceImpl">
    <property name="uploadDir" value="${spring.uploadDir}"/>
  </bean>
</beans>
               在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>
    <filters> <!-- 指定 filter -->
      <filter>src/main/filters/${deploy.env}.properties</filter>
    </filters>
    <resources>
      <resource> <!-- spring.xml 应该在 src/main/resources 目录下 -->
        <directory>src/main/resources</directory> 
        <filtering>true</filtering> <!-- 是否使用过滤器 -->
      </resource>
    </resources>
  </build>

  <profiles>
    <profile>
      <id>development</id>
      <activation>
        <activeByDefault>true</activeByDefault>
      </activation>
      <propertys>
        <deploy.env>develop</deploy.env>
      </propertys>
    </profile>

    <profile>
      <id>test</id>
      <propertys>
        <deploy.env>test</deploy.env>
      </propertys>
    </profile>

    <profile>
      <id>production</id>
      <propertys>
        <deploy.env>production</deploy.env>
      </propertys>
    </profile>

  </profiles>
</project>

                然后就可以针对不同的环境设置不同的目录了:

                src/main/filters/develop.properties文件

# 上传路径:
spring.uploadDir=c:/uploadDir
                 src/main/filters/test.properties文件

# 上传路径:
spring.uploadDir=/tmp/upload_dir
                 src/main/filters/production.properties文件
# 上传路径:
spring.uploadDir=/app/project/upload_dir
                如果配置了多个filter,并且2个filter中有相同的key,则后面的value为最终取值
<build>
  <filters>
    <filter>src/main/filters/production.properties</filter>
    <filter>src/main/filters/test.properties</filter>
  </filters>
</build>
               因为test.properties在最后,因为spring.uploadDir为test.properties的取值/tmp/upload_dir

               实际上顺序就是:resource中找到需要匹配的文件->从需要属性的文件开始-->filter->properties中的值(profile在这里实际上就是作为参数,根据不同环境为filter赋值)

3.maven的properties加载顺序

        (1)<build><filters>中的配置

        (2)pom.xml中的<properties>

        (3)mvn -Dproperty=value中定义的property

          相同key的property,以最后一个文件中的配置为最终配置

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值