maven發布時在不同的環境使用不同的配置文件


  在開發時,不同的環境總會使用到不同的配置。如本地,測試,預發布,發布等環境,像數據庫這些都要使用到不同的配置。如果手動改的話肯定會十分的麻煩。

還好maven提供的功能能夠幫我們解決這個問題。

 

我們通過不同環境使用不同數據庫的配置來說明

直接上代碼:

1.db.properties

jdbc.username=${jdbc.username}
jdbc.password=${jdbc.password}
jdbc.url=${jdbc.url}
name=${myName}

2.dev.properties

jdbc.url=jdbc:mysql://127.0.0.1:3306/devdb?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull
jdbc.username=devuser
jdbc.password=dev123456

3.product.properties

jdbc.url=jdbc:mysql://127.0.0.1:3306/productdb?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull
jdbc.username=productuser
jdbc.password=product123456

4.test.properties

jdbc.url=jdbc:mysql://127.0.0.1:3306/testdb?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull
jdbc.username=testuser
jdbc.password=test123456

5.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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>mavenImparityProfile</groupId>
  <artifactId>mavenImparityProfile</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>mavenImparityProfile Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

    <profiles>
        <profile>
            <id>test</id>
            <properties>
                <env>test</env><!--相當於定義一個變量 供下面使用-->
                <myName>張三</myName><!--使用一個properties文件中未定義,但是其他地方會取值的變量-->
            </properties>
            <activation><!--默認激活-->
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>dev</id>
            <properties>
                <env>dev</env>
                <myName>李四</myName>
            </properties>
            <activation><!--默認激活-->
                <activeByDefault>false</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>product</id>
            <properties>
                <env>product</env>
            </properties>
        </profile>
    </profiles>
  <build>
    <finalName>mavenImparityProfile</finalName>
      <filters><!--獲得過濾使用的源文件  即有實際數據的地反-->
          <filter>src/main/resources/properties/${env}.properties</filter>
      </filters>

      <!-- 指定 src/main/resources下所有文件及文件夾為資源文件 -->
      <resources>
        <resource>
            <directory>src/main/resources</directory>
             <filtering>true</filtering> <!--是否使用過濾器-->
         </resource>
          <!-- 第二中方式 設置對dev.properties,等進行過慮,即這些文件中的${key}會被替換掉為真正的值-->
          <!-- <resource>
              <directory>src/main/resources/properties/properties</directory>&lt;!&ndash;一定要指向上層目錄&ndash;&gt;
              <includes>
                  &lt;!&ndash;要遍歷出來文件都必須寫&ndash;&gt;
                  <include>product.properties</include>
                  <include>test.properties</include>
                  <include>dev.properties</include>
                  <include>db.properties</include>
              </includes>
              <filtering>true</filtering>
          </resource>-->
     </resources>
  </build>

</project>

 

其實現主要是通過配置frofile來實現。上面配置了3個環境(test,dev,product)。test環境是默認激活的。

我們直接執行 deploy 則使用的是test的配置。

如果要使用product的配置,則使用maven的命令 mvn clean package -P product (注:-P要大寫 -P后面的參數是我們前面定義不同環境的id。如果你使用的是idea工具,在配置run的時候不用寫mvn這個參數)

 

附錄:

如果要配置jenkins,其他的參數配置不變,只需要修改maven的命令

 

這里是發布product環境


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM