Maven 教程之 pom.xml 詳解


pom.xml 就是 maven 的配置文件,用以描述項目的各種信息。

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>

  <!-- The Basics -->
  <groupId>...</groupId>
  <artifactId>...</artifactId>
  <version>...</version>
  <packaging>...</packaging>
  <dependencies>...</dependencies>
  <parent>...</parent>
  <dependencyManagement>...</dependencyManagement>
  <modules>...</modules>
  <properties>...</properties>

  <!-- Build Settings -->
  <build>...</build>
  <reporting>...</reporting>

  <!-- More Project Information -->
  <name>...</name>
  <description>...</description>
  <url>...</url>
  <inceptionYear>...</inceptionYear>
  <licenses>...</licenses>
  <organization>...</organization>
  <developers>...</developers>
  <contributors>...</contributors>

  <!-- Environment Settings -->
  <issueManagement>...</issueManagement>
  <ciManagement>...</ciManagement>
  <mailingLists>...</mailingLists>
  <scm>...</scm>
  <prerequisites>...</prerequisites>
  <repositories>...</repositories>
  <pluginRepositories>...</pluginRepositories>
  <distributionManagement>...</distributionManagement>
  <profiles>...</profiles>
</project>

基本配置

  • project - project 是 pom.xml 中描述符的根。
  • modelVersion - modelVersion 指定 pom.xml 符合哪個版本的描述符。maven 2 和 3 只能為 4.0.0。

一般 jar 包被識別為: groupId:artifactId:version 的形式。

<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>org.codehaus.mojo</groupId>
  <artifactId>my-project</artifactId>
  <version>1.0</version>
  <packaging>war</packaging>
</project>

maven 坐標

在 maven 中,根據 groupIdartifactIdversion 組合成 groupId:artifactId:version來唯一識別一個 jar 包。

  • groupId - 團體、組織的標識符。團體標識的約定是,它以創建這個項目的組織名稱的逆向域名(reverse domain name)開頭。一般對應着 java 的包結構。
  • artifactId - 單獨項目的唯一標識符。比如我們的 tomcat、commons 等。不要在 artifactId 中包含點號(.)。
  • version - 一個項目的特定版本。
  • maven 有自己的版本規范,一般是如下定義 major version、minor version、incremental version-qualifier ,比如 1.2.3-beta-01。要說明的是,maven 自己判斷版本的算法是 major、minor、incremental 部分用數字比較,qualifier 部分用字符串比較,所以要小心 alpha-2 和 alpha-15 的比較關系,最好用 alpha-02 的格式。
  • maven 在版本管理時候可以使用幾個特殊的字符串 SNAPSHOT、LATEST、RELEASE。比如 1.0-SNAPSHOT。各個部分的含義和處理邏輯如下說明:
    • SNAPSHOT - 這個版本一般用於開發過程中,表示不穩定的版本。
    • LATEST - 指某個特定構件的最新發布,這個發布可能是一個發布版,也可能是一個 snapshot 版,具體看哪個時間最后。
    • RELEASE :指最后一個發布版。
  • packaging - 項目的類型,描述了項目打包后的輸出,默認是 jar。常見的輸出類型為:pom, jar, maven-plugin, ejb, war, ear, rar, par。

依賴配置

dependencies

<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">
  ...
  <dependencies>
    <dependency>
     <groupId>org.apache.maven</groupId>
      <artifactId>maven-embedder</artifactId>
      <version>2.0</version>
      <type>jar</type>
      <scope>test</scope>
      <optional>true</optional>
      <exclusions>
        <exclusion>
          <groupId>org.apache.maven</groupId>
          <artifactId>maven-core</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
    ...
  </dependencies>
  ...
</project>
  • groupId, artifactId, version - 和基本配置中的 groupIdartifactIdversion 意義相同。
  • type - 對應 packaging 的類型,如果不使用 type 標簽,maven 默認為 jar。
  • scope - 此元素指的是任務的類路徑(編譯和運行時,測試等)以及如何限制依賴關系的傳遞性。有 5 種可用的限定范圍:
  • compile - 如果沒有指定 scope 標簽,maven 默認為這個范圍。編譯依賴關系在所有 classpath 中都可用。此外,這些依賴關系被傳播到依賴項目。
  • provided - 與 compile 類似,但是表示您希望 jdk 或容器在運行時提供它。它只適用於編譯和測試 classpath,不可傳遞。
  • runtime - 此范圍表示編譯不需要依賴關系,而是用於執行。它是在運行時和測試 classpath,但不是編譯 classpath。
  • test - 此范圍表示正常使用應用程序不需要依賴關系,僅適用於測試編譯和執行階段。它不是傳遞的。
  • system - 此范圍與 provided 類似,除了您必須提供明確包含它的 jar。該 artifact 始終可用,並且不是在倉庫中查找。
  • systemPath - 僅當依賴范圍是系統時才使用。否則,如果設置此元素,構建將失敗。該路徑必須是絕對路徑,因此建議使用 propertie 來指定特定的路徑,如\$ {java.home} / lib。由於假定先前安裝了系統范圍依賴關系,maven 將不會檢查項目的倉庫,而是檢查庫文件是否存在。如果沒有,maven 將會失敗,並建議您手動下載安裝。
  • optional - optional 讓其他項目知道,當您使用此項目時,您不需要這種依賴性才能正常工作。
  • exclusions - 包含一個或多個排除元素,每個排除元素都包含一個表示要排除的依賴關系的 groupId 和 artifactId。與可選項不同,可能或可能不會安裝和使用,排除主動從依賴關系樹中刪除自己。

parent

maven 支持繼承功能。子 POM 可以使用 parent 指定父 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
                      https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <parent>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>my-parent</artifactId>
    <version>2.0</version>
    <relativePath>../my-parent</relativePath>
  </parent>

  <artifactId>my-project</artifactId>
</project>
  • relativePath - 注意 relativePath 元素。在搜索本地和遠程存儲庫之前,它不是必需的,但可以用作 maven 的指示符,以首先搜索給定該項目父級的路徑。

dependencyManagement

dependencyManagement 是表示依賴 jar 包的聲明。即你在項目中的 dependencyManagement 下聲明了依賴,maven 不會加載該依賴,dependencyManagement 聲明可以被子 POM 繼承。

dependencyManagement 的一個使用案例是當有父子項目的時候,父項目中可以利用 dependencyManagement 聲明子項目中需要用到的依賴 jar 包,之后,當某個或者某幾個子項目需要加載該依賴的時候,就可以在子項目中 dependencies 節點只配置 groupId 和 artifactId就可以完成依賴的引用。

dependencyManagement 主要是為了統一管理依賴包的版本,確保所有子項目使用的版本一致,類似的還有pluginspluginManagement

modules

子模塊列表。

<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>org.codehaus.mojo</groupId>
  <artifactId>my-parent</artifactId>
  <version>2.0</version>
  <packaging>pom</packaging>

  <modules>
    <module>my-project</module>
    <module>another-project</module>
    <module>third-project/pom-example.xml</module>
  </modules>
</project>

properties

屬性列表。定義的屬性可以在 pom.xml 文件中任意處使用。使用方式為 ${propertie} 。

<project>
  ...
  <properties>
    <maven.compiler.source>1.7<maven.compiler.source>
    <maven.compiler.target>1.7<maven.compiler.target>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  </properties>
  ...
</project>

 

構建配置

build

build 可以分為 "project build" 和 "profile build"。

<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">
  ...
  <!-- "Project Build" contains more elements than just the BaseBuild set -->
  <build>...</build>

  <profiles>
    <profile>
      <!-- "Profile Build" contains a subset of "Project Build"s elements -->
      <build>...</build>
    </profile>
  </profiles>
</project>

基本構建配置:

<build>
  <defaultGoal>install</defaultGoal>
  <directory>${basedir}/target</directory>
  <finalName>${artifactId}-${version}</finalName>
  <filters>
    <filter>filters/filter1.properties</filter>
  </filters>
  ...
</build>

defaultGoal : 默認執行目標或階段。如果給出了一個目標,它應該被定義為它在命令行中(如 jar:jar)。如果定義了一個階段(如安裝),也是如此。

directory :構建時的輸出路徑。默認為:${basedir}/target 。

finalName :這是項目的最終構建名稱(不包括文件擴展名,例如:my-project-1.0.jar)

filter :定義 * .properties 文件,其中包含適用於接受其設置的資源的屬性列表(如下所述)。換句話說,過濾器文件中定義的“name = value”對在代碼中替換\$ {name}字符串。

resources

資源的配置。資源文件通常不是代碼,不需要編譯,而是在項目需要捆綁使用的內容。

<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">
  <build>
    ...
    <resources>
      <resource>
        <targetPath>META-INF/plexus</targetPath>
        <filtering>false</filtering>
        <directory>${basedir}/src/main/plexus</directory>
        <includes>
          <include>configuration.xml</include>
        </includes>
        <excludes>
          <exclude>**/*.properties</exclude>
        </excludes>
      </resource>
    </resources>
    <testResources>
      ...
    </testResources>
    ...
  </build>
</project>
  • resources: 資源元素的列表,每個資源元素描述與此項目關聯的文件和何處包含文件。
  • targetPath: 指定從構建中放置資源集的目錄結構。目標路徑默認為基本目錄。將要包裝在 jar 中的資源的通常指定的目標路徑是 META-INF。
  • filtering: 值為 true 或 false。表示是否要為此資源啟用過濾。請注意,該過濾器 * .properties 文件不必定義為進行過濾 - 資源還可以使用默認情況下在 POM 中定義的屬性(例如\$ {project.version}),並將其傳遞到命令行中“-D”標志(例如,“-Dname = value”)或由 properties 元素顯式定義。過濾文件覆蓋上面。
  • directory: 值定義了資源的路徑。構建的默認目錄是${basedir}/src/main/resources
  • includes: 一組文件匹配模式,指定目錄中要包括的文件,使用*作為通配符。
  • excludes: 與 includes 類似,指定目錄中要排除的文件,使用*作為通配符。注意:如果 include 和 exclude 發生沖突,maven 會以 exclude 作為有效項。
  • testResources: testResources 與 resources 功能類似,區別僅在於:testResources 指定的資源僅用於 test 階段,並且其默認資源目錄為:${basedir}/src/test/resources 。

plugins

<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">
  <build>
    ...
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.6</version>
        <extensions>false</extensions>
        <inherited>true</inherited>
        <configuration>
          <classifier>test</classifier>
        </configuration>
        <dependencies>...</dependencies>
        <executions>...</executions>
      </plugin>
    </plugins>
  </build>
</project>
  • groupId, artifactId, version :和基本配置中的 groupIdartifactIdversion 意義相同。
  • extensions :值為 true 或 false。是否加載此插件的擴展名。默認為 false。
  • inherited :值為 true 或 false。這個插件配置是否應該適用於繼承自這個插件的 POM。默認值為 true。
  • configuration - 這是針對個人插件的配置,這里不擴散講解。
  • dependencies :這里的 dependencies 是插件本身所需要的依賴。
  • executions :需要記住的是,插件可能有多個目標。每個目標可能有一個單獨的配置,甚至可能將插件的目標完全綁定到不同的階段。執行配置插件的目標的執行。
  • id: 執行目標的標識。
  • goals: 像所有多元化的 POM 元素一樣,它包含單個元素的列表。在這種情況下,這個執行塊指定的插件目標列表。
  • phase: 這是執行目標列表的階段。這是一個非常強大的選項,允許將任何目標綁定到構建生命周期中的任何階段,從而改變 maven 的默認行為。
  • inherited: 像上面的繼承元素一樣,設置這個 false 會阻止 maven 將這個執行傳遞給它的子代。此元素僅對父 POM 有意義。
  • configuration: 與上述相同,但將配置限制在此特定目標列表中,而不是插件下的所有目標。
  • <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">
      ...
      <build>
        <plugins>
          <plugin>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>1.1</version>
            <executions>
              <execution>
                <id>echodir</id>
                <goals>
                  <goal>run</goal>
                </goals>
                <phase>verify</phase>
                <inherited>false</inherited>
                <configuration>
                  <tasks>
                    <echo>Build Dir: ${project.build.directory}</echo>
                  </tasks>
                </configuration>
              </execution>
            </executions>
    
          </plugin>
        </plugins>
      </build>
    </project>

    pluginManagement

    與 dependencyManagement 很相似,在當前 POM 中僅聲明插件,而不是實際引入插件。子 POM 中只配置 groupId 和 artifactId 就可以完成插件的引用,且子 POM 有權重寫 pluginManagement 定義。

    它的目的在於統一所有子 POM 的插件版本。

    directories

  • <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">
      ...
      <build>
        <sourceDirectory>${basedir}/src/main/java</sourceDirectory>
        <scriptSourceDirectory>${basedir}/src/main/scripts</scriptSourceDirectory>
        <testSourceDirectory>${basedir}/src/test/java</testSourceDirectory>
        <outputDirectory>${basedir}/target/classes</outputDirectory>
        <testOutputDirectory>${basedir}/target/test-classes</testOutputDirectory>
        ...
      </build>
    </project>

    目錄元素集合存在於 build 元素中,它為整個 POM 設置了各種目錄結構。由於它們在配置文件構建中不存在,所以這些不能由配置文件更改。

    如果上述目錄元素的值設置為絕對路徑(擴展屬性時),則使用該目錄。否則,它是相對於基礎構建目錄:${basedir}

    extensions

    擴展是在此構建中使用的 artifacts 的列表。它們將被包含在運行構建的 classpath 中。它們可以啟用對構建過程的擴展(例如為 Wagon 傳輸機制添加一個 ftp 提供程序),並使活動的插件能夠對構建生命周期進行更改。簡而言之,擴展是在構建期間激活的 artifacts。擴展不需要實際執行任何操作,也不包含 Mojo。因此,擴展對於指定普通插件接口的多個實現中的一個是非常好的。

  • <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">
      ...
      <build>
        ...
        <extensions>
          <extension>
            <groupId>org.apache.maven.wagon</groupId>
            <artifactId>wagon-ftp</artifactId>
            <version>1.0-alpha-3</version>
          </extension>
        </extensions>
        ...
      </build>
    </project>

    reporting

    報告包含特定針對 site 生成階段的元素。某些 maven 插件可以生成 reporting 元素下配置的報告,例如:生成 javadoc 報告。reporting 與 build 元素配置插件的能力相似。明顯的區別在於:在執行塊中插件目標的控制不是細粒度的,報表通過配置 reportSet 元素來精細控制。

    而微妙的區別在於 reporting 元素下的 configuration 元素可以用作 build 下的 configuration ,盡管相反的情況並非如此( build 下的 configuration 不影響 reporting元素下的 configuration )。

    另一個區別就是 plugin 下的 outputDirectory 元素。在報告的情況下,默認輸出目錄為 ${basedir}/target/site

  • <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">
      ...
      <reporting>
        <plugins>
          <plugin>
            ...
            <reportSets>
              <reportSet>
                <id>sunlink</id>
                <reports>
                  <report>javadoc</report>
                </reports>
                <inherited>true</inherited>
                <configuration>
                  <links>
                    <link>http://java.sun.com/j2se/1.5.0/docs/api/</link>
                  </links>
                </configuration>
              </reportSet>
            </reportSets>
          </plugin>
        </plugins>
      </reporting>
      ...
    </project>

    項目信息

    項目信息相關的這部分標簽都不是必要的,也就是說完全可以不填寫。

    它的作用僅限於描述項目的詳細信息。

    下面的示例是項目信息相關標簽的清單:

  • <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">
      ...
    
      <!-- 項目信息 begin -->
    
      <!--項目名-->
      <name>maven-notes</name>
    
      <!--項目描述-->
      <description>maven 學習筆記</description>
    
      <!--項目url-->
      <url>https://github.com/dunwu/maven-notes</url>
    
      <!--項目開發年份-->
      <inceptionYear>2017</inceptionYear>
    
      <!--開源協議-->
      <licenses>
        <license>
          <name>Apache License, Version 2.0</name>
          <url>https://www.apache.org/licenses/LICENSE-2.0.txt</url>
          <distribution>repo</distribution>
          <comments>A business-friendly OSS license</comments>
        </license>
      </licenses>
    
      <!--組織信息(如公司、開源組織等)-->
      <organization>
        <name>...</name>
        <url>...</url>
      </organization>
    
      <!--開發者列表-->
      <developers>
        <developer>
          <id>victor</id>
          <name>Zhang Peng</name>
          <email>forbreak at 163.com</email>
          <url>https://github.com/dunwu</url>
          <organization>...</organization>
          <organizationUrl>...</organizationUrl>
          <roles>
            <role>architect</role>
            <role>developer</role>
          </roles>
          <timezone>+8</timezone>
          <properties>...</properties>
        </developer>
      </developers>
    
      <!--代碼貢獻者列表-->
       <contributors>
        <contributor>
          <!--標簽內容和<developer>相同-->
        </contributor>
      </contributors>
    
      <!-- 項目信息 end -->
    
      ...
    </project>

    這部分標簽都非常簡單,基本都能做到顧名思義,且都屬於可有可無的標簽,所以這里僅簡單介紹一下:

    • name - 項目完整名稱
    • description - 項目描述
    • url - 一般為項目倉庫的 host
    • inceptionYear - 開發年份
    • licenses - 開源協議
    • organization - 項目所屬組織信息
    • developers - 項目開發者列表
    • contributors - 項目貢獻者列表, 的子標簽和 的完全相同。

    環境配置

    issueManagement

    這定義了所使用的缺陷跟蹤系統(Bugzilla,TestTrack,ClearQuest 等)。雖然沒有什么可以阻止插件使用這些信息的東西,但它主要用於生成項目文檔。

  • <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">
      ...
      <issueManagement>
        <system>Bugzilla</system>
        <url>http://127.0.0.1/bugzilla/</url>
      </issueManagement>
      ...
    </project>

    ciManagement

    CI 構建系統配置,主要是指定通知機制以及被通知的郵箱。

  • <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">
      ...
      <ciManagement>
        <system>continuum</system>
        <url>http://127.0.0.1:8080/continuum</url>
        <notifiers>
          <notifier>
            <type>mail</type>
            <sendOnError>true</sendOnError>
            <sendOnFailure>true</sendOnFailure>
            <sendOnSuccess>false</sendOnSuccess>
            <sendOnWarning>false</sendOnWarning>
            <configuration><address>continuum@127.0.0.1</address></configuration>
          </notifier>
        </notifiers>
      </ciManagement>
      ...
    </project>

     

  • mailingLists

    郵件列表

  • <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">
      ...
      <mailingLists>
        <mailingList>
          <name>User List</name>
          <subscribe>user-subscribe@127.0.0.1</subscribe>
          <unsubscribe>user-unsubscribe@127.0.0.1</unsubscribe>
          <post>user@127.0.0.1</post>
          <archive>http://127.0.0.1/user/</archive>
          <otherArchives>
            <otherArchive>http://base.google.com/base/1/127.0.0.1</otherArchive>
          </otherArchives>
        </mailingList>
      </mailingLists>
      ...
    </project>

     

  • scm

    SCM(軟件配置管理,也稱為源代碼/控制管理或簡潔的版本控制)。常見的 scm 有 svn 和 git 

  • <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">
      ...
      <scm>
        <connection>scm:svn:http://127.0.0.1/svn/my-project</connection>
        <developerConnection>scm:svn:https://127.0.0.1/svn/my-project</developerConnection>
        <tag>HEAD</tag>
        <url>http://127.0.0.1/websvn/my-project</url>
      </scm>
      ...
    </project>

    prerequisites

    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
                          https://maven.apache.org/xsd/maven-4.0.0.xsd">
      ...
      <prerequisites>
        <maven>2.0.6</maven>
      </prerequisites>
      ...
    </project>

     

  • repositories

    repositories 是遵循 Maven 存儲庫目錄布局的 artifacts 集合。默認的 Maven 中央存儲庫位於上。

  • <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">
      ...
      <repositories>
        <repository>
          <releases>
            <enabled>false</enabled>
            <updatePolicy>always</updatePolicy>
            <checksumPolicy>warn</checksumPolicy>
          </releases>
          <snapshots>
            <enabled>true</enabled>
            <updatePolicy>never</updatePolicy>
            <checksumPolicy>fail</checksumPolicy>
          </snapshots>
          <id>codehausSnapshots</id>
          <name>Codehaus Snapshots</name>
          <url>http://snapshots.maven.codehaus.org/maven2</url>
          <layout>default</layout>
        </repository>
      </repositories>
      <pluginRepositories>
        ...
      </pluginRepositories>
      ...
    </project>

    pluginRepositories

    與 repositories 差不多。

  • <project xmlns="http://maven.apache.org/POM/4.0.0"
      xmlns:xsi="http://www.w3.org/001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                          https://maven.apache.org/xsd/maven-4.0.0.xsd">
      ...
      <distributionManagement>
        ...
        <downloadUrl>http://mojo.codehaus.org/my-project</downloadUrl>
        <status>deployed</status>
      </distributionManagement>
      ...
    </project>

    distributionManagement

    它管理在整個構建過程中生成的 artifact 和支持文件的分布。從最后的元素開始:

  • <project xmlns="http://maven.apache.org/POM/4.0.0"
      xmlns:xsi="http://www.w3.org/001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                          https://maven.apache.org/xsd/maven-4.0.0.xsd">
      ...
      <distributionManagement>
        ...
        <downloadUrl>http://mojo.codehaus.org/my-project</downloadUrl>
        <status>deployed</status>
      </distributionManagement>
      ...
    </project>
    • repository - 與 repositories 相似
    • site - 站點信息
    • relocation - 項目遷移位置

    profiles

    activation 是一個 profile 的關鍵。配置文件的功能來自於在某些情況下僅修改基本 POM 的功能。這些情況通過 activation 元素指定。

  • <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">
      ...
      <profiles>
        <profile>
          <id>test</id>
          <activation>
            <activeByDefault>false</activeByDefault>
            <jdk>1.5</jdk>
            <os>
              <name>Windows XP</name>
              <family>Windows</family>
              <arch>x86</arch>
              <version>5.1.2600</version>
            </os>
            <property>
              <name>sparrow-type</name>
              <value>African</value>
            </property>
            <file>
              <exists>${basedir}/file2.properties</exists>
              <missing>${basedir}/file1.properties</missing>
            </file>
          </activation>
          ...
        </profile>
      </profiles>
    </project>

     

  • 參考地址:https://zhuanlan.zhihu.com/p/76874769


免責聲明!

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



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