什麼是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>