Maven 教程之 pom.xml 詳解

:notebook: 本文已歸檔到:「bloghtml

簡介

什麼是 pom?

POM 是 Project Object Model 的縮寫,即項目對象模型。java

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

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 的形式。github

<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 包。web

  • 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 - 包含一個或多個排除元素,每一個排除元素都包含一個表示要排除的依賴關係的 groupIdartifactId。與可選項不一樣,可能或可能不會安裝和使用,排除主動從依賴關係樹中刪除本身。

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 繼承。apache

dependencyManagement 的一個使用案例是當有父子項目的時候,父項目中能夠利用 dependencyManagement 聲明子項目中須要用到的依賴 jar 包,以後,當某個或者某幾個子項目須要加載該依賴的時候,就能夠在子項目中 dependencies 節點只配置 groupIdartifactId 就能夠完成依賴的引用。api

dependencyManagement 主要是爲了統一管理依賴包的版本,確保全部子項目使用的版本一致,相似的還有pluginspluginManagementtomcat

modules

子模塊列表。dom

<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 相似,指定目錄中要排除的文件,使用*做爲通配符。注意:若是 includeexclude 發生衝突,maven 會以 exclude 做爲有效項。
  • testResources: testResourcesresources 功能相似,區別僅在於: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 中只配置 groupIdartifactId 就能夠完成插件的引用,且子 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 報告。reportingbuild 元素配置插件的能力類似。明顯的區別在於:在執行塊中插件目標的控制不是細粒度的,報表經過配置 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 - 項目貢獻者列表,<contributor> 的子標籤和 <developer> 的徹底相同。

環境配置

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 中央存儲庫位於https://repo.maven.apache.org/maven2/上。

<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>
複製代碼

參考資料

相關文章
相關標籤/搜索