zoukankan      html  css  js  c++  java
  • Maven里面多环境下的属性过滤(配置)

    情景:通常一个项目都为分为开发环境(dev)和测试环境(test)还有正式环境(prod),如果每次一打包都要手动地去更改配置文件,例如数据库连接配置。将会很容易出差错。

    解决方案:maven profiles。maven提供了profiles元素让开发者们可以定义相关的变量的值,然后在打包的时候根据profile的id去选择变量写入配置文件中。

    例如:

      配置文件jdbc.proterties里面的内容:

    name=${pom.name}
    password=${pom.password}

    pom.xml 里面的profiles内容
    <profiles>
    <profile>
    <id>dev</id>
    <properties>
    <pom.name>dev</pom.name>
    <pom.password>123456</pom.password>
    </properties>
    <activation>
    <activeByDefault>true</activeByDefault>
    </activation>
    </profile>
    <profile>
    <id>test</id>
    <properties>
    <pom.name>test</pom.name>
    <pom.password>1.1</pom.password>
    </properties>
    </profile>
    </profiles>





    当使用maven命令打包的时候,在命令后加 -P dev 例如:mvn package -P dev 。这时候maven就会选择profile的id是dev的里面的变量写入到jdbc.properties里面。

    大概过程就是这样,下面写写重点:
    问题:为什么会写入jdbc.properties文件而不是application.properties或者web.xml文件呢。
    答案:因为还需要在pom文件里面的build标签下配置filter标签,内容如下:
    <filters>
    <filter>src/main/resources/jdbc.properties</filter>
    </filters>

    还需要配置resource标签,这个标签标明需要被过滤的资源文件,内容如下:

    <resources>
           <resource>
    <directory>src/main/resources</directory>
    <filtering>true</filtering>
    </resource>
    </resources>

    这个标签的意思就是说资源文件放在
    directory 配置的目录下,并且开启了过滤模式,表示为遇到filters里面的文件的时候就改变里面的el表达式变量。
    整个pom文件如下:
    <?xml version="1.0" encoding="UTF-8"?>
    <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">
    <parent>
    <artifactId>dubbo-learn</artifactId>
    <groupId>com.learn.dubbo</groupId>
    <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>maven-profiles</artifactId>
    <build>
    <filters>
    <filter>src/main/resources/jdbc.properties</filter>
    </filters>
    <resources>
    <resource>
    <directory>src/main/resources</directory>
    <filtering>true</filtering>
    </resource>
    </resources>
    </build>
    <profiles>
    <profile>
    <id>dev</id>
    <properties>
    <pom.name>dev</pom.name>
    <pom.password>123456</pom.password>
    </properties>
    </profile>
    <profile>
    <id>test</id>
    <properties>
    <pom.name>test</pom.name>
    <pom.password>1.1</pom.password>
    </properties>
    </profile>
    </profiles>
    </project>

    注意:filters里面的文件都只能是文件的全名不能是 *.properties这样的格式。否则会报错。如果想要同表达式匹配指定的文件。
    可以使用resource里面的include标签,修改不使用filters而使用include后的文件为:
    <?xml version="1.0" encoding="UTF-8"?>
    <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">
    <parent>
    <artifactId>dubbo-learn</artifactId>
    <groupId>com.learn.dubbo</groupId>
    <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>maven-profiles</artifactId>
    <build>
    <!-- <filters>
    <filter>src/main/resources/application.properties</filter>
    </filters>-->
    <resources>
    <resource>
    <directory>src/main/resources</directory>
    <includes>
    <include>*.properties</include>
    </includes>
    <filtering>true</filtering>
    </resource>
    </resources>
    </build>
    <profiles>
    <profile>
    <id>dev</id>
    <properties>
    <pom.name>dev</pom.name>
    <pom.password>123456</pom.password>
    </properties>
    </profile>
    <profile>
    <id>test</id>
    <properties>
    <pom.name>test</pom.name>
    <pom.password>123456</pom.password>
    </properties>
    </profile>
    </profiles>
    </project>

    有机会再讲讲include和exclude的内容。晚安

  • 相关阅读:
    CSS中的单位简介
    grid-layout
    position
    form
    objects
    个人介绍
    第十二周作业:
    第十一周作业
    第十周web作业
    第九周作业
  • 原文地址:https://www.cnblogs.com/JieWen/p/9326299.html
Copyright © 2011-2022 走看看