Maven - POM



POM 代表專案物件模型 (Project Object Model)。它是 Maven 中的基本工作單元。它是一個 XML 檔案,位於專案的基目錄中,名為 pom.xml。

POM 包含有關專案的資訊以及 Maven 用於構建專案(s) 的各種配置詳細資訊。

POM 還包含目標和外掛。在執行任務或目標時,Maven 會在當前目錄中查詢 POM。它讀取 POM,獲取所需的配置資訊,然後執行目標。可以在 POM 中指定的一些配置如下:

  • 專案依賴
  • 外掛
  • 目標
  • 構建配置檔案
  • 專案版本
  • 開發者
  • 郵件列表

在建立 POM 之前,我們應該首先確定專案的**組**(groupId)、其**名稱**(artifactId) 和其版本,因為這些屬性有助於在儲存庫中唯一標識專案。

POM 示例

<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">
   <modelVersion>4.0.0</modelVersion>

   <groupId>com.companyname.project-group</groupId>
   <artifactId>project</artifactId>
   <version>1.0</version>
</project>

需要注意的是,每個專案應該只有一個 POM 檔案。

  • 所有 POM 檔案都需要**project** 元素和三個必填欄位:**groupId、artifactId、version**。

  • 儲存庫中的專案表示法為**groupId:artifactId:version**。

  • POM 的最低要求:

序號 節點和描述
1

專案根

這是專案根標籤。您需要指定基本的模式設定,例如 apache 模式和 w3.org 規範。

2

模型版本

模型版本應為 4.0.0。

3

groupId

這是專案組的 ID。這通常在一個組織或專案中是唯一的。例如,一個銀行集團 com.company.bank 包含所有與銀行相關的專案。

4

artifactId

這是專案的 ID。這通常是專案名稱。例如,consumer-banking。連同 groupId 一起,artifactId 定義了儲存庫中構件的位置。

5

version

這是專案的版本。與 groupId 一起,它用於構件的儲存庫中,以將不同版本彼此區分開來。例如:

com.company.bank:consumer-banking:1.0

com.company.bank:consumer-banking:1.1。

超級 POM

超級 POM 是 Maven 的預設 POM。所有 POM 都繼承自父 POM 或預設 POM(無論是否顯式定義)。這個基本 POM 稱為**超級 POM**,包含預設繼承的值。

Maven 使用有效的 POM(來自超級 POM 的配置加上專案配置)來執行相關目標。它幫助開發者在其 pom.xml 中指定最少的配置細節。儘管配置可以很容易地被覆蓋。

檢視超級 POM 的預設配置的一種簡單方法是執行以下命令:**mvn help:effective-pom**

在計算機上的任何目錄中建立一個 pom.xml。使用上面提到的示例 pom 的內容。

在下面的示例中,我們在 C:\MVN\project 資料夾中建立了一個 pom.xml。

現在開啟命令控制檯,轉到包含 pom.xml 的資料夾,並執行以下**mvn**命令。

C:\MVN\project>mvn help:effective-pom

Maven 將開始處理並顯示有效的 pom。

C:\MVN>mvn help:effective-pom
[INFO] Scanning for projects...
[INFO]
[INFO] ---------------< com.companyname.project-group:project >----------------
[INFO] Building project 1.0
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-help-plugin:3.2.0:effective-pom (default-cli) @ project ---
[INFO]
Effective POMs, after inheritance, interpolation, and profiles are applied:

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  2.261 s
[INFO] Finished at: 2021-12-10T19:54:53+05:30
[INFO] ------------------------------------------------------------------------

C:\MVN>

應用繼承、插值和配置檔案後,有效的 POM 將作為控制檯中的結果顯示。

<?xml version="1.0" encoding="Cp1252"?>
<!-- ====================================================================== -->
<!--                                                                        -->
<!-- Generated by Maven Help Plugin on 2021-12-10T19:54:52+05:30            -->
<!-- See: http://maven.apache.org/plugins/maven-help-plugin/                -->
<!--                                                                        -->
<!-- ====================================================================== -->
<!-- ====================================================================== -->
<!--                                                                        -->
<!-- Effective POM for project                                              -->
<!-- 'com.companyname.project-group:project:jar:1.0'                        -->
<!--                                                                        -->
<!-- ====================================================================== -->
<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>
   <groupId>com.companyname.project-group</groupId>
   <artifactId>project</artifactId>
   <version>1.0</version>
   <repositories>
      <repository>
         <snapshots>
            <enabled>false</enabled>
         </snapshots>
         <id>central</id>
         <name>Central Repository</name>
         <url>https://repo.maven.apache.org/maven2</url>
      </repository>
   </repositories>
   <pluginRepositories>
      <pluginRepository>
         <releases>
            <updatePolicy>never</updatePolicy>
         </releases>
         <snapshots>
            <enabled>false</enabled>
         </snapshots>
         <id>central</id>
         <name>Central Repository</name>
         <url>https://repo.maven.apache.org/maven2</url>
      </pluginRepository>
   </pluginRepositories>
   <build>
      <sourceDirectory>C:\MVN\src\main\java</sourceDirectory>
      <scriptSourceDirectory>C:\MVN\src\main\scripts</scriptSourceDirectory>
      <testSourceDirectory>C:\MVN\src\test\java</testSourceDirectory>
      <outputDirectory>C:\MVN\target\classes</outputDirectory>
      <testOutputDirectory>C:\MVN\target\test-classes</testOutputDirectory>
      <resources>
         <resource>
            <directory>C:\MVN\src\main\resources</directory>
         </resource>
      </resources>
      <testResources>
         <testResource>
            <directory>C:\MVN\src\test\resources</directory>
         </testResource>
      </testResources>
      <directory>C:\MVN\target</directory>
      <finalName>project-1.0</finalName>
      <pluginManagement>
         <plugins>
            <plugin>
               <artifactId>maven-antrun-plugin</artifactId>
               <version>1.3</version>
            </plugin>
            <plugin>
               <artifactId>maven-assembly-plugin</artifactId>
               <version>2.2-beta-5</version>
            </plugin>
            <plugin>
               <artifactId>maven-dependency-plugin</artifactId>
               <version>2.8</version>
            </plugin>
            <plugin>
               <artifactId>maven-release-plugin</artifactId>
               <version>2.5.3</version>
            </plugin>
         </plugins>
      </pluginManagement>
      <plugins>
         <plugin>
            <artifactId>maven-clean-plugin</artifactId>
            <version>2.5</version>
            <executions>
               <execution>
               <id>default-clean</id>
               <phase>clean</phase>
               <goals>
                  <goal>clean</goal>
               </goals>
               </execution>
            </executions>
         </plugin>
         <plugin>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.6</version>
            <executions>
               <execution>
                  <id>default-testResources</id>
                  <phase>process-test-resources</phase>
                  <goals>
                     <goal>testResources</goal>
                  </goals>
               </execution>
               <execution>
                  <id>default-resources</id>
                  <phase>process-resources</phase>
                  <goals>
                     <goal>resources</goal>
                  </goals>
               </execution>
            </executions>
         </plugin>
         <plugin>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.4</version>
            <executions>
               <execution>
                  <id>default-jar</id>
                  <phase>package</phase>
                  <goals>
                     <goal>jar</goal>
                  </goals>
               </execution>
            </executions>
         </plugin>
         <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <executions>
               <execution>
                  <id>default-compile</id>
                  <phase>compile</phase>
                  <goals>
                     <goal>compile</goal>
                  </goals>
               </execution>
               <execution>
                  <id>default-testCompile</id>
                  <phase>test-compile</phase>
                  <goals>
                     <goal>testCompile</goal>
                  </goals>
               </execution>
            </executions>
         </plugin>
         <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.12.4</version>
            <executions>
               <execution>
                  <id>default-test</id>
                  <phase>test</phase>
                  <goals>
                     <goal>test</goal>
                  </goals>
               </execution>
            </executions>
         </plugin>
         <plugin>
            <artifactId>maven-install-plugin</artifactId>
            <version>2.4</version>
            <executions>
               <execution>
                  <id>default-install</id>
                  <phase>install</phase>
                  <goals>
                     <goal>install</goal>
                  </goals>
               </execution>
            </executions>
         </plugin>
         <plugin>
            <artifactId>maven-deploy-plugin</artifactId>
            <version>2.7</version>
            <executions>
               <execution>
                  <id>default-deploy</id>
                  <phase>deploy</phase>
                  <goals>
                     <goal>deploy</goal>
                  </goals>
               </execution>
            </executions>
         </plugin>
         <plugin>
            <artifactId>maven-site-plugin</artifactId>
            <version>3.3</version>
            <executions>
               <execution>
                  <id>default-site</id>
                  <phase>site</phase>
                  <goals>
                     <goal>site</goal>
                  </goals>
                  <configuration>
                     <outputDirectory>C:\MVN\target\site</outputDirectory>
                     <reportPlugins>
                        <reportPlugin>
                           <groupId>org.apache.maven.plugins</groupId>
                           <artifactId>maven-project-info-reports-plugin</artifactId>
                        </reportPlugin>
                     </reportPlugins>
                  </configuration>
               </execution>
               <execution>
                  <id>default-deploy</id>
                  <phase>site-deploy</phase>
                  <goals>
                     <goal>deploy</goal>
                  </goals>
                  <configuration>
                     <outputDirectory>C:\MVN\target\site</outputDirectory>
                     <reportPlugins>
                        <reportPlugin>
                           <groupId>org.apache.maven.plugins</groupId>
                           <artifactId>maven-project-info-reports-plugin</artifactId>
                        </reportPlugin>
                     </reportPlugins>
                  </configuration>
               </execution>
            </executions>
            <configuration>
               <outputDirectory>C:\MVN\target\site</outputDirectory>
               <reportPlugins>
                  <reportPlugin>
                     <groupId>org.apache.maven.plugins</groupId>
                     <artifactId>maven-project-info-reports-plugin</artifactId>
                  </reportPlugin>
               </reportPlugins>
            </configuration>
         </plugin>
      </plugins>
   </build>
   <reporting>
      <outputDirectory>C:\MVN\target\site</outputDirectory>
   </reporting>
</project>

在上面的 pom.xml 中,您可以看到預設的專案原始檔夾結構、輸出目錄、所需的外掛、儲存庫、報表目錄,Maven 在執行所需目標時將使用這些目錄。

Maven pom.xml 不需要手動編寫。Maven 提供了許多原型外掛來建立專案,這些外掛依次建立專案結構和 pom.xml。

廣告