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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion><!--maven2.0必須是這樣寫,現在是maven2唯一支持的版本-->
<!-- The Basics -->
<groupId>...</groupId> <!--指定組名,例如:org.apache.maven-->
<artifactId>...</artifactId> <!--指定工程名例如:appfuse-->
<version>...</version> <!--指定版本號
<packaging>...</packaging> <!--The current core packaging values are: pom, jar, maven-plugin, ejb, war, ear, rar, par-->
<classifier>...</classifier> <!--projects are displayed as groupId:artifactId:packaging:classifier:version-->

<name>...</name> <!--一些無關太重要的東西,用戶描述你的項目的名字,可選的-->

<url>...</url> <!--暫時不知何物,貌似無關重要,只是寫明開發團隊的網站,可選的-->
<!--
這裏有些東西暫時不談-->
<dependencies>...</dependencies> <!--例子:    <dependency>

      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.0</version>
      <type>jar</type>
      <scope>test</scope>
      <optional>true</optional>
    </dependency>
groupId,artifactId和version這個三組合標示依賴的具體工程,而且這個依賴工程必須是maven中心包管理範圍內的。如果碰上非開源包,maven支持不了這個包,那麼則有三種方法處理:1.本地安裝這個插件install plugin例如:
mvn install:install-file -Dfile=non-maven-proj.jar -DgroupId=some.group -DartifactId=non-maven-proj -Dversion=1

2.創建自己的Repositories並且部署這個包,使用類似上面的deploy:deploy-file 命令3.設置scope爲system,並且指定系統路徑

dependency裏面的classifier,用於區分從同一個pom編譯出來的但是內容不同的同名包,例如同一個工程編譯出兩個artifact,一個支持jdk1.5一個支持jdk1.4,那麼就可以使用這個來命名爲jdk15和jdk14來區分,它如果出現在包名中,那麼它必須跟在版本號後。還有一種情況是將一個工程的一些次要artifact附到主要artifact中,就可以使用這個來區分,例如一個工程產生source,javadoc,class三種東西,那麼就可以使用不同的classifier來分別標識這些東西 
dependency裏面的type,默認爲jar,類型,常用如:jar,ejb-client,test-jar,可以設置plugins中的extensions值爲true後在增加新類型
dependency裏面的scope,指定classpath,可以爲:compile(默認的,compile scope在所有classpaths內有效,這些dependencies將會傳播到項目中。provided:指示jdk或者某個容器可以提供他,它只在compilation和test的classpaths有效,而且不會傳播的。runtime:指示這個dependency在編譯過程是不必要的,但是執行需要,在test和runtime的classpaths有效,在compile的classpaths無效。test:指示這個dependency在一般程序運行是無效的,但是在test的compilation和execution是有效的,system則跟provided類似,但是這種dependency必須人工明確地制定。這種依賴不會在repository中查找。
dependency裏面的systemPath:只在dependency的scope聲明爲system的時候纔有用除,否則,build的過程將會失敗。路徑必須是絕對的,所以最好使用property來聲明機器的特定路徑。
dependency裏面的optional:如果工程本身是一個dependency那麼就標記爲optional,例如X需要A,A需要B,那麼X只需要optional的B,則B在X中就是optional聲明的了
dependency裏面的exclusions:如果X需要A,A包含B依賴,那麼X可以聲明不要B依賴,只要在exclusions中聲明exclusion。optional是不會install或者使用B,而exclusion是將B從依賴樹中是刪除。例如appfuse不想使用hibernate,但是appfuse是集成hibernate的,所以就排除掉:
            <exclusions>
                <exclusion>
                    <groupId>org.appfuse</groupId>
                    <artifactId>appfuse-hibernate</artifactId>
                </exclusion>
            </exclusions>-->

<!--Inheritance:如果一個工程是pareent或者aggregation(即mutil-module的)的,那麼必須在packaging賦值pom。child工程從parent繼承的包括:dependencies,developers and contributors,plugin lists,reports lists,plugin execution with matching ids,plugin configuration-->
<parent>...</parent> <!--參照下面例子:relativePath是可選的,maven會首先搜索這個地址,在搜索本地和遠程repositories之前
  <parent>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>my-parent</artifactId>
    <version>2.0</version>
    <relativePath>../my-parent</relativePath>
  </parent>
-->
 <dependencyManagement>...</dependencyManagement>  <!--用於幫助管理children的dependencies。例如如果parent使用dependencyManagement定義一個dependencyon junit:junit:4.0,那麼它的children就可以只引用groupId和artifactId,而version就可以通過parent來設置。好處就是集中管理依賴詳情-->
<modules>...</modules><!--對於多模塊project,outer-module沒有必要考慮inner-module的dependencies,當列出modules的時候。modules的順序是不重要的,因爲maven會自動根據依賴關係來拓撲排序,modules例子:
    <module>my-project<module>
    <module>another-project<module>
-->
<properties>...</properties> <!--參照http://www.blogjava.net/jianyue/articles/maven2_setting.html,是一樣的-->
<!-- Build Settings --><build>...</build> <!--
defaultGoal默認的目標,必須跟命令行上的參數相同例如jar:jar,或者與時期(parse)相同,例如install
directory指定build target目標的目錄,默認爲${basedir}/target,即項目根目錄下的target
finalName指定去掉後綴名後的工程名字,例如:默認爲${artifactId}-${version}
filters:用於定義指定filter屬性文件位置,例如filter元素賦值filters/filter1.properties,那麼這個文件裏面就可以定義name=value對,這個name=value對的值就可以在工程pom中通過${name}引用,默認的filter目錄是${basedir}/src/main/filters/
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>
targetPath:指定build資源到哪個目的目錄,默認是base directory
filtering:指定是否將filter文件(即上面說的filters裏定義的*.property文件)的變量值在這個resource文件有效,例如上面就指定那些變量值在configuration文件無效
directory:指定屬性文件的目錄,build的過程需要找到它,並且將其放到targetPath下。默認的directory是${basedir}/src/main/resources
includes:指定包含文件的patterns,符合樣式並且在directory目錄下的文件將會是包含進project的資源文件
excludes:指定不包含在內的patterns,如果includes與excludes有衝突,那麼excludes勝利,那些符合衝突樣式的文件還是不會包含進來的
testResources這個模塊包含測試資源元素,其內容定義與resources類似。不同的一點是默認的測試資源路徑是${basefir}/src/test/resources,測試資源是不部署的。
-->
<plugins>...</plugins> <!--
<plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.0</version>
        <extensions>false</extensions>
        <inherited>true</inherited>
        <configuration>
          <classifier>test</classifier>
        </configuration>
        <dependencies>...</dependencies>
        <executions>...</executions>
      </plugin>

extensions:true or false,決定是否要load這個plugin的extensions
inherited:是否讓子pom繼承true or false
configuration:通常用於私有不開源的plugin,不能夠詳細瞭解plugin的內部工作原理,但使plugin滿足需要滿足的properties
dependencies:與pom基礎的dependencies的結構和功能都相同,只是plugin的dependencies用於plugin,而pom的dependencies用於本身這個工程,在plugin的dependencies主要用於改變plugin原來的dependencies,例如排除一些用不到的dependency或者修改dependency的版本等,詳細請看pom基礎的dependencies
executions:plugin也有很多個目標,每個目標具有不同的配置,executions就是設定plugin的目標
          <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>
id:標識符
goals:裏面列出一系列的goal元素,例如上面的run goal
phase:聲明goals執行的時期,例如:verify
inherited:是否傳遞execution到子pom
configuration:設置execution下列表的goals 的設置,而不是plugin所有goals的設置
plugin Management: 用於管理plugin,與pom build裏的plugins區別是,這裏的plugin是列出來,然後讓子pom來決定是否引用的,例如後面的引用方法。
<pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-jar-plugin</artifactId>
          <version>2.2</version>
          <executions>
            <execution>
              <id>pre-process-classes</id>
              <phase>compile</phase>
              <goals>
                <goal>jar</goal>
              </goals>
              <configuration>
                <classifier>pre-process</classifier>
              </configuration>
            </execution>
          </executions>
        </plugin>
      </plugins>
    </pluginManagement>
子pom引用方法:
在pom的build裏的plugins引用:
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
      </plugin>
    </plugins>
build 裏面的Directories
    <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>
這幾個元素只在parent bulid element裏面定義,他們設置多種路徑結構,他們並不在profile裏,所以不能通過profile來修改
build 裏面的Extensions
它們是一系列build過程中要使用的產品,他們會包含在running bulid‘s classpath裏面。他們可以開啓extensions,也可以通過提供條件來激活plugins。簡單來講,extensions是在build過程被激活的產品
    <extensions>
      <extension>
        <groupId>org.apache.maven.wagon</groupId>
        <artifactId>wagon-ftp</artifactId>
        <version>1.0-alpha-3</version>
      </extension>
    </extensions>
-->
 <reporting>...</reporting> <!--
reporting包含site生成階段的一些元素,某些maven  plugin可以生成reports並且在repoting下配置。例如javadoc,maven site等,在reporting下配置reprot plugin的方法與build幾乎一樣,最不同的是:build的plug-in goals在executions下設置,而reporting的configures goals在reportset。更微妙的不同是reporting下的plugin configuration works as a build plugin configuration,但是相反是不對的(即build plugin configuration does not affect a reporting plugin)。
excludeDefaults:是否排除site generator默認產生的reports
outpoutDirectory,默認的dir變成:${basedir}/target/site 
Report sets:設置execution goals,相當於build裏面的executions。不同的是不能夠bind a report to another phase,只能夠是site
<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>
reporting裏面的厄reportSets和build裏面的executions的作用都是控制pom的不同粒度去控制build的過程,我們不單要配置plugins,還要配置那些plugins單獨的goals。
-->
<!-- More Project Information -->
<description>...</description> 
project的描述
<inceptionYear>...</inceptionYear> 
工程的初始時間
<licenses>...</licenses>
<licenses>
  <license>
    <name>Apache 2</name>
    <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
    <distribution>repo</distribution>
    <comments>A business-friendly OSS license</comments>
  </license>
</licenses>
<!--列出本工程直接的licenses,而不要列出dependencies的licenses,

name, url and comments: are self explanatory, and have been encountered before in other capacities. The fourth license element is: distribution: This describes how the project may be legally distributed. The two stated methods are repo (they may be downloaded from a Maven repository) or manual (they must be manually installed).

-->
<organization>...</organization>
<!--
   <organization>
    <name>Codehaus Mojo</name>
    <url>http://mojo.codehaus.org</url>
  </organization>
很多工程都受到某些組織運行,這裏設置基本信息
-->
<developers>...</developers>
<!--例如:一個開發者可以有多個roles,properties是
  <developers>
    <developer>
      <id>eric</id>
      <name>Eric</name>
      <email>[email protected]</email>
      <url>http://eric.propellors.net</url>
      <organization>Codehaus</organization>
      <organizationUrl>http://mojo.codehaus.org</organizationUrl>
      <roles>
        <role>architect</role>
        <role>developer</role>
      </roles>
      <timezone>-6</timezone>
      <properties>
        <picUrl>http://tinyurl.com/prv4t</picUrl>
      </properties>
    </developer>
  </developers>
-->
<contributors>...</contributors>
<!--跟developer差不多,只是contributors是副的工作人員,不過良好工程應該需要更多的contributors而不是developer,例如:
  <contributors>
    <contributor>
      <name>Noelle</name>
      <email>[email protected]</email>
      <url>http://noellemarie.com</url>
      <organization>Noelle Marie</organization>
      <organizationUrl>http://noellemarie.com</organizationUrl>
      <roles>
        <role>tester</role>
      </roles>
      <timezone>-5</timezone>
      <properties>
        <gtalk>[email protected]</gtalk>
      </properties>
    </contributor>
  </contributors>
-->
<!-- Environment Settings --> <issueManagement>...</issueManagement>
 <!--定義defect tracking system缺陷跟蹤系統,比如有(bugzilla,testtrack,clearquest等),例如:
  <issueManagement>
    <system>Bugzilla</system>
    <url>http://127.0.0.1/bugzilla/</url>
  </issueManagement>
-->
<ciManagement>...</ciManagement> 
<!--Continuous Integration Management:設置自動build系統,一些集成程序包括continuum,Cruise control等。例如:

<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>[email protected]</address></configuration>
      </notifier>
    </notifiers>
  </ciManagement>

maven捕獲一些經常重發生的配置,在notifier元素裏配置。A notifier is the manner in which people are notified of certain build statuses. In the following example, this POM is setting a notifier of type mail (meaning email), and configuring the email address to use on the specified triggers sendOnErrorsendOnFailure, and not sendOnSuccess or sendOnWarning.
-->
<mailingLists>...</mailingLists>
<!--例如:
  <mailingLists>
    <mailingList>
      <name>User List</name>
      <subscribe>[email protected]</subscribe>
      <unsubscribe>[email protected]</unsubscribe>
      <post>[email protected]</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>
看不懂解釋啊,照搬吧:

subscribeunsubscribe: There elements specify the email addresses which are used for performing the relative actions To subscribe to the user list above, a user would send an email to [email protected]. archive: This element specifies the url of the archive of old mailing list emails, if one exists. If there are mirrored archives, they can be specified under otherArchives. post: The email address which one would use in order to post to the mailing list. Note that not all mailing lists have the ability to post to (such as a build failure list). 
-->
<scm>...</scm>

<!--例如:
  <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>
connection, developerConnection: 都是連接字符串,其中後者是具有write權限的scm連接,常用的scm工具包括cvs與subversion,還有其他scms,url的字符串格式是:scm:[provider]:[provider_specific],例如cvs的是scm:cvs:pserver:127.0.0.1:/cvs/root:my-project
tag:說明project所在的目錄tag,默認是HEAD,表示根目錄
url:公開的可瀏覽repository
-->

<prerequisites>...</prerequisites>
 <!--首要條件,如果不滿足,maven會在事件開始之前失敗,在pom4.0,唯一的首要條件是maven元素-->
<repositories>...</repositories>
<!--要成爲maven2的repository artifact,必須具有pom文件在$BASE_REPO/groupId/artifactId/version/artifactId-version.pom
BASE_REPO可以是本地,也可以是遠程的。repository元素就是聲明那些去查找的repositories
默認的central Maven repository在http://repo1.maven.org/maven2/
<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>
release和snapshots:是artifact的兩種policies,pom可以選擇那種政策有效。
enable:本別指定兩種類型是否可用,true or false
updatePolicy:說明更新發生的頻率always 或者 never 或者 daily(默認的)或者 interval:X(X是分鐘數)
checksumPolicy:When Maven deploys files to the repository, it also deploys corresponding checksum files. Your options are to ignorefail, or warn on missing or incorrect checksums.
layout:maven1.x與maven2有不同的layout,所以可以聲明爲default或者是legacy(遺留方式maven1.x)。
-->
 <pluginRepositories>...</pluginRepositories>
<!--與Repositories具有類似的結構,只是Repositories是dependencies的home,而這個是plugins 的home。-->
<distributionManagement>...</distributionManagement>
<!--管理distribution和supporting files。
downloadUrl:是其他項目爲了抓取本項目的pom’s artifact而指定的url,就是說告訴pom upload的地址也就是別人可以下載的地址。
status:這裏的狀態不要受到我們的設置,maven會自動設置project的狀態,有效的值:none:沒有聲明狀態,pom默認的;converted:本project是管理員從原先的maven版本convert到maven2的;partner:以前叫做synched,意思是與partner repository已經進行了同步;deployed:至今爲止最經常的狀態,意思是製品是從maven2 instance部署的,人工在命令行deploy的就會得到這個;verified:本製品已經經過驗證,也就是已經定下來了最終版。
repository:聲明deploy過程中current project會如何變成repository,說明部署到repository的信息。
    <repository>
      <uniqueVersion>false</uniqueVersion>
      <id>corp1</id>
      <name>Corporate Repository</name>
      <url>scp://repo1/maven2</url>
      <layout>default</layout>
    </repository>
    <snapshotRepository>
      <uniqueVersion>true</uniqueVersion>
      <id>propSnap</id>
      <name>Propellors Snapshots</name>
      <url>sftp://propellers.net/maven</url>
      <layout>legacy</layout>
    </snapshotRepository>
id, name::唯一性的id,和可讀性的name
uniqueVersion:指定是否產生一個唯一性的version number還是使用address裏的其中version部分。true or false
url:說明location和transport protocol
layout:default或者legacy-->
<site><!---聲明如何部署project‘s 的site和document->
<!--例如:
    <site>
      <id>mojo.website</id>
      <name>Mojo Website</name>
      <url>scp://beaver.codehaus.org/home/projects/mojo/public_html/</url>
    </site>
與上面repository的元素相同意思
-->
Relocation
<!-- 說明工程的變更,在這裏警告使用者當心工程被重命名了等信息。重新指定id和名稱,並且寫個message註明備註   <relocation>
      <groupId>org.apache</groupId>
      <artifactId>my-project</artifactId>
      <version>1.0</version>
      <message>We have moved the Project under Apache</message>
    </relocation>
-->
<profiles>...</profiles>
<!--pom4.0的一個新特性就是具有根據environment來修改設置的能力。

它包含可選的activation(profile的觸發器)和一系列的changes。例如test過程可能會指向不同的數據庫(相對最終的deployment)或者不同的dependencies或者不同的repositories,並且是根據不同的JDK來改變的。那麼結構如下:

  <profiles>
    <profile>
      <id>test</id>
      <activation>...</activation>
      <build>...</build>
      <modules>...</modules>
      <repositories>...</repositories>
      <pluginRepositories>...</pluginRepositories>
      <dependencies>...</dependencies>
      <reporting>...</reporting>
      <dependencyManagement>...</dependencyManagement>
      <distributionManagement>...</distributionManagement>
    </profile>
  </profiles>
Activation
觸發這個profile的條件配置如下例:(只需要其中一個成立就可以激活profile,如果第一個條件滿足了,那麼後面就不會在進行匹配。
    <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>mavenVersion</name>
          <value>2.0.3</value>
        </property>
        <file>
          <exists>${basedir}/file2.properties</exists>
          <missing>${basedir}/file1.properties</missing>
        </file>
      </activation>

-->
激活profile的方法有多個:setting文件的activeProfile元素明確指定激活的profile的ID,在命令行上明確激活Profile用-P flag 參數
查看某個build會激活的profile列表可以用:mvn help:active-profiles
</project>

發佈了7 篇原創文章 · 獲贊 2 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章