maven
配置篇 之
pom.xml
說
完了
settings.xml
配置,下來
說
一下
maven2
的主要配置
pom.xml
什 麼 是 pom?
pom 作 爲項 目 對 象模型。通 過 xml 表示maven 項 目,使用pom.xml來 實現 。主要描述了 項 目:包括配置文件; 開發 者需要遵循的 規則 ,缺陷管理系 統 , 組織 和licenses, 項 目的url, 項 目的依 賴 性,以及其他所有的 項 目相 關 因素。
快速察看:
<project>
<modelVersion>4.0.0</modelVersion>
<!-- The Basics -->
<groupId>...</groupId>
<artifactId>...</artifactId>
<version>...</version>
<packaging>...</packaging>
<dependencies>...</dependencies>
<parent>...</parent>
<dependencyManagement>...</dependencyManagement>
<modules>...</modules>
<properties>...</properties>
<!-- Build Settings -->
<build>...</build>
<reporting>...</reporting>
<!-- More Project Information -->
<name>...</name>
<description>...</description>
<url>...</url>
<inceptionYear>...</inceptionYear>
<licenses>...</licenses>
<organization>...</organization>
<developers>...</developers>
<contributors>...</contributors>
<!-- Environment Settings -->
<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 。所有需要的元素
什 麼 是 pom?
pom 作 爲項 目 對 象模型。通 過 xml 表示maven 項 目,使用pom.xml來 實現 。主要描述了 項 目:包括配置文件; 開發 者需要遵循的 規則 ,缺陷管理系 統 , 組織 和licenses, 項 目的url, 項 目的依 賴 性,以及其他所有的 項 目相 關 因素。
快速察看:
<project>
<modelVersion>4.0.0</modelVersion>
<!-- The Basics -->
<groupId>...</groupId>
<artifactId>...</artifactId>
<version>...</version>
<packaging>...</packaging>
<dependencies>...</dependencies>
<parent>...</parent>
<dependencyManagement>...</dependencyManagement>
<modules>...</modules>
<properties>...</properties>
<!-- Build Settings -->
<build>...</build>
<reporting>...</reporting>
<!-- More Project Information -->
<name>...</name>
<description>...</description>
<url>...</url>
<inceptionYear>...</inceptionYear>
<licenses>...</licenses>
<organization>...</organization>
<developers>...</developers>
<contributors>...</contributors>
<!-- Environment Settings -->
<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
關
系:
主要 爲 依 賴 , 繼 承,合成
依 賴關 系:
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.0</version>
<type>jar</type>
<scope>test</scope>
<optional>true</optional>
</dependency>
...
</dependencies>
主要 爲 依 賴 , 繼 承,合成
依 賴關 系:
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.0</version>
<type>jar</type>
<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 你只包括指定的 項 目,不包括相 關 的依 賴 。此因素主要用於解決版本衝突 問題
<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>
表示 項 目maven-embedder需要 項 目maven-core,但我 們 不想引用maven-core
繼 承 關 系
另一個 強 大的 變 化,maven 帶 來的是 項 目 繼 承。主要的 設 置:
定 義 父 項 目
<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,用於子 項 目 繼 承。 主要的元素如下:
外在告 訴 maven 你只包括指定的 項 目,不包括相 關 的依 賴 。此因素主要用於解決版本衝突 問題
<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>
表示 項 目maven-embedder需要 項 目maven-core,但我 們 不想引用maven-core
繼 承 關 系
另一個 強 大的 變 化,maven 帶 來的是 項 目 繼 承。主要的 設 置:
定 義 父 項 目
<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
- 插件配置
- 子項目配置
<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 。
合成(或者多個模 塊 )
一個 項 目有多個模 塊 ,也叫做多重模 塊 ,或者合成 項 目。
如下的定 義 :
<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
<project>
<!-- "Project Build" contains more elements than just the BaseBuild set -->
<build>...</build>
<profiles>
<profile>
<!-- "Profile Build" contains a subset of "Project Build"s elements -->
<build>...</build>
</profile>
</profiles>
</project>
基本元素
<build>
<defaultGoal>install</defaultGoal>
<directory>${basedir}/target</directory>
<finalName>${artifactId}-${version}</finalName>
<filters>
<filter>filters/filter1.properties</filter>
</filters>
...
</build>
<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 。
合成(或者多個模 塊 )
一個 項 目有多個模 塊 ,也叫做多重模 塊 ,或者合成 項 目。
如下的定 義 :
<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
<project>
<!-- "Project Build" contains more elements than just the BaseBuild set -->
<build>...</build>
<profiles>
<profile>
<!-- "Profile Build" contains a subset of "Project Build"s elements -->
<build>...</build>
</profile>
</profiles>
</project>
基本元素
<build>
<defaultGoal>install</defaultGoal>
<directory>${basedir}/target</directory>
<finalName>${artifactId}-${version}</finalName>
<filters>
<filter>filters/filter1.properties</filter>
</filters>
...
</build>
- defaultGoal: 定義默認的目標或者階段。如install
- directory: 編譯輸出的目錄
- finalName: 生成最後的文件的樣式
- filter: 定義過濾,用於替換相應的屬性文件,使用maven定義的屬性。設置所有placehold的值
資 源 (resources)
你 項 目中需要指定的 資 源。如 spring 配置文件 ,log4j.properties
<project>
<build>
...
<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>
</resources>
<testResources>
...
</testResources>
...
</build>
</project>
- resources: resource的列表,用於包括所有的資源
- targetPath: 指定目標路徑,用於放置資源,用於build
- filtering: 是否替換資源中的屬性placehold
- directory: 資源所在的位置
- includes: 樣式,包括那些資源
- excludes: 排除的資源
- testResources: 測試資源列表
插件
在 build 時 , 執 行的插件,比 較 有用的部分,如使用 jdk 5.0 編譯 等等
<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>
在 build 時 , 執 行的插件,比 較 有用的部分,如使用 jdk 5.0 編譯 等等
<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目標,一個插件可以有多個目標。
如下:
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<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>
</executions>
</plugin>
說 明:
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<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>
</executions>
</plugin>
說 明:
- id:規定execution 的唯一標誌
- goals: 表示目標
- phase: 表示階段,目標將會在什麼階段執行
- inherited: 和上面的元素一樣,設置false maven將會拒絕執行繼承給子插件
- configuration: 表示此執行的配置屬性
插件管理
pluginManagement :插件管理以同 樣 的方式包括插件元素,用於在特定的 項 目中配置。所有 繼 承於此 項 目的子 項 目都能使用。主要定 義 插件的共同元素
擴 展元素集合
主要包括以下的元素:
Directories
用於 設 置各 種 目 錄結 構,如下:
<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 路徑。
<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 插件能 輸 出相 應 的定製和配置 報 表。
<reporting>
<plugins>
<plugin>
<outputDirectory>${basedir}/target/site</outputDirectory>
<artifactId>maven-project-info-reports-plugin</artifactId>
<reportSets>
<reportSet></reportSet>
</reportSets>
</plugin>
</plugins>
</reporting>
Report Sets
用於配置不同的目 標 , 應 用於不同的 報 表
<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>
更多的 項 目信息
name: 項 目除了 artifactId 外,可以定 義 多個名稱
description: 項 目描述
url: 項 目 url
inceptionYear: 創 始年份
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>
Organization
配置 組織 信息
<organization>
<name>Codehaus Mojo</name>
<url>http://mojo.codehaus.org</url>
</organization>
Developers
配置 開發 者信息
<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>
<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 等
<issueManagement>
<system>Bugzilla</system>
<url>http://127.0.0.1/bugzilla</url>
</issueManagement>
Continuous Integration Management
連續 整合管理,基於 triggers 或者 timings
<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
<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>
SCM
軟 件配置管理,如 cvs 和 svn
<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 配置到文件系 統
<distributionManagement>
<repository>
<id>proficio-repository</id>
<name>Proficio Repository</name>
<url>file://${basedir}/target/deploy</url>
</repository>
</distributionManagement>
2 使用 ssh2 配置
<distributionManagement>
<repository>
<id>proficio-repository</id>
<name>Proficio Repository</name>
<url>scp://sshserver.yourcompany.com/deploy</url>
</repository>
</distributionManagement>
3 使用 sftp 配置
<distributionManagement>
<repository>
<id>proficio-repository</id>
<name>Proficio Repository</name>
<url>sftp://ftpserver.yourcompany.com/deploy</url>
</repository>
</distributionManagement>
4 使用外在的 ssh 配置
編譯擴 展用於指定使用 wagon 外在 ssh 提供,用於提供你的文件到相 應 的 遠 程服 務 器。
<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 配置
<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.wagon</groupId>
<artifactId>wagon-ftp</artifactId>
<version>1.0-alpha-6</version>
</extension>
</extensions>
</build>
repository 對應 於你的 開發庫 ,用 戶 信息通 過 settings.xml 中的 server 取得
Profiles
類 似於 settings.xml 中的 profiles ,增加了幾個元素,如下的 樣 式:
<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>