Maven pom 配置

經過在 pom.xml 中定義 jar 包版本和依賴,可以方便的管理 jar 文件。pom做爲項目對象模型。經過xml表示maven項目,使用pom.xml來實現。主要描述了項目:包括配置文件;開發者須要遵循的規則,缺陷管理系統,組織和licenses,項目的url,項目的依賴性,以及其餘全部的項目相關因素。 
java

Xml代碼  收藏代碼mysql

  1. <?xml version="1.0" encoding="UTF-8"?>  web

  2. <project>  spring

  3.     <modelVersion>4.0.0</modelVersion>  sql

  4.     <groupId>...</groupId>  apache

  5.     <artifactId>...</artifactId>  瀏覽器

  6.     <version>...</version>  服務器

  7.     <packaging>...</packaging>  eclipse

  8.     <dependencies>...</dependencies>  maven

  9.     <parent>...</parent>  

  10.     <dependencyManagement>...</dependencyManagement>  

  11.     <modules>...</modules>  

  12.     <properties>...</properties>  

  13.   

  14.     <build>...</build>  

  15.     <reporting>...</reporting>  

  16.   

  17.     <name>...</name>  

  18.     <description>...</description>  

  19.     <url>...</url>  

  20.     <inceptionYear>...</inceptionYear>  

  21.     <license>...</license>  

  22.     <organization>...</organization>  

  23.     <developers>...</developers>  

  24.     <contributors>...</contributors>  

  25.   

  26.     <issueManagement>...</issueManagement>  

  27.     <ciManagement>...</ciManagement>  

  28.     <mailingLists>...</mailingLists>  

  29.     <scm>...</scm>  

  30.     <preprequisites>...</preprequisites>  

  31.     <repositories>...</repositories>  

  32.     <pluginRepositories>...</pluginRepositories>  

  33.     <distributionManagement>...</distributionManagement>  

  34.     <profiles>...</profiles>  

  35. </project>  


經常使用元素的說明: 
   modelVersion:描述這個POM文件聽從哪一個版本的項目描述符 
   groupId:項目或者組織的惟一標誌,而且配置時生成的路徑也是由今生成。一般用一個徹底正確的包的名字來與其餘項目的相似名字來進行區分(好比:org.apache.maven) 
   artifactId: 項目的通用名稱 
   groupId 和 artifactId 合起來做爲當前項目的惟一標識,maven2最終會根據這兩個值,決定項目發佈到 repo 上時所處的位置。 
   version:項目的版本號,它用來標記同一個項目發佈的不一樣版本。 
   packaging: 這個項目生產出來的artifact 類型,如 pom, jar, maven-plugin, ejb, war, ear, rar, par ,... 。 

最小配置 
任何項目都最少要包含如下幾個標籤。 

Xml代碼  收藏代碼

  1. <project>  

  2.     <modelVersion>4.0.0</modelVersion>  

  3.     <groupId>...</groupId>  

  4.     <artifactId>...</artifactId>  

  5.     <version>...</version>  

  6. </project>          


modelVersion 這個標籤必須存在,並且它的值必須是4.0.0,這標誌着咱們如今使用的是maven2。 

1 maven的繼承定義 
   假設定義了一個父項目:  
      

Xml代碼  收藏代碼

  1. <project>  

  2.         <modelVersion>4.0.0</modelVersion>  

  3.         <groupId>com.taobao</groupId>  

  4.         <artifactId>taobao-parent</artifactId>  

  5.         <version>2.0</version>  

  6.          <packaging>pom</packaging>  

  7.        </project>  

   packaging 類型須要pom,用於parent和合成多個項目。那麼在其下的子項目中加上以下設置用以繼承 
       

Xml代碼  收藏代碼

  1. <parent>  

  2.           <groupId>com.taobao</groupId>  

  3.           <artifactId> taobao-parent </artifactId>  

  4.           <version>2.0</version>  

  5.        </parent>  


maven繼承案例 - springside sample : mini-web: 

Xml代碼  收藏代碼

  1. <project>  

  2.     <modelVersion>4.0.0</modelVersion>  

  3.     <parent>  

  4.         <groupId>org.springside</groupId>  

  5.         <artifactId>springside3-parent</artifactId>  

  6.         <version>3.3.2</version>  

  7.     </parent>  

  8.     <artifactId>mini-web</artifactId>  

  9.     <packaging>war</packaging>  

  10.     <name>Springside3's Mini-Web Example</name>  

  11.       

  12.       

  13.     ...  

  14. </project>  



2 合成(或者多個模塊) 
    一個項目有多個模塊,也叫作多重模塊,或者合成項目。每個被列出來的子模塊都指向包含這個模塊的目錄文件的相對路徑。 
   以下定義:     
  

Xml代碼  收藏代碼

  1. <modules>  

  2.        <module>tc-client</module>  

  3.        <module>tc-server</module>  

  4.    </modules>  



3  build 設置 
pom中一大部分配置都包含在build標籤中,這部分是在對項目進行構建時所須要的配置,當你對項目進行編譯,測試,打包,發佈的時候,這部分配置就會在對應的階段起做用了。build 主要分爲兩部分,基本元素和擴展元素集合,注意:包括項目build和profile build 
  xml 代碼   : 
   

Xml代碼  收藏代碼

  1. <project>  

  2.             <build></build>  

  3.             <profiles>  

  4.                  <profile>  

  5.                       <build></build>  

  6.                 </profile>  

  7.            </profiles>  

  8.    </project>    

build標籤中能夠包含不少插件,這些插件能夠配置到項目的某些構建階段,隨着項目的構建進程發揮做用。 
4 報表部分 
咱們執行mvn site就能夠爲項目生成一系列能夠用來描述項目信息的網頁,maven2中的一大部分插件就是專門在這時候發揮效用的,它們能夠根據項目的結構,源代碼,測試,SCM信息等,生成各類特殊功能的報表,這要把這些插件配置在reporting標籤中,它們就會在執行mvn site的同時起做用。全部的報告將會包括在瀏覽器的導航欄中。 
xml 代碼   : 
   

Xml代碼  收藏代碼

  1. <project>  

  2.             <build></build>  

  3.             <profiles>  

  4.                  <profile>  

  5.                       <build></build>  

  6.                 </profile>  

  7.            </profiles>  

  8.     <reporting></reporting>  

  9.    </project>    




5  插件 
在build時,執行的插件,比較有用的部分,如使用jdk 5.0編譯等等 
xml 代碼: 

Xml代碼  收藏代碼

  1. <project>  

  2. <build>  

  3.     …  

  4.     <plugins>  

  5.       <plugin>  

  6.         <groupId>org.apache.maven.plugins</groupId>  

  7.         <artifactId>maven-jar-plugin</artifactId>  

  8.         <version>2.0</version>  

  9.         <extensions>false</extensions>  

  10.         <inherited>true</inherited>  

  11.         <configuration>  

  12.           <classifier>test</classifier>  

  13.         </configuration>  

  14.         <dependencies></dependencies>  

  15.         <executions></executions>  

  16.       </plugin>  

  17.     </plugins>  

  18. </build>  

  19. </project>  


extensions: true or false,是否裝載插件擴展。默認false 
inherited: true or false,是否此插件配置將會應用於poms,那些繼承於此的項目 
configuration: 指定插件配置 
dependencies: 插件須要依賴的包 
executions: 用於配置execution目標,一個插件能夠有多個目標。 
springside sample : mini-web 中插件配置的案例: 

Xml代碼  收藏代碼

  1. <build>  

  2.         <plugins>  

  3.             <!-- eclipse插件 -->  

  4.             <plugin>  

  5.                 <groupId>org.apache.maven.plugins</groupId>  

  6.                 <artifactId>maven-eclipse-plugin</artifactId>  

  7.                 <configuration>  

  8.                     <wtpversion>2.0</wtpversion>  

  9.                     <additionalProjectnatures>  

  10.                         <projectnature>org.springframework.ide.eclipse.core.springnature  

  11.                         </projectnature>  

  12.                     </additionalProjectnatures>  

  13.                 </configuration>  

  14.             </plugin>  

  15.         </plugins>  

  16.     </build>  


6 資源(resources)你項目中須要指定的資源。如spring配置文件,log4j.properties 
xml 代碼: 

Xml代碼  收藏代碼

  1. <project>  

  2. <build>  

  3.     …  

  4.     <resources>  

  5.       <resource>  

  6.         <targetPath>META-INF/plexus</targetPath>  

  7.         <filtering>false</filtering>  

  8.         <directory>${basedir}/src/main/plexus</directory>  

  9.         <includes>  

  10.           <include>configuration.xml</include>  

  11.         <includes>  

  12.         <excludes>  

  13.           <exclude>**/*.properties</exclude>  

  14.         <excludes>  

  15.       </resource>  

  16.     </resources>  

  17.     <testResources>  

  18.       …  

  19.     </testResources>  

  20.     …  

  21. </build>  

  22. </project>  


resources: resource的列表,用於包括全部的資源 
targetPath: 指定目標路徑,用於放置資源,用於build 
filtering: 是否替換資源中的屬性placehold 
directory: 資源所在的位置 
includes: 樣式,包括那些資源 
excludes: 排除的資源 
testResources: 測試資源列表 
7  依賴關係 
咱們日常接觸最多的就是dependencies和dependency標籤了。由於既然使用了maven2,最大的得益之處仍是能夠用它統一管理依賴庫。dependencies和它內部的dependency標籤就用來配置當前項目所依賴的第三方庫的,而且每一個依賴都須要groupId,artifactId和version這些數據 。 

Xml代碼  收藏代碼

  1. <project>  

  2.     <modelVersion>4.0.0</modelVersion>  

  3.     <groupId>...</groupId>  

  4.     <artifactId>...</artifactId>  

  5.     <version>...</version>  

  6.     <dependencies>  

  7.     <dependency>  

  8.         <groupId>junit</groupId>  

  9.         <artifactId>junit</artifactId>  

  10.         <version>4.0</version>  

  11.         <type>jar</type>  

  12.         <scope>test</scope>  

  13.         <optional>true</optional>  

  14.     </dependency>  

  15.     </dependencies>  

  16. </project>  

        
咱們能夠看到dependency標籤中也包含了groupId,artifactId和version信息,這三部分就是一個maven2項目所需的最小配置,也是maven2所管理的項目的惟一標識,有了這三個數據,maven2就能夠在repo中查找到對應的依賴,並將其包含到咱們的項目中,從而實現對依賴庫的管理。 

上面的依賴中,咱們使用了groupId爲junit,artifactId爲junit,版本爲4.0的依賴,這個依賴會在test做用域中起做用,也就是說,只有在進行單元測試的時候,纔會用到這個依賴,與之對應的是,當咱們的程序正常編譯,運行,打包發佈時,是不會包含這個依賴的。maven2內置了多個做用域,可讓咱們將不一樣階段使用的依賴庫進行隔離。 

8 其它標籤description     當前項目的一個細節描述,當須要描述這個項目的時候被Maven所用,好比在web 站點中。 這個元素可以被指定爲CDATA 類型,容許在描述器中 HTML的tags, 並不鼓勵用空白文原本表示。 若是你須要去修改生成的web 站點的索引頁,你可以用你本身的索引來代替自動生成的文本。 
url     當前項目的主頁的URL prerequisites     描述當前項目的編譯環境的先決條件 
issueManagement     當前項目的發佈管理信息。 
ciManagement     當前項目的連續集成信息。 
inceptionYear     當前項目開始的年份, 用4位數字描述。 涉及到介紹狀況時用做提供版權信息mailingLists     包含的信息包括郵件列表developers     描述當前的項目的開發人員的信息contributors     描述對當前項目有貢獻的人員的信息,不特指開發人員licenses     這個元素描述了當前項目的全部的許可文件。每個許可文件用一個許可元素來描述,而後描述額外的元素。 一般只列出適用於這個項目的許可文件以及適用於 依賴的非licenses.若是多個licenses都列出來了,那麼假設這個用戶選擇其中的所需的,而不是接受全部的許可文件。 
scm     指定當前項目中的版本控制工具,好比CVS, Subversion, 等等。 
organization     這個元素描述這個項目所屬組織的各類屬性的描述。這些屬性應用於文檔建立的時候 (版權通知和連接)。 
repositories     發現依賴和擴展的遠程資源庫。 
pluginRepositories     發現plugins 的遠程資源庫的列表,主要是爲了編譯和報告dependencies     這個元素描述了全部與當前項目相關的依賴。這些依賴被用做建立一個編譯時的路徑。 他們被自動的從資源庫中下在下來依據當前項目的定義。如需更多信息,參看 the dependency mechanism reports     Deprecated.禁止適用。如今的版本中被 Maven所忽略掉。 
dependencyManagement     缺省的依賴信息將會從這個元素中繼承。這些依賴在這一部分中被不馬上被解決的。當一個源於這個POM的元素描述了一個依賴根據匹配的 groupId 和 artifactId,這個部分的版本和其餘值用做那些尚未指定的依賴。 
distributionManagement     對於一個項目分配的信息容許對於遠程web服務器和資源庫的site和artifacts配置。 
properties     屬性能夠做爲POM的自始自終的替換物,若是可行的話能夠用做resources 的過濾器,格式是<name>value</name>.案例以下: 

Xml代碼  收藏代碼

  1. <!-- 項目屬性 -->  

  2. <project>  

  3.     <properties>  

  4.         <jdbc.driver.groupId>mysql</jdbc.driver.groupId>  

  5.         <jdbc.driver.artifactId>mysql-connector-java</jdbc.driver.artifactId>  

  6.         <jdbc.driver.version>5.1.11</jdbc.driver.version>  

  7.     </properties>  

  8. </project>  



9 能夠經過如下方式進行安裝: 

使用如下的命令安裝: 
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: 標註可選,當項目自身也是依賴時。用於連續依賴時使用 

相關文章
相關標籤/搜索