iT邦幫忙

2025 iThome 鐵人賽

DAY 10
0
Software Development

我們與Maven的距離系列 第 19

Day18 - Profile

  • 分享至 

  • xImage
  •  

前言

以專案開發來說,我們會在本機開發,部署sit環境進行整合測試,再部署到uat環境進行使用者驗收測試,最後驗收通過上版至正式環境運行,算一算你會有4套DB連線設定資訊,你會希望有一套機制能夠幫你在構建專案時將對應的設定檔包進去,這時候你就少不了Maven Profile幫你這個忙了

What is Profile

Maven Profile是一組可以讓你為了在不同環境運行配置在建置時抽換的設定檔
設置位置:pom.xml(最常見)、settings.xml(使用者/全域)、profiles.xml(較少用)
設定資源過濾:告訴maven那些資源需要特別處理
啟動方式

範例演示Explicit

1. 創建web project

2. 於src/main下創建resources資料夾

3. 創建dbconfig.properties

profile.env=${profile.active}
database.jdbc.driverClassName=${db.driverClassName}
database.jdbc.url=${db.url}
database.jdbc.name=${db.name}
database.jdbc.password=${db.password}

4.pom.xml設定

開啟靜態資源路徑掃描<filtering>,這樣maven才會認得哪邊的設定檔需要加工處理,採用默認的<activeByDefault>,讓dev環境為預設有效,這樣就不在command就無須加入-P參數

   <build>  
   <!-- 略 -->
    <resources>
      <resource>
        <directory>src/main/resources</directory>
        <filtering>true</filtering>
      </resource>
    </resources>
  </build>
  <profiles>
    <profile>
      <id>dev</id>
      <activation>
        <activeByDefault>true</activeByDefault>
      </activation>
      <properties>
        <profile.active>dev</profile.active>
        <db.driver>com.mysql.cj.jdbc.Driver</db.driver>
        <db.url>jdbc:mysql://localhost:3306/yourdb</db.url>
        <db.username>devuser</db.username>
        <db.password>devpassword</db.password>
      </properties>
    </profile>
  </profiles>

5. 執行mvn clean package

https://ithelp.ithome.com.tw/upload/images/20251003/20128084lrQT9FUEpN.png

6. 確認構建設置db資訊正確

https://ithelp.ithome.com.tw/upload/images/20251003/20128084Mqkm3u8NJy.png

範例演示更換檔案的方式

有的時候我們會更希望以明確的方式將各個環境的設定檔一併進入版控,讓構建的時候在決定要將哪一個設定檔納入,這邊我們使用maven-antrun-plugin來達成

myweb
\---main
    +---resources
    |       dbconfig-prod.properties
    |       dbconfig-sit.properties
    |       dbconfig.properties
    |

我們來設置每一個環境的設定檔(sit、prod)

# sit environment
database.jdbc.driverClassName=com.mysql.cj.jdbc.Driver
database.jdbc.url=jdbc:mysql://localhost:3306/sitdb
database.jdbc.na=situser
database.jdbc.pa=sitpassword

pom.xml設定

 <profiles>
    <!-- SIT 環境 Profile -->
    <profile>
      <id>sit</id>
      <properties>
        <env>sit</env>
      </properties>
      <build>
        <resources>
          <!-- 處理其他資源檔案,排除所有 dbconfig 檔案 -->
          <resource>
            <directory>src/main/resources</directory>
            <excludes>
              <exclude>dbconfig*.properties</exclude>
            </excludes>
          </resource>
        </resources>
        <plugins>
          <!-- 使用 antrun 來複製並重新命名配置檔 -->
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>3.1.0</version>
            <executions>
              <execution>
                <phase>process-resources</phase>
                <goals>
                  <goal>run</goal>
                </goals>
                <configuration>
                  <target>
                    <copy file="src/main/resources/dbconfig-sit.properties"
                          tofile="${project.build.outputDirectory}/dbconfig.properties"/>
                  </target>
                </configuration>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </profile>

    <!-- PROD 環境 Profile -->
    <!-- 略 -->
  </profiles>

執行maven clean package -Pprod
https://ithelp.ithome.com.tw/upload/images/20251003/20128084yLiMCNOjse.png

小結

今日介紹了好用的profile讓我們在構建各個環境時候可以使用特定的profile進行構建,並介紹2個實務上構建時會採用的方式。另外我們未介紹隱式啟用啟動設定檔的方式(Implicit profile activation),官網這部分寫得很清楚,留給讀者自行嘗試。

Reference


上一篇
Day17 - BOM (Bill of Materials)
下一篇
Day19 - JAR Packaging Discussion
系列文
我們與Maven的距離24
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言