Maven pom.xml詳解

原文:http://my.oschina.net/u/1187481/blog/204865 <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>    <!-- groupId: groupId:項目或者組織的唯一標誌,並且配置時生成的路徑也是由此生成,        如com.mycompany.app生成的相對路徑爲:/com/mycompany/app -->    <groupId>asia.banseon</groupId>    <!-- artifactId: 項目的通用名稱 -->    <artifactId>banseon-maven2</artifactId>    <!-- packaging: 打包的機制,如pom, jar, maven-plugin, ejb, war, ear, rar, par   -->    <packaging>jar</packaging>    <!-- version:項目的版本 -->    <version>1.0-SNAPSHOT</version>    <!-- 項目的名稱, Maven 產生的文檔用 -->    <name>banseon-maven</name>    <!-- 哪個網站可以找到這個項目,提示如果 Maven 資源列表沒有,可以直接上該網站尋找, Maven 產生的文檔用 -->    <url>http://www.baidu.com/banseon</url>    <!-- 項目的描述, Maven 產生的文檔用 -->    <description>A maven project to study maven.</description>    <!-- 開發者信息 -->    <developers>        <developer>            <id>HELLO WORLD</id>            <name>banseon</name>            <email>[email protected]</email>            <roles>                <role>Project Manager</role>                <role>Architect</role>            </roles>            <organization>demo</organization>            <organizationUrl>http://hi.baidu.com/banseon</organizationUrl>            <properties>                <dept>No</dept>            </properties>            <timezone>-5</timezone>        </developer>    </developers>    <!-- 類似 developers -->    <contributors></contributors>    <!-- 本項目相關 mail list, 用於訂閱等信息 -->    <mailingLists>        <mailingList>            <name>Demo</name>            <!-- Link mail -->            <post>[email protected]</post>            <!-- mail for subscribe the project -->            <subscribe>[email protected]</subscribe>            <!-- mail for unsubscribe the project -->            <unsubscribe>[email protected]</unsubscribe>            <archive>            http:/hi.baidu.com/banseon/demo/dev/            </archive>        </mailingList>    </mailingLists>    <!-- 項目的問題管理系統(Bugzilla, Jira, Scarab,或任何你喜歡的問題管理系統)的名稱和URL,本例爲 jira -->    <issueManagement>        <system>jira</system>        <url>http://jira.baidu.com/banseon</url>    </issueManagement>    <!-- organization information -->    <organization>        <name>demo</name>        <url>http://www.baidu.com/banseon</url>    </organization>    <!-- License -->    <licenses>        <license>            <name>Apache 2</name>            <url>http://www.baidu.com/banseon/LICENSE-2.0.txt</url>            <distribution>repo</distribution>            <comments>A business-friendly OSS license</comments>        </license>    </licenses>    <!--        - scm(software configuration management)標籤允許你配置你的代碼庫,爲Maven web站點和其它插件使用。        - 如果你正在使用CVS或Subversion,source repository頁面同樣能給如何使用代碼庫的詳細的、工具相關的指令。        - 下面是一個典型SCM的配置例子    -->    <scm>        <!-- 項目在 svn 上對應的資源 -->        <connection>            scm:svn:http://svn.baidu.com/banseon/maven/banseon/banseon-maven2-trunk(dao-trunk)        </connection>        <developerConnection>            scm:svn:http://svn.baidu.com/banseon/maven/banseon/dao-trunk        </developerConnection>        <url>http://svn.baidu.com/banseon</url>    </scm>    <!-- 用於配置分發管理,配置相應的產品發佈信息,主要用於發佈,在執行mvn deploy後表示要發佈的位置 -->    <distributionManagement>        <!-- 配置到文件系統 -->        <repository>            <id>banseon-maven2</id>            <name>banseon maven2</name>            <url>file://${basedir}/target/deploy</url>        </repository>        <!-- 使用ssh2配置 -->        <snapshotRepository>            <id>banseon-maven2</id>            <name>Banseon-maven2 Snapshot Repository</name>            <url>scp://svn.baidu.com/banseon:/usr/local/maven-snapshot</url>        </snapshotRepository>        <!-- 使用ssh2配置 -->        <site>            <id>banseon-site</id>            <name>business api website</name>            <url>                scp://svn.baidu.com/banseon:/var/www/localhost/banseon-web            </url>        </site>    </distributionManagement>    <!-- 依賴關係 -->    <dependencies>        <dependency>            <groupId>junit</groupId>            <artifactId>junit</artifactId>            <version>3.8.1</version>            <!-- scope 說明                - compile :默認範圍,用於編譯                - provided:類似於編譯,但支持你期待jdk或者容器提供,類似於classpath                - runtime: 在執行時,需要使用                - test:    用於test任務時使用                - system: 需要外在提供相應得元素。通過systemPath來取得                - systemPath: 僅用於範圍爲system。提供相應的路徑                - optional:   標註可選,當項目自身也是依賴時。用於連續依賴時使用            -->            <scope>test</scope>            <!--                - systemPath: 僅用於範圍爲system。提供相應的路徑                - optional: 標註可選,當項目自身也是依賴時。用於連續依賴時使用            -->            <!--                <type>jar</type>                <optional>true</optional>            -->        </dependency>        <!--            - 外在告訴maven你只包括指定的項目,不包括相關的依賴。此因素主要用於解決版本衝突問題            - 如下依賴表示 項目acegi-security依賴 org.springframework.XXX 項目,但我們不需要引用這些項目        -->        <dependency>            <groupId>org.acegisecurity</groupId>            <artifactId>acegi-security</artifactId>            <version>1.0.5</version>            <scope>runtime</scope>            <exclusions>                <exclusion>                    <artifactId>spring-core</artifactId>                    <groupId>org.springframework</groupId>                </exclusion>                <exclusion>                    <artifactId>spring-support</artifactId>                    <groupId>org.springframework</groupId>                </exclusion>                <exclusion>                    <artifactId>commons-logging</artifactId>                    <groupId>commons-logging</groupId>                </exclusion>                <exclusion>                    <artifactId>spring-jdbc</artifactId>                    <groupId>org.springframework</groupId>                </exclusion>                <exclusion>                    <artifactId>spring-remoting</artifactId>                    <groupId>org.springframework</groupId>                </exclusion>            </exclusions>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring</artifactId>            <version>2.5.1</version>            <scope>runtime</scope>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-web</artifactId>            <version>2.5.1</version>            <scope>runtime</scope>        </dependency>        <dependency>            <groupId>postgresql</groupId>            <artifactId>postgresql</artifactId>            <version>8.2-504.jdbc4</version>            <scope>runtime</scope>        </dependency>        <dependency>            <groupId>com.oracle</groupId>            <artifactId>ojdbc6</artifactId>            <version>11.1.0.6</version>            <scope>runtime</scope>        </dependency>    </dependencies>    <!--        - maven proxy, 本地倉庫,替代 maven.apache.org 網站 jar 列表,用戶下載時,首先尋找該站點        - 如資源找到,則下載。否則纔去 jar 列表網站。對多人團隊,可節省下載速度和個人存儲空間。    -->    <repositories>        <repository>            <id>banseon-repository-proxy</id>            <name>banseon-repository-proxy</name>            <url>http://192.168.1.169:9999/repository/</url>            <layout>default</layout>        </repository>    </repositories>

()

什麼是pom?    pom作爲項目對象模型。通過xml表示maven項目,使用pom.xml來實現。主要描述了項目:包括配置文件;開發者需要遵循的規則,缺陷管理系統,組織和licenses,項目的url,項目的依賴性,以及其他所有的項目相關因素。 快速察看:

xml 代碼

<project> < modelVersion>4.0.0modelVersion> < groupId>...<groupId> < artifactId>...<artifactId> < version>...<version> < packaging>...<packaging> < dependencies>...<dependencies> < parent>...<parent> < dependencyManagement>...<dependencyManagement> < modules>...<modules> < properties>...<properties> < build>...<build> < reporting>...<reporting> < name>...<name> < description>...<description> < url>...<url> < inceptionYear>...<inceptionYear> < licenses>...<licenses> < organization>...<organization> < developers>...<developers> < contributors>...<contributors> < issueManagement>...<issueManagement> < ciManagement>...<ciManagement> < mailingLists>...<mailingLists> < scm>...<scm> < prerequisites>...<prerequisites> < repositories>...<repositories> < pluginRepositories>...<pluginRepositories> < distributionManagement>...<distributionManagement> < profiles>...<profiles> < project>

基本內容: POM包括了所有的項目信息。 maven 相關: pom定義了最小的maven2元素,允許groupId,artifactId,version。所有需要的元素

groupId:項目或者組織的唯一標誌,並且配置時生成的路徑也是由此生成,如org.codehaus.mojo生成的相對路徑爲:/org/codehaus/mojo artifactId: 項目的通用名稱 version:項目的版本 packaging: 打包的機制,如pom, jar, maven-plugin, ejb, war, ear, rar, par classifier: 分類

POM關係: 主要爲依賴,繼承,合成 依賴關係: xml 代碼 <dependencies>    <dependency>      <groupId>junit<groupId>      <artifactId>junit<artifactId>      <version>4.0<version>      <type>ja<rtype>      <scope>test<scope>      <optional>true<optional>    <dependency>    ... < dependencies>

groupId, artifactId, version:描述了依賴的項目唯一標誌 可以通過以下方式進行安裝:

使用以下的命令安裝: mvn install:install-file –Dfile=non-maven-proj.jar –DgroupId=some.group –DartifactId=non-maven-proj –Dversion=1 創建自己的庫,並配置,使用deploy:deploy-file 設置此依賴範圍爲system,定義一個系統路徑。不提倡。 type:相應的依賴產品包形式,如jar,war scope:用於限制相應的依賴範圍,包括以下的幾種變量: compile :默認範圍,用於編譯 provided:類似於編譯,但支持你期待jdk或者容器提供,類似於classpath runtime:在執行時,需要使用 test:用於test任務時使用 system:需要外在提供相應得元素。通過systemPath來取得 systemPath: 僅用於範圍爲system。提供相應的路徑 optional: 標註可選,當項目自身也是依賴時。用於連續依賴時使用 獨佔性 外在告訴maven你只包括指定的項目,不包括相關的依賴。此因素主要用於解決版本衝突問題

xml 代碼

<dependencies>    <dependency>      <groupId>org.apache.maven<groupId>      <artifactId>maven-embedder<artifactId>      <version>2.0<version>      <exclusions>        <exclusion>          <groupId>org.apache.maven<groupId>          <artifactId>maven-core<artifactId>        <exclusion>      <exclusions>    <dependency> < dependencies>

表示項目maven-embedder需要項目maven-core,但我們不想引用maven-core

繼承關係 另一個強大的變化,maven帶來的是項目繼承。主要的設置: 定義父項目

xml 代碼

<project> < modelVersion>4.0.0<modelVersion> < groupId>org.codehaus.mojo<groupId> < artifactId>my-parent<artifactId> < version>2.0version> < packaging>pom<packaging> < project>

packaging 類型,需要pom用於parent和合成多個項目。我們需要增加相應的值給父pom,用於子項目繼承。主要的元素如下:

依賴型 開發者和合作者 插件列表 報表列表 插件執行使用相應的匹配ids 插件配置 子項目配置 xml 代碼

<project> < 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可以不需要,但是用於指明parent的目錄,用於快速查詢。

dependencyManagement: 用於父項目配置共同的依賴關係,主要配置依賴包相同因素,如版本,scope。

合成(或者多個模塊)    一個項目有多個模塊,也叫做多重模塊,或者合成項目。 如下的定義:

xml 代碼

<project> < modelVersion>4.0.0<modelVersion> < groupId>org.codehaus.mojo<groupId> < artifactId>my-parent<artifactId> < version>2.0<version> < modules>    <module>my-project1<module>    <module>my-project2<module> < modules> < project>

build 設置    主要用於編譯設置,包括兩個主要的元素,build和report build    主要分爲兩部分,基本元素和擴展元素集合 注意:包括項目build和profile build

xml 代碼

<project>

<build>...<build> < profiles>    <profile>

     <build>...<build>    <profile> < profiles> < project>

基本元素

xml 代碼

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

defaultGoal: 定義默認的目標或者階段。如install directory: 編譯輸出的目錄 finalName: 生成最後的文件的樣式 filter: 定義過濾,用於替換相應的屬性文件,使用maven定義的屬性。設置所有placehold的值

資源(resources)    你項目中需要指定的資源。如spring配置文件,log4j.properties

xml 代碼

<project> < build>    ...    <resources>      <resource>        <targetPath>META-INF/plexus<targetPath>        <filtering>falsefiltering>        <directory>${basedir}/src/main/plexus<directory>        <includes>          <include>configuration.xml<include>        <includes>        <excludes>          <exclude>**/*.properties<exclude>        <excludes>      <resource>    <resources>    <testResources>      ...    <testResources>    ... < build> < project>

resources: resource的列表,用於包括所有的資源 targetPath: 指定目標路徑,用於放置資源,用於build filtering: 是否替換資源中的屬性placehold directory: 資源所在的位置 includes: 樣式,包括那些資源 excludes: 排除的資源 testResources: 測試資源列表 插件 在build時,執行的插件,比較有用的部分,如使用jdk 5.0編譯等等

xml 代碼

<project> < build>    ...    <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>    <plugins> < build> < project>

extensions: true or false,是否裝載插件擴展。默認false inherited: true or false,是否此插件配置將會應用於poms,那些繼承於此的項目 configuration: 指定插件配置 dependencies: 插件需要依賴的包 executions: 用於配置execution目標,一個插件可以有多個目標。 如下:

xml 代碼

<plugin>        <artifactId>maven-antrun-plugin<artifactId>

       <executions>          <execution>            <id>echodirid>            <goals>              <goal>run<goal>            <phase>verify<phase>            <inherited>false<inherited>            <configuration>              <tasks>                <echo>Build Dir: ${project.build.directory}<echo>              <tasks>            <configuration>          <execution>        <executions>     <plugin>

說明:

id:規定execution 的唯一標誌 goals: 表示目標 phase: 表示階段,目標將會在什麼階段執行 inherited: 和上面的元素一樣,設置false maven將會拒絕執行繼承給子插件 configuration: 表示此執行的配置屬性

插件管理    pluginManagement:插件管理以同樣的方式包括插件元素,用於在特定的項目中配置。所有繼承於此項目的子項目都能使用。主要定義插件的共同元素

擴展元素集合 主要包括以下的元素: Directories 用於設置各種目錄結構,如下:

xml 代碼

<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>

Extensions

表示需要擴展的插件,必須包括進相應的build路徑。

xml 代碼

<project> < 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 插件能輸出相應的定製和配置報表。

xml 代碼

<reporting>    <plugins>      <plugin>        <outputDirectory>${basedir}/target/siteoutputDirectory>        <artifactId>maven-project-info-reports-pluginartifactId>        <reportSets>          <reportSet>reportSet>        reportSets>      plugin>    plugins> reporting>

Report Sets    用於配置不同的目標,應用於不同的報表

xml 代碼

<reporting>    <plugins>      <plugin>        ...        <reportSets>          <reportSet>            <id>sunlinkid>            <reports>              <report>javadoc<report>            <inherited>truei<nherited>            <configuration>              <links>                <link>http://java.sun.com/j2se/1.5.0/docs/api/<link>            <configuration>          <reportSet>        <reportSets>      <plugin>    <plugins> < reporting>

name:項目除了artifactId外,可以定義多個名稱 description: 項目描述 url: 項目url inceptionYear:創始年份 Licenses

xml 代碼

<licenses> < license>    <name>Apache 2name>    <url>http://www.apache.org/licenses/LICENSE-2.0.txt<url>    <distribution>repodistribution>    <comments>A business-friendly OSS license<comments> < license> < licenses>

Organization 配置組織信息

xml 代碼

<organization>    <name>Codehaus Mojoname>    <url>http://mojo.codehaus.org<url> organization>

Developers 配置開發者信息

xml 代碼

<developers>    <developer>      <id>eric<id>      <name>Eric<name>      <email>[email protected]<email>      <url>http://eric.propellors.net<url>      <organization>Codehausorganization>      <organizationUrl>http://mojo.codehaus.orgorganization<Url>      <roles>        <role>architect<role>        <role>developer<role>      <roles>      <timezone>-6timezone>      <properties>        <picUrl>http://tinyurl.com/prv4tpic<Url>      <properties>    <developer> < developers>

Contributors

xml 代碼

<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>

環境設置

Issue Management    定義相關的bug跟蹤系統,如bugzilla,testtrack,clearQuest等

xml 代碼

<issueManagement>    <system>Bugzilla<system>    <url>http://127.0.0.1/bugzillau<rl> < issueManagement>

Continuous Integration Management 連續整合管理,基於triggers或者timings

xml 代碼

<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>

Mailing Lists

xml 代碼

<mailingLists>   <mailingList>     <name>User List<name>     <subscribe>[email protected]<subscribe>     <unsubscribe>[email protected]<subscribe>     <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>

SCM 軟件配置管理,如cvs 和svn

xml 代碼

<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>

Repositories

配置同setting.xml中的開發庫

Plugin Repositories 配置同 repositories

Distribution Management 用於配置分發管理,配置相應的產品發佈信息,主要用於發佈,在執行mvn deploy後表示要發佈的位置 1 配置到文件系統

xml 代碼

<distributionManagement> < repository> < id>proficio-repository<id> < name>Proficio Repository<name> < url>file://${basedir}/target/deploy<url> < repository> < distributionManagement>

2 使用ssh2配置

xml 代碼

<distributionManagement> < repository> < id>proficio-repository<id> < name>Proficio Repository<name> < url>scp://sshserver.yourcompany.com/deploy<url> < repository> < distributionManagement>

3 使用sftp配置

xml 代碼

<distributionManagement> < repository> < id>proficio-repositoryi<d> < name>Proficio Repository<name> < url>sftp://ftpserver.yourcompany.com/deploy<url> < repository> < distributionManagement>

4 使用外在的ssh配置    編譯擴展用於指定使用wagon外在ssh提供,用於提供你的文件到相應的遠程服務器。

xml 代碼

<distributionManagement> < repository> < id>proficio-repository<id> < name>Proficio Repository<name> < url>scpexe://sshserver.yourcompany.com/deploy<url> < repository> < distributionManagement> < build> < extensions> < extension> < groupId>org.apache.maven.wagon<groupId> < artifactId>wagon-ssh-external<artifactId> < version>1.0-alpha-6<version> < extension> < extensions> < build>

5 使用ftp配置

xml 代碼

<distributionManagement> < repository> < id>proficio-repository<id> < name>Proficio Repository<name> < url>ftp://ftpserver.yourcompany.com/deploy<url> < repository> < distributionManagement> < build> < extensions> < extension> < groupId>org.apache.maven.wagongroupId> < artifactId>wagon-ftpartifactId> < version>1.0-alpha-6version> < extension> < extensions> < build>

repository 對應於你的開發庫,用戶信息通過settings.xml中的server取得

Profiles 類似於settings.xml中的profiles,增加了幾個元素,如下的樣式:

xml 代碼

<profiles>    <profile>      <id>test<id>      <activation>...<activation>      <build>...<build>      <modules>...<modules>      <repositories>...<repositories>      <pluginRepositories>...<pluginRepositories>      <dependencies>...<dependencies>      <reporting>...r<eporting>      <dependencyManagement>...<dependencyManagement>      <distributionManagement>...<distributionManagement>    <profile> < profiles>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章