Maven與Ant比較

來源:互聯網
上載者:User

標籤:檔案中   project   schema   lin   儲存   int   mode   task   環境   

Ant是軟體構建工具,Maven的定位是軟體專案管理和理解工具。Maven除了具備Ant的功能外,還增加了以下主要的功能:

1)使用Project Object Model來對軟體專案管理;

2)內建了更多的隱式規則,使得構建檔案更加簡單;

3)內建依賴管理和Repository來實現依賴的管理和統一儲存;

4)內建了軟體構建的生命週期;

 

 

一 POM(Project Object Model)與專案管理

每一個Maven工程都包含一個pom.xml檔案,其他儲存了該工程相關的資訊,從而達到一定的專案管理的功能。例如包含了工程的配置,缺陷跟蹤系統資訊,工程的組織,許可協議,工程的路徑,依賴等資訊。

典型的pom.xml如下:

<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>

<!-- Project Meta Data -->
<name>...</name>
< description>...</description>
< url>...</url>
< inceptionYear>...</inceptionYear>
< licenses>...</licenses>
< organization>...</organization>

<developers>...</developers>
< contributors>...</contributors>

<!-- Environment -->
<issueManagement>...</issueManagement>
< ciManagement>...</ciManagement>
< mailingLists>...</mailingLists>
< scm>...</scm>
< prerequisites>...</prerequisites>
< repositories>...</repositories>
< pluginRepositories>...</pluginRepositories>
< distributionManagement>...</distributionManagement>
< profiles>...</profiles>
< /project>

 

 

二 隱形的規則和簡單的構建檔案

Maven工程的目錄結構必須為如下的結構

 

Maven還有內建的構建生命週期,內建定義了build,test,package,deploy等task。

 

由於Maven工程目錄的規則和內建的構建生命週期,從而使得構建檔案簡單,例如如下的構建檔案中甚至沒有出現build,package等task的定義,但是我們已經可以調用wvm package等內建的task了:

<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/xsd/maven-4.0.0.xsd">   <modelVersion>4.0.0</modelVersion>   <groupId>com.mycompany.app</groupId>   <artifactId>my-app</artifactId>   <packaging>jar</packaging>   <version>1.0-SNAPSHOT</version>   <name>Maven Quick Start Archetype</name>   <url>http://maven.apache.org</url>   <dependencies>     <dependency>       <groupId>junit</groupId>       <artifactId>junit</artifactId>       <version>3.8.1</version>       <scope>test</scope>     </dependency>   </dependencies> </project>

 

三 依賴管理和Repository

Maven的dependence management用來管理所有此project的dependences,且在dependence repository中自動尋找和下載dependence。

 

1) 依賴管理

例如工程以來MySQL如下:

<dependencyManagement>
< dependencies>
< dependency>
< groupId>mysql</groupId>
< artifactId>mysql-connector-java</artifactId>
< version>5.1.2</version>
< /dependency>
< dependencies>
< /dependencyManagement>

 

<dependency>
< groupId>mysql</groupId>
< artifactId>mysql-connector-java</artifactId>
< /dependency>

 

2)dependence repository

更好的辦法是公司或每個team有自己的repository,例如:

 

 

四 構建生命週期的定義

構建生命週期顯式地定義了構建,測試,和發布的過程,是每個Maven工程的核心。Maven包含了3個內建的生命週期:default,clean和site。

 

1)default生命週期處理了工程的編譯,測試和部署,他一共包含20多個階段,主要的階段如下:

Validate: 驗證所有的工程資訊是否可用且正確

Compile: 編譯原始碼

Test: 在一套framework下運行但願測試

Package: 以發布的格式打包編譯的代碼

Integration-test: 在整合測試環境中處理(部署)發布包

Verify: 檢測發布包是否正確可用
Install: 在本地的repository上安裝發布包
Deploy: 在遠端repository上安裝發布包

 

以上的階段具有先後順序,執行某個階段時,此階段前的所有階段都會被自動地執行。

 

2)clean生命週期處理工程的清理工作,包含3個階段:pre-clean, clean, post-clean。

 

3)site生命週期處理工程site文檔的組建和部署,包含下列階段:

pre-site, site, post-site 和site-deploy,其中site-deploy用來將site文檔部署到指定的web server上。

 

 

 

轉:http://www.cnblogs.com/itech/archive/2011/11/01/2231837.html

Maven與Ant比較

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.