maven配置文件解析

maven2配置文件主要分爲settings.xml與pom.xml兩種,下面將逐一介紹。 
===================================settings.xml===================================================================
 settings.xml對於maven來講至關於全局性的配置,用於全部的項目。在maven2中存在兩個 settings.xml,一個位於maven2的安裝目錄conf下面,做爲全局性配置。對於團隊設置,保持一致的定義是關鍵,因此 maven2/conf下面的settings.xml就做爲團隊共同的配置文件。保證全部的團隊成員都擁有相同的配置。固然對於每一個成員,都須要特殊的 自定義設置,如用戶信息,因此另一個settings.xml就做爲本地配置。默認的位置爲:${user.dir} /.m2/settings.xml目錄中(${user.dir} 指windows 中的用戶目錄)。
    settings.xml基本結構以下:
xml 代碼
 2 <settings xmlns="http://maven.apache.org/POM/4.0.0"  
 3  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
 4  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0  5  http://maven.apache.org/xsd/settings-1.0.0.xsd">  
 6   <localRepository/>  
 7   <interactiveMode/>  
 8   <usePluginRegistry/>  
 9   <offline/>  
10   <pluginGroups/>  
11   <servers/>  
12   <mirrors/>  
13   <proxies/>  
14   <profiles/>  
15   <activeProfiles/>  
16 </settings>  
簡單介紹一下幾個主要的配置因素:
localRepository:表示本地庫的保存位置,也就是maven2主要的jar保存位置,默認在${user.dir}/.m2/repository,若是須要另外設置,就換成其餘的路徑。
offline:若是不想每次編譯,都去查找遠程中心庫,那就設置爲true。固然前提是你已經下載了必須的依賴包。
Servers:在POM中的 distributionManagement元素定義了開發庫。然而,特定的username和pwd不能使用於pom.xml,因此經過此配置來保存server信息
xml 代碼
 2 <servers>  
 3    <server>  
 4      <id>server001</id>  
 5      <username>my_login</username>  
 6      <password>my_password</password>  
 7      <privateKey>${usr.home}/.ssh/id_dsa</privateKey>  
 8      <passphrase>some_passphrase</passphrase>  
 9      <filePermissions>664</filePermissions>  
10      <directoryPermissions>775</directoryPermissions>  
11      <configuration></configuration>  
12    </server>  
13  </servers>   
  • id:server 的id,用於匹配distributionManagement庫id,比較重要。
  • username, password:用於登錄此服務器的用戶名和密碼
  • privateKey, passphrase:設置private key,以及passphrase
  • filePermissions, directoryPermissions:當庫文件或者目錄建立後,須要使用權限進行訪問。參照unix文件許可,如664和775
Mirrors:表示鏡像庫,指定庫的鏡像,用於增長其餘庫
xml 代碼
1  <mirrors>  
2    <mirror>  
3      <id>planetmirror.com</id>  
4      <name>PlanetMirror Australia</name>  
5      <url>http://downloads.planetmirror.com/pub/maven2</url>  
6      <mirrorOf>central</mirrorOf>  
7    </mirror>  
8  </mirrors>  
  • id,name:惟一的標誌,用於區別鏡像
  • url:鏡像的url
  • mirrorOf:此鏡像指向的服務id
Proxies:此設置,主要用於沒法直接訪問中心的庫用戶配置。
xml 代碼
 1  <proxies>  
 2    <proxy>  
 3      <id>myproxy</id>  
 4      <active>true</active>  
 5      <protocol>http</protocol>  
 6      <host>proxy.somewhere.com</host>  
 7      <port>8080</port>  
 8      <username>proxyuser</username>  
 9      <password>somepassword</password>  
10      <nonProxyHosts>*.google.com|ibiblio.org</nonProxyHosts>  
11    </proxy>  
12  </proxies>  
  • id:代理的標誌
  • active:是否激活代理
  • protocol, host, port:protocol://host:port 代理
  • username, password:用戶名和密碼
  • nonProxyHosts: 不須要代理的host
Profiles:相似於pom.xml中的profile元素,主要包括activation,repositories,pluginRepositories 和properties元素,  剛開始接觸的時候,可能會比較迷惑,其實這是maven2中比較強大的功能。從字面上來講,就是個性配置。單獨定義profile後,並不會生效,須要經過知足條件來激活。
repositories 和pluginRepositories: 定義其餘開發庫和插件開發庫。對於團隊來講,確定有本身的開發庫。能夠經過此配置來定義。以下的配置,定義了本地開發庫,用於release 發佈。
xml 代碼
 1  <repositories>  
 2         <repository>  
 3           <id>repo-local</id>  
 4        <name>Internal 開發庫</name>  
 5        <url>http://192.168.0.2:8082/repo-local</url>  
 6           <releases>  
 7             <enabled>true</enabled>  
 8             <updatePolicy>never</updatePolicy>  
 9             <checksumPolicy>warn</checksumPolicy>  
10           </releases>  
11           <snapshots>  
12             <enabled>false</enabled>  
13           </snapshots>  
14           <layout>default</layout>  
15         </repository>  
16       </repositories>  
17       <pluginRepositories>  
18     <pluginRepository>  
19     <id>repo-local</id>  
20     <name>Internal 開發庫</name>  
21     <url>http://192.168.0.2:8082/repo-local</url>  
22     <releases>  
23             <enabled>true</enabled>  
24             <updatePolicy>never</updatePolicy>  
25             <checksumPolicy>warn</checksumPolicy>  
26     </releases>  
27     <snapshots>  
28     <enabled>false</enabled>  
29     </snapshots>  
30     <layout>default</layout>  
31     </pluginRepository>  
32     </pluginRepositories>  
releases, snapshots:每一個產品的版本的Release或者snapshot(注:release和snapshot的區別,release通常是比較穩定的版本,而snapshot基本上不穩定,只是做爲快照)

properties:maven 的properties做爲placeholder值,如ant的properties。包括如下的5種類型值:
  1. env.X,返回當前的環境變量
  2. project.x:返回pom中定義的元素值,如project.version
  3. settings.x:返回settings.xml中定義的元素
  4. java 系統屬性:全部通過java.lang.System.getProperties()返回的值
  5. x:用戶本身設定的值
Activation: 用於激活此profile
xml 代碼
 1  <activation>  
 2         <activeByDefault>false</activeByDefault>  
 3         <jdk>1.5</jdk>  
 4         <os>  
 5           <name>Windows XP</name>  
 6           <family>Windows</family>  
 7           <arch>x86</arch>  
 8           <version>5.1.2600</version>  
 9         </os>  
10         <property>  
11           <name>mavenVersion</name>  
12           <value>2.0.3</value>  
13         </property>  
14         <file>  
15           <exists>${basedir}/file2.properties</exists>  
16           <missing>${basedir}/file1.properties</missing>  
17         </file>  
18  </activation>  
  • jdk:若是匹配指定的jdk版本,將會激活
  • os:操做系統
  • property:若是maven能檢測到相應的屬性
  • file: 用於判斷文件是否存在或者不存在
除了使用activation來激活profile,一樣能夠經過activeProfiles來激活
Active Profiles:表示激活的profile,經過profile id來指定。
xml 代碼
1   <activeProfiles>  
2      <activeProfile>env-test</activeProfile> 指定的profile id  
3   </activeProfiles> 
===================================pom.xml===================================================================
什麼是pom:pom做爲項目對象模型。經過xml表示maven項目,使用pom.xml來實現。主要描述了項目:包括配置文件;開發者須要遵循的規則,缺陷管理系統,組織和licenses,項目的url,項目的依賴性,以及其餘全部的項目相關因素。

xml 代碼
 1 <project>  
 2   <modelVersion>4.0.0modelVersion>  3   
 4     
 5   <groupId>...<groupId>  6   <artifactId>...<artifactId>  7   <version>...<version>  8   <packaging>...<packaging>  9   <dependencies>...<dependencies> 10   <parent>...<parent> 11   <dependencyManagement>...<dependencyManagement> 12   <modules>...<modules> 13   <properties>...<properties> 14   
15     
16   <build>...<build> 17   <reporting>...<reporting> 18   
19     
20   <name>...<name> 21   <description>...<description> 22   <url>...<url> 23   <inceptionYear>...<inceptionYear> 24   <licenses>...<licenses> 25   <organization>...<organization> 26   <developers>...<developers> 27   <contributors>...contributors> 28   
29     
30   <issueManagement>...<issueManagement> 31   <ciManagement>...<ciManagement> 32   <mailingLists>...<mailingLists> 33   <scm>...<scm> 34   <prerequisites>...<prerequisites> 35   <repositories>...<repositories> 36   <pluginRepositories>...<pluginRepositories> 37   <distributionManagement>...<distributionManagement> 38   <profiles>...<profiles> 39 <project>  
基本內容:
    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 代碼
 
 1 <dependencies>  
 2     <dependency>  
 3       <groupId>junitgroupId>  4       <artifactId>junitartifactId>  5       <version>4.0version>  6       <type>jartype>  7       <scope>testscope>  8       <optional>trueoptional>  9     <dependency>  
10  ... 11   <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 代碼
 1  <dependencies>  
 2     <dependency>  
 3       <groupId>org.apache.maven<groupId>  
 4       <artifactId>maven-embedder<artifactId>  
 5       <version>2.0<version>  
 6       <exclusions>  
 7         <exclusion>  
 8           <groupId>org.apache.maven<groupId>  
 9           <artifactId>maven-core<artifactId>  
10         <exclusion>  
11       <exclusions>  
12     <dependency> 
表示項目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.0<version>  
  <packaging>pom<packaging>  
<project> 
    packaging 類型,須要pom用於parent和合成多個項目。咱們須要增長相應的值給父pom,用於子項目繼承。主要的元素以下:
  • 依賴型
  • 開發者和合做者
  • 插件列表
  • 報表列表
  • 插件執行使用相應的匹配ids
  • 插件配置
  • 子項目配置
xml 代碼
 1 <project>  
 2   <modelVersion>4.0.0<modelVersion>  
 3   <parent>  
 4     <groupId>org.codehaus.mojo<groupId>  
 5     <artifactId>my-parent<artifactId>  
 6     <version>2.0<version>  
 7     <relativePath>../my-parent<relativePath>  
 8   <parent>  
 9   <artifactId>my-project<artifactId>  
10 <project> 
relativePath能夠不須要,可是用於指明parent的目錄,用於快速查詢。

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

合成(或者多個模塊)
    一個項目有多個模塊,也叫作多重模塊,或者合成項目。
以下的定義:
xml 代碼
 1 <project>  
 2   <modelVersion>4.0.0<modelVersion>  
 3   <groupId>org.codehaus.mojo<groupId>  
 4   <artifactId>my-parent<artifactId>  
 5   <version>2.0<version>  
 6   <modules>  
 7     <module>my-project1<module>  
 8     <module>my-project2<module>  
 9   <modules>  
10 <project> 
build 設置
    主要用於編譯設置,包括兩個主要的元素,build和report
  build
    主要分爲兩部分,基本元素和擴展元素集合
注意:包括項目build和profile build
xml 代碼
1  <project>   
2   <build>...build> 3   <profiles>  
4     <profile>    
5       <build>...build> 6     <profile>  
7   <profiles>  
8 <project> 
基本元素
xml 代碼
1 <build>  
2   <defaultGoal>install<defaultGoal>  
3   <directory>${basedir}/target<directory>  
4   <finalName>${artifactId}-${version}<finalName>  
5   <filters>  
6     <filter>filters/filter1.properties<filter>  
7   <filters>  
8  ... 9 <build> 
  • defaultGoal: 定義默認的目標或者階段。如install
  • directory: 編譯輸出的目錄
  • finalName: 生成最後的文件的樣式
  • filter: 定義過濾,用於替換相應的屬性文件,使用maven定義的屬性。設置全部placehold的值
資源(resources)你項目中須要指定的資源。如spring配置文件,log4j.properties
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: 測試資源列表
插件: 在build時,執行的插件,比較有用的部分,如使用jdk 5.0編譯等等
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目標,一個插件能夠有多個目標。
以下:
   
xml 代碼
 2 <plugin>  
 3         <artifactId>maven-antrun-plugin<artifactId>  
 4           <executions>  
 5           <execution>  
 6             <id>echodir<id>  
 7             <goals>  
 8               <goal>run<goal>  
 9             <goals>  
10             <phase>verify<phase>  
11             <inherited>falsein<herited>  
12             <configuration>  
13               <tasks>  
14                 <echo>Build Dir: ${project.build.directory}<echo>  
15               <tasks>  
16             <configuration>  
17           <execution>  
18         <executions>  
19       <plugin> 
  說明:
  • id:規定execution 的惟一標誌
  • goals: 表示目標
  • phase: 表示階段,目標將會在什麼階段執行
  • inherited: 和上面的元素同樣,設置false maven將會拒絕執行繼承給子插件
  • configuration: 表示此執行的配置屬性

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

擴展元素集合主要包括如下的元素:
Directories
用於設置各類目錄結構,以下:
xml 代碼
1 <build>  
2     <sourceDirectory>${basedir}/src/main/java<sourceDirectory>  
3     <scriptSourceDirectory>${basedir}/src/main/<scriptsscriptSourceDirectory>  
4     <testSourceDirectory>${basedir}/src/test/java<testSourceDirectory>  
5     <outputDirectory>${basedir}/target/classes<outputDirectory>  
6     <testOutputDirectory>${basedir}/target/test-classes<testOutputDirectory>  
7  ... 8   <build>  

Extensions:表示須要擴展的插件,必須包括進相應的build路徑。
xml 代碼
 1 <project>  
 2   <build>  
 3  ...  4     <extensions>  
 5       <extension>  
 6         <groupId>org.apache.maven.wagon<groupId>  
 7         <artifactId>wagon-ftp<artifactId>  
 8         <version>1.0-alpha-3<version>  
 9       <extension>  
10     <extensions>  
11  ... 12   <build>  
13 <project>  
Reporting:    用於在site階段輸出報表。特定的maven 插件能輸出相應的定製和配置報表。
 
xml 代碼
 
 1 <reporting>  
 2     <plugins>  
 3       <plugin>  
 4         <outputDirectory>${basedir}/target/site<outputDirectory>  
 5         <artifactId>maven-project-info-reports-plugin<artifactId>  
 6         <reportSets>  
 7           <reportSet><reportSet>  
 8         <reportSets>  
 9       <plugin>  
10     <plugins>  
11   <reporting>  
Report Sets:用於配置不一樣的目標,應用於不一樣的報表
xml 代碼
 
 1 <reporting>  
 2     <plugins>  
 3       <plugin>  
 4  ...  5         <reportSets>  
 6           <reportSet>  
 7             <id>sunlink<id>  
 8             <reports>  
 9               <report>javadoc<report>  
10             <reports>  
11             <inherited>truein<herited>  
12             <configuration>  
13               <links>  
14                 <link>http://java.sun.com/j2se/1.5.0/docs/api/<link>  
15               <links>  
16             <configuration>  
17           <reportSet>  
18         <reportSets>  
19       <plugin>  
20     <plugins>  
21   <reporting> 
相關文章
相關標籤/搜索