1. 程式人生 > >Maven的理解

Maven的理解

一個Maven2工程的核心就是這一個pom.xml,它包含了你的工程詳細資訊,如:版本、配置、依賴、測試、專案成員等等。學習maven2主要就是學習如何配置pom.xml。

一個簡單的而完全可操作的pom.xml如下所示:

<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> <!--maven2-->
<groupId>ctzj_bss</groupId> <!-- 這個專案所屬組的id,一般是專案所在的公司或組織的域名 -->
<artifactId>NRC</artifactId> <!-- 專案的id,和groupId一起組成這個專案的一個唯一id,以用在別的maven2工程中 -->
<packaging>jar</packaging> <!-- 最後這個工程的打包型別,在這裡是打成jar包 -->
<version>1.0</version> <!-- 版本號 -->
<name>kenan nrc batch</name> <!-- 專案名, 區別於artifactId-->
<description>insert a nrc in kenan db</description> <!-- 專案描述,會出現在生成的工程站點上 -->
<build> <!-- 專案的構建資訊 -->
<sourceDirectory>src</sourceDirectory>
<testSourceDirectory>test</testSourceDirectory>
<outputDirectory>target\classes</outputDirectory>
<testOutputDirectory>target\test-classes</testOutputDirectory>
<directory>target</directory> <!-- 構建後生成檔案(jar,site等)所在位置 -->
</build>
<dependencies> <!-- 專案的依賴外接包,maven的優勢之一 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<reporting> <!-- 專案報表,有很多外掛可供選擇 -->
<outputDirectory>target/site</outputDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
</plugin>
</plugins>
</reporting>
</project>


在你配置了你的pom檔案後,你就可以使用如下命令來執行maven2

mvn compiler:compile 編譯你的原檔案
mvn jar:jar 打包
mvn site:site 生成專案站點


接下來逐個介紹pom.xml的各個要素

developor,organization and mail lit
build
dependencies
reporting

1.developor,organization and mail list

可以在pom中加入開發人員列表,專案所屬組織資訊,郵件列表,這些資訊會出現在生成的專案站點上

<organization>
<name>CTZJ BSS</name>
</organization>
<developers>
<developer>
<name>Chen Lin</name>
<organization>ZJHCsoft</organization>
<email>
[email protected]
</email>
</developer>
</developers>
<mailingLists>
<mailingList>
<name>${pom.name} Dev List</name>
</mailingList>
<mailingList>
<name>${pom.name} User List</name>
</mailingList>
</mailingLists>

2. build

配置專案的構建資訊,主要有指定目錄,包括原始碼、測試原始碼、輸出目錄、資源目錄等.如果沒有特殊需求以上的pom已經足夠了

3. dependencies

配置專案要用到的jar包.自認為maven對jar包的管理是它的一個重要特色.下面是一個jar包的配置:

<dependency>
<groupId>concurrent</groupId>
<artifactId>concurrent</artifactId>
<version>1.3.4</version>
<scope>compile</scope>
</dependency>

groupId: 沒有特定的含義,一般來自同一組織的jar包會有相同的groupId值
artifactId: 這個jar的id,一般是這個jar包的名稱
version: jar包的版本號,一般情況下,artifactId-version.jar就是這個jar包的檔名
scope: 這個jar所影響的範圍,compile指它會在編譯原始碼時用到.還有test,provide,runtime等值

如果你不知道jar包的groupId和artifactId,可以到http://www.mavenregistry.com/ 進行查詢

一旦指定後,當你進行編譯時,它就會到你的本地資源庫

另,如果你引入本地包,則需要先行將它安裝到本地資料庫(repository),使用如下命令

mvn install:install-file -DgroupId=%groupId% -DartifactId=%artifactId% -DgeneratePom=true -Dfile=%file% -Dpackaging=jar -Dversion=%version%

4. reporting

通過使用相應的report plug-in可以生成各種各樣的report.如javadoc,junit test report,程式碼檢查等

常用的常見有

<plugin>
<groupId>org.apache.maven.plugins</groupId> <!--java doc -->
<artifactId>maven-javadoc-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId> <!-- 程式碼檢查 -->
<artifactId>maven-checkstyle-plugin</artifactId>
<configuration>
<configLocation>config/sun_checks.xml</configLocation>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId> <!-- test 結果報表 -->
<artifactId>surefire-report-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId> <!-- 原始碼的HTML化 -->
<artifactId>jxr-maven-plugin</artifactId>
</plugin>

在maven的官方網站有可以找到更多的report plugin


最後一個比較完整的pom.xml看起來像這樣:

<?xml version="1.0" encoding="UTF-8"?>
<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>
<name>CutoverModule</name>
<groupId>ctzj_batch</groupId>
<artifactId>eai_cutover</artifactId>
<url>http://134.98.83.137:8081/scarab/issues</url>
<inceptionYear>2006</inceptionYear>
<organization>
<name>CTZJ BSS</name>
<url>http://www.zjhcsoft.com</url>
</organization>
<version>1.0</version>
<description>97 cutover batch module:invoke eai-adapter to update the phone number or value of
c4/c5</description>
<developers>
<developer>
<name>Chen Lin</name>
<organization>ZJHCSoft</organization>
<email>
[email protected]
</email>
</developer>
</developers>
<mailingLists>
<mailingList>
<name>${pom.name} Dev List</name>
</mailingList>
</mailingLists>
<build>
<sourceDirectory>src</sourceDirectory>
<testSourceDirectory>test</testSourceDirectory>
<outputDirectory>target\classes</outputDirectory>
<testOutputDirectory>target\test-classes</testOutputDirectory>
<directory>target</directory>
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>2.1</version>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.1</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.1.2</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>2.0</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.1</version>
</plugin>
</plugins>
</build>
<ciManagement>
<system>continuum</system>
<url>http://localhost:8080/continuum/servlet/continuum</url>
</ciManagement>
<scm>
<connection>scm:local|E:/eclipseworkshop|EAIBatch</connection>
</scm>
<dependencies>
<dependency>
<groupId>ctzj_batch</groupId>
<artifactId>Eaiadapter</artifactId>
<version>1.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>ctzj_batch</groupId>
<artifactId>ta</artifactId>
<version>1.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>ctzj_batch</groupId>
<artifactId>grnds-common</artifactId>
<version>4.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>concurrent</groupId>
<artifactId>concurrent</artifactId>
<version>1.3.4</version>
<scope>compile</scope>
</dependency>
</dependencies>
<reporting>
<outputDirectory>target/site</outputDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<configuration>
<configLocation>config/sun_checks.xml</configLocation>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>surefire-report-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jxr-maven-plugin</artifactId>
</plugin>
</plugins>
</reporting>
</project>






maven的POM結構
[ 作者: | 來源:| 點選數: <script src="maven的POM結構-教程-原始碼-下載.files/Click.htm" type="text/javascript"></script> 65 ]
<!-- 參加開發的人員 -->
<developers>
<developer>
<name>Dave Blackett (Jetspeed logos)</name>
<id/>
<email>
[email protected]
</email>
<organization/>
</developer>
......
</developers>

******專案相關性部分******與一箇中央構件資源庫一起使用,將消除幾個常見的構建問題

<!---------------------------------------------------- -->
<!-- 專案相關性部分 -->
<!---------------------------------------------------- -->

<!--build過程中maven依據之建立classpath,並且maven會到remote repository自動下載這些dependencies ,如type是jar,下載的檔案就是<artifactId>-<version>.jar-->
<dependencies>

<!-- 專案依賴JAR檔案 "activation-1.0.1.jar",檔案在Maven repository
的activation/jars子目錄,並且如果資源庫中找不到,maven會在自動從maven.repo.remote下載(《maven安裝使用》中提到的修改是因為maven的預設設定遠端資源庫無法連線上)-->
<dependency>
<id>activation</id>
<version>1.0.1</version>
<properties>
<war.bundle.jar>true</war.bundle.jar>
</properties>
</dependency>
</dependencies>


groupId 告訴 Maven 資源庫內哪個子目錄中包含相關性檔案。
artifactId 告訴 Maven 該構件的唯一標識。如果是jar檔案,可以理解為去掉字尾和版本號的檔名 。
version 表示相關性的版本號。
jar (可選的)表示相關性的 JAR 檔案。在絕大多數情況下,可以從相關性的 <artifactId> 和 <version> 構造 JAR 檔案的名稱。
type (可選的)相關性的型別;如 jar 和分發版等。預設值是 jar。
url 可選的)相關性專案的 URL,在相關性是在因特網上找到的第三方庫時非常有用。
properties dependency還有properties可以設定,具體要參照各自引用的plugin


******專案構建部分******
<!---------------------------------------------------- -->
<!-- 專案build和reports部分 -->
<!---------------------------------------------------- -->

<build>
<!-- (OPTIONAL) 定義原始檔位置. -->
<sourceDirectory>src/java</sourceDirectory>

<!-- (OPTIONAL) 定義單元測試原始檔位置. -->
<unitTestSourceDirectory>test/java</unitTestSourceDirectory>

<!-- (OPTIONAL) 單元測試用例檔案模式. -->
<unitTest>
<!-- 排除測試 -->
<excludes>
<exclude>org/apache/jetspeed/services/registry/TestRegistryCategories.java</exclude>
......
</excludes>
<!-- 包含測試 -->
<includes>
<include>**/Test*.java</include>
</includes>
<!-- 單元測試使用的資源 -->
<resources>
<resource>
<!-- 資源位置,project.xml的相對路徑 -->
<directory>${basedir}/src/java</directory>
<!-- 排除檔案 -->
<excludes>
<exclude>**/*.java</exclude>
</excludes>
<!-- 包含檔案 -->
<includes>
<include>org/apache/jetspeed/modules/localization/JetspeedLocalization_*.properties</include>
</includes>
</resource>
.......
</resources>
</unitTest>

<!-- 專案使用的資源,結構與單元測試使用的資源結構相同 -->
<resources>
<resource>
......
</resource>
.......
</resources>

<!-- (OPTIONAL) 使用maven site生成站點的時候被引用,並以指定的順序排列在導航條內,report指定生成報告的plugin -->
<reports>
<report>
<report>maven-tasklist-plugin</report>
<report>maven-changelog-plugin</report>
<report>maven-javadoc-plugin</report>

相關推薦

maven 理解

http://blog.csdn.net/ningbohezhijunbl/article/details/24799465 maven的Pom.xml中build下的resources,一直不太理解,現在總結一下。 resources:描述工程中資源的位置 <

maven理解

https://www.cnblogs.com/kuoAT/p/6845876.html 在父pom檔案的dependencyManagement標籤下引入的依賴,是標明父專案認為子專案可能用到的依賴,而不是會強制引入,當子專案用到父專案dependencyManageme

Maven理解及使用

Maven的作用在開發中,為了保證編譯通過,我們會到處去尋找jar包,當編譯通過了,執行的時候,卻發現"ClassNotFoundException",我們想到的是,難道還差jar包?每個Java專案的目錄結構都沒有一個統一的標準,配置檔案到處都是,單元測試程式碼到底應該放在那裡也沒有一個權威的規範。因此,我

Maven的構建生命周期理解

設置 字節 left ati 流程 每一個 pack 來源 刪除 以下引用官方的生命周期解釋https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html: 一、構建生命周期基

Maven倉庫理解、如何引入本地包、Maven多種方式打可執行jar包

依賴 tro 個人 部署 格式 多種方式 ava null 路徑 轉載博客:http://quicker.iteye.com/blog/2319947 有關MAVEN倉庫的理解參見:http://blog.csdn.net/wanghantong/article/det

maven簡單理解

pre tro 文件 mvc 自己的 尋找 www. junit xsd 前言: maven項目也是一個項目,類似於javaProject,javaWebProject,就是多了些功能,其他也沒啥,所以大家接觸的時候不要害怕! 1 . 幫你下載jar包 maven項目會有

Maven基本理解

方法 流行 新的 ron webapp 包沖突 provide nal 堅持 我記得在搞懂maven之前看了幾次重復的maven的教學視頻。不知道是自己悟性太低還是怎麽滴,就是搞不清楚,現在弄清楚了,基本上入門了。寫該篇博文,就是為了幫助那些和我一樣對於maven迷迷

專案實訓--Maven理解的使用

Maven的理解: 具象化:maven到底是什麼? 如果學過gradle 的就知道,這是個類似產品。但是這個使用xml 來配置的。 Maven是基於專案物件模型(POM project object model),可以通過一小段描述資訊(配置)來管理專案的構建,報告和文件的

maven繼承與聚合的理解

剛接觸的maven感覺一頭霧水,經過一番掙扎終於弄明白了些 1.首先說下maven的繼承:主要是現在專案都比較大,模組比較多,很多模組又有很多相同的依賴,如果每個模組都去依賴的話就會很多餘,如是就出現了繼承,建立一個父專案統一管理相依賴,其餘模組只需要繼承父模組就行了 2.聚合主要

對於maven中的pom.xml檔案的理解

<project xmlns="http://maven.apache.org/POM/4.0.0"  xmlns:xsi="http://www.w3.org/2001/XML Schema-instance" xsi:schemaLocation="http://maven.a

Maven簡單理解和介紹

前言 本文主要面向Maven的初學者,介紹了何為Maven以及Maven的簡單用法。更加詳細的用法可以檢視文章底部的連結。 1 Maven簡介 1.1 何為Maven Maven是Apache組織開發的一個開源的跨平臺的專案管理工具,主要服務基於java平臺的專案構建、依賴管理和專案

maven的深入理解《三》

前面兩篇是對maven的基礎的介紹,這一篇更側重於實際的運用 1.maven設定編譯級別的兩種方式 用外掛的形式: <plugin> <groupId>org.apache.maven.plugins</groupId>

理解Maven中的SNAPSHOT版本和正式版本

Maven中建立的依賴管理方式基本已成為Java語言依賴管理的事實標準,Maven的替代者Gradle也基本沿用了Maven的依賴管理機制。在Maven依賴管理中,唯一標識一個依賴項是由該依賴項的三個屬性構成的,分別是groupId、artifactId以及version

Gradle的簡介,這裡就不羅嗦了,Google上一大堆,這裡就說說它的基本使用吧(可以簡單的理解為它是Maven的高階版本,沒那麼神祕)。

基本使用流程: 1. 下載 Gradle 到本地,並配置環境變數。 2. 在專案中建立 build.gradle檔案 3. 編寫 build.grade 指令碼 4. 執行grade 命令 快捷使用方法: 下載Android Studio,讓它幫我們搞定一切。 指定依賴: 1.

關於maven的一些個人理解

一、什麼是Maven?maven是一個專案構建和管理的工具,提供了幫助管理構建、文件、報告、依賴、釋出、分發的方法。可以方便的編譯程式碼、進行依賴管理、管理二進位制庫等等。如果沒有Maven,你可能不得不經歷的過程!!如果使用了spring,去spring的官網下載jar包;

理解 maven 多模組專案依賴關係

語言功底差,直接上程式碼。然後再解釋1。父pom<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:

《轉》maven中import scope依賴方式解決單繼承問題的理解

在maven多模組專案中,為了保持模組間依賴的統一,常規做法是在parent model中,使用dependencyManagement預定義所有模組需要用到的dependency(依賴) <dependencyManagement>

Maven學習總結(18)——深入理解Maven倉庫

一.本地倉庫(Local Repository) 本地倉庫就是一個本機的目錄,這個目錄被用來儲存我們專案的所有依賴(外掛的jar包還有一些其他的檔案),簡單的說,當你build一個Maven專案的時候,所有的依賴檔案都會放在本地倉庫裡,倉庫供所有專案都可以使用 預設情

Maven的構建生命週期理解

  以下引用官方的生命週期解釋https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html: 一、構建生命週期基礎: Maven基於構建生命週期的中心概念。這意味著構建和分發特

深入理解maven與應用(二):靈活的構建

 一個優秀的構建系統必須足夠靈活,應該能夠讓專案在不同的環境下都能成功構建。maven為了支援構建的靈活性,內建了三大特性,即:屬性、profile和資源過濾。 1、maven屬性  maven屬性分6類:     1、內建屬性:如${basedir}表示專案根目錄,$