iT邦幫忙

2025 iThome 鐵人賽

DAY 7
1
Software Development

spring boot 3 學習筆記系列 第 7

Day07 - Spring Boot pom.xml 解析

  • 分享至 

  • xImage
  •  

pom.xml 是 Maven 專案的核心設定檔案,它負責定義專案的骨架、管理依賴套件,並設定建構 (build) 的所有流程。在 Spring Boot 專案中,它更是扮演著「統整依賴」和「簡化建構流程」的關鍵角色。

以下內容將以一個透過 Spring Initializr 建立的 Spring Boot 3 專案pom.xml 為例,我們將一步步解析每一個重要標籤(Tag)和元素(Element)進行解析,來理解每個設定的意義。

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

  <!-- 專案繼承自 Spring Boot 的父層 POM -->
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>3.5.5</version>
		<relativePath/> <!-- 從 Maven Repository 取得父層 POM -->
	</parent>

  <!-- 專案的基本資訊 -->
	<groupId>com.example</groupId>
	<artifactId>demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>demo</name>
	<description>Demo project for Spring Boot</description>
	<url/>
  
	<licenses>
		<license/>
	</licenses>

	<developers>
		<developer/>
	</developers>

	<scm>
		<connection/>
		<developerConnection/>
		<tag/>
		<url/>
	</scm>

  <!-- 專案屬性 -->
	<properties>
		<java.version>21</java.version>
	</properties>

  <!-- 專案依賴 -->
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

  <!-- 建構與插件設定 -->
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

1. <parent> (父層專案, Parent POM)

pom.xml 核心概念之一就是「繼承」。你可以將父層專案 (Parent POM) 想像成一個通用設計藍圖。有了這個藍圖,當你在開發專案時,就不用自己去設定版本號、編譯規則等繁瑣細節,而是直接繼承 Spring Boot 團隊的最佳實踐。

範例:

<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>3.5.5</version>
  <relativePath/>
</parent>

說明:

  • <groupId>:指定父層專案的群組 ID,在此範例這裡是 org.springframework.boot
  • <artifactId>:指定父層專案的 Artifact ID,在此範例這裡是 spring-boot-starter-parent
  • <version>:指定父層專案的版本,在此範例這裡是 3.5.5
  • <relativePath>:指定父層專案的相對路徑,當這個標籤留空時,Maven 會自動到線上的 Maven 中央倉庫(Maven Central Repository)下載這個父層專案。

主要功能:

  • 統一版本管理:這是一個非常重要的功能。spring-boot-starter-parent 使用了 Maven 的依賴管理 (Dependency Management) 機制。這表示當你在 dependencies 區塊中引入 spring-boot-starter-web 等其他 Spring Boot 啟動器時,你不需要手動指定版本號。Maven 會自動從父層專案繼承版本資訊,確保所有相關套件的版本都是相容的。這能大幅減少版本衝突問題。
  • 繼承預設設定:讓當前專案繼承 Spring Boot 的預設設定,包括依賴版本管理、編碼方式(例如:UTF-8)和通用的 Maven 組態,使專案配置更加簡單且一致。

2. 專案基本資訊(Project Info)

你可以將這些資訊想像成專案的「身分證」。這些標籤幫助 Maven 正確地辨識和管理你的專案,確保它在整個生態系中是唯一的。

範例:

<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<url/>

說明:

  • <groupId>:指定專案的群組 ID,通常是專案所屬組織或公司的唯一標識符。在這個範例,它被設置為 com.example
  • <artifactId>:指定專案的工件 ID,這是專案在群組中的唯一標識符,通常是專案的名稱。在這個範例,它被設置為 demo
  • <version>:指定專案的版本號。在這個範例,它被設置為 0.0.1-SNAPSHOT 是一個**快照(SNAPSHOT)**版本,代表它是一個正在開發中的不穩定版本。當你發布一個正式版時,通常會移除 -SNAPSHOT
  • <name>:指定專案的名稱。在這個範例,它被設置為 demo
  • <description>:提供專案的描述。在這個範例,它被設置為 Demo project for Spring Boot,表示這是一個 Spring Boot 的展示專案。
  • <url>:指定專案的 URL。(可選)

主要功能:

  • 唯一性識別:這些基本資訊有助於 Maven 能正確辨識專案與依賴,便於管理。

3. <licenses> (授權資訊, 可選)

用於描述專案的授權資訊。這些標籤通常包含專案所使用的授權類型、授權的名稱、URL 以及其他相關資訊。

範例:

<licenses>
    <license>
        <name>Apache License, Version 2.0</name>
        <url>http://www.apache.org/licenses/LICENSE-2.0</url>
        <distribution>repo</distribution>
    </license>
</licenses>

說明:

  • <name>:指定了授權的名稱。
  • <url>:提供了授權的 URL。
  • <distribution>:描述了授權的分發方式。

主要功能:

  • 授權聲明:明確告知專案使用者和開發者了解此專案的授權條款。

4. <developers> (開發者資訊, Developers, 可選)

用於指定專案的開發人員資訊,通常包含開發人員的詳細資訊,例如姓名、電子郵件、角色等。

範例:

<developers>
  <developer>
    <id>developer1</id>
    <name>John Doe</name>
    <email>john.doe@example.com</email>
    <roles>
      <role>developer</role>
    </roles>
  </developer>
</developers>

說明:

  • <developers>:用來包裹所有開發人員資訊的容器標籤(Tag)。
  • <developer>:用來描述單個開發人員資訊的元素(Element)。
    • <id>:開發人員的唯一識別碼。
    • <name>:開發人員姓名。
    • <email>:開發人員電子郵件。
    • <roles>:專案中擔任的角色,例如開發者、維護者等。

主要功能:

  • 聯繫資訊:方便專案合作與維護時能夠聯繫到對應開發人員。

5. <scm> (版本控制管理, Source Control Management, 可選)

用於定義專案的版本控制管理(如 Git)資訊。

範例:

<scm>
  <connection/>
  <developerConnection/>
  <tag/>
  <url/>
</scm>

說明:

  • <connection>:定義匿名讀取存取 SCM 儲存庫的連接 URL。
  • <developerConnection>:定義開發者讀寫存取 SCM 儲存庫的連接 URL。
  • <tag>:定義專案在 SCM 中的標籤或版本。
  • <url>:定義 SCM 儲存庫的瀏覽 URL。

主要功能:

  • 版本控制:有助於專案的版本管理和協作開發。

6. <properties> (屬性)

這個區塊就像是專案的「變數」區和用來設定版本號、路徑、專案的編碼方式等資訊。你可以在這裡定義各種屬性,並在 pom.xml 的其他地方重複使用,這有助於統一管理和維護。

範例:

<properties>
  <java.version>21</java.version>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <!-- 指定編碼 -->
</properties>

說明:

  • <java.version> 元素:這個元素定義了專案所使用的 Java 版本。在這個範例中,設定為 21,表示專案將使用 Java 21 版本來編譯和執行。
  • <project.build.sourceEncoding>:設定專案的編碼格式,通常設為 UTF-8

主要功能:

  • 一致性管理:確保不同環境下使用相同版本與設定,避免環境差異導致問題。

7. <dependencies> (依賴)

這裡是專案的「購物清單」。你可以在這裡列出專案運行所需的所有外部函式庫 (libraries) 或套件 (packages)。Maven 會根據清單自動下載並引入這些套件。

範例:

以下為 Spring Boot 預設相依套件:

<dependencies>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
  </dependency>

  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
  </dependency>
</dependencies>

說明:

以下為 Spring Boot 預設相依套件分別是:

  • spring-boot-starter:Spring Boot 基本功能的的核心啟動套件,包含了最基本的 Spring Framework 和 Spring Boot 功能。
  • spring-boot-starter-test:提供了測試所需的一切工具,例如 JUnit 5、Mockito 等。
  • <scope>test</scope>:這裡的 scope 標籤定義了這個依賴的作用範圍。test 表示這個套件只會在執行測試時使用,打包發布時不會被包含在內。

如果需要添加其他依賴,只需將對應的 <dependency> 區塊添加到 <dependencies> 中即可。例如,若需要加入 Web 應用的功能:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
  • spring-boot-starter-web:包含 Web 應用程式所需的基礎函式庫。

以下是對 <dependency> 元素(Element)的說明:

  • <groupId>: 定義了相依套件的組織或群組,這裡是 org.springframework.boot
  • <artifactId>: 定義了相依套件的名稱,這裡是 spring-boot-starter-test
  • <scope>: 定義了相依套件的作用範圍,這裡是 test,表示這個相依套件僅在測試階段使用。

主要功能:

  • 依賴管理:Maven 會自動處理依賴版本與下載。

8. <build> (建構)

這個區塊告訴 Maven 如何「建造」你的專案。你可以在這裡設定專案的建構流程,例如編譯、測試、打包,並定義要使用的各種插件 (Plugins)。

範例:

<build>
  <plugins>
    <plugin>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>
  </plugins>
</build>

說明:

  • <plugins>: 一個容器,用來存放所有建構(build)過程中需要使用的插件(Plugins)清單,這些插件(Plugins)會在建構(build)過程中被執行。
  • <plugin>: 定義一個單獨的 Maven 插件(Plugins)。
    • <groupId>: 插件(Plugins)的群組 ID。
    • <artifactId>: 插件(Plugins)的 Artifact ID。

範例中配置了 spring-boot-maven-plugin 插件,這是 Spring Boot 專案最重要的插件之一。它有兩大主要功能:

  • 打包可執行檔:將你的應用程式及其所有依賴套件打包成一個獨立的、可執行的 JAR 檔案。這個 JAR 檔已經內建了 Web 伺服器,所以你不需要額外安裝,可以直接用 java -jar your-app.jar 指令來運行。
  • 運行專案:支援 mvn spring-boot:run 指令,讓你快速啟動專案,方便開發與測試。

主要功能:

  • 自動化建構與執行:支援 Spring Boot 應用程式的打包與執行。
  • 能用 mvn spring-boot:run 啟動專案,或打包成可執行 .jar

精簡版 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 
         https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>3.5.5</version>
  </parent>

  <groupId>com.example</groupId>
  <artifactId>demo</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <properties>
    <java.version>21</java.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>
</project>

透過上述配置,您可以快速建立並運行一個基於 Spring Boot 3 的 Web 專案。

總結:Maven 專案的運作流程

透過以上介紹,我們可以將整個 Maven 專案的運作流程想像成這樣:

 pom.xml (專案藍圖)
        │
        ▼
  解析依賴 <dependencies>
        │
        ▼
  從 Maven 中央倉庫 下載所需套件 (.jar)
        │
        ▼
  執行建構 <build> (編譯、測試、打包)
        │
        ▼
  得到可執行 JAR (Spring Boot 應用)

透過這個簡單的流程,就能理解 pom.xml 如何將專案的設計與實作完美地連結在一起。

相關資料來源


上一篇
Day06 - Spring Boot Gradle 簡介
下一篇
Day08 - Spring Boot Properties 概念入門
系列文
spring boot 3 學習筆記17
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言