一、簡介
Maven是一個項目建構工具,最大的特點就是第三方依賴庫(這裡用到的依賴庫就是Nexus,之後會說)對項目進行統一管理,還可以通過配置指令,完成項目的編譯,測試,打包等動作
而其最主要的配置檔案就是pom.xml和settings.xml
pom.xml檔案則是Maven的核心對象模型,一個項目都需要一個對應的pom.xml檔案
Maven也定義了自己的目錄結構,這裡就不介紹了,隻需要知道pom.xml檔案是放在與src同級目錄下,如下圖所示:

二、配置
1.将下載下傳好的Maven壓縮檔案解壓,目錄結構如下圖所示:
2.配置
maven需要jre的支援,是以首先確定機器上裝有JDK,并配有環境變量。
之後,還需要配置maven的環境變量,假設我的安裝位置如上圖,那麼環境變量就應設為:
M2_HOME:D:\apache-maven-2.2.1
PATH:%M2_HOME%\bin
配置完成後,記得重新打開DOS視窗,然後輸入指令mvn -v,檢查是否安裝成功,如果成功,如下圖所示:
然後,就要去配置項目中的pom.xml檔案了,pom檔案可以通過maven自動生成,這裡就直接介紹需要配置的地方了:
<?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>
<groupId>Test</groupId>
<artifactId>Test</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.3</version>
<executions>
<execution>
<phase>pre-integration-test</phase>
<configuration>
<tasks>
<sequential>
<exec executable="cmd" >
<arg line="/c sc stop Tomcat6"/>
</exec>
<sleep seconds="30" />
<delete file="D:/cretomcat/webapps/Test.war" />
<delete dir="D:/cretomcat/webapps/Test" />
<delete dir="D:/cretomcat/work/Catalina/localhost" />
<copy overwrite="true" todir="D:/cretomcat/webapps" file="target/Test.war" />
<exec executable="cmd" >
<arg line="/c sc start Tomcat6" />
</exec>
<echo taskname="waitfor" message="wait for deploy..." />
<sleep minutes="1" />
</sequential>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<finalName>Test</finalName>
</build>
<dependencies>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts-core</artifactId>
<version>${strutsVersion}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>org.springframework.core</artifactId>
<version>3.0.1.RELEASE</version>
<classifier>A</classifier>
</dependency>
</dependencies>
<properties>
<springVersion>2.5.4</springVersion>
<struts2Version>2.1.6</struts2Version>
</properties>
</project>
這就是pom.xml配置完成後的檔案内容,基本都可以看懂,有幾點需要解釋下:
1.<plugin>标簽是引入maven的插件,這裡引入了3個插件:
maven-compiler-plugin:
我們都知道Maven本質上是一個插件架構,它的核心并不執行任何具體的建構任務,所有這些任務都交給插件來完成,例如編譯源代碼是由maven- compiler-plugin完成的。進一步說,每個任務對應了一個插件目标(goal),每個插件會有一個或者多個目标,例如maven- compiler-plugin的compile目标用來編譯位于
src/main/java/
目錄下的主源碼
maven-resources-plugin:
maven-compiler-plugin用來編譯Java代碼,maven-resources-plugin則用來處理資源檔案
maven-antrun-plugin:
由于Maven是做不了一些檔案删除和Copy的動作的,是以它就必須請教Ant來幫它執行,是以就必須引入Ant插件,然後就是做了一連串的動作,目的就是為了讓CC最新生成的War包成功地釋出到Tomcat上,這裡由于Tomcat是以服務的方式啟動的,是以直接通過Dos指令Start即可
2.<dependency>則是依賴項的Maven坐标,根據坐标Maven可以找到對應的jar包進行引入,坐标對應的jar包是放在第三方倉庫中,是以要去Maven目錄下的setting.xml檔案中設定jar包倉庫的路徑
去Maven的安裝目錄下配置setting.xml檔案來建立鏡像庫,用于加載自己定義的jar包倉庫
找到<mirrors>标簽,加入第三方類庫URL,如下圖所示:
<mirrors>
<!-- mirror
| Specifies a repository mirror site to use instead of a given repository. The repository that
| this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
| for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
|
<mirror>
<id>mirrorId</id>
<mirrorOf>repositoryId</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://my.repository.com/repo/path</url>
</mirror>
-->
<mirror>
<id>Nexus</id>
<mirrorOf>*</mirrorOf>
<name>Nexus Public Mirror</name>
<url>http:127.0.0.1:8081//nexus/content/groups/public</url>
</mirror>
</mirrors>
最後,隻需在IP位址對應的機器上安裝Nexus并配置jar包和對應坐标就可以了,每次打包項目時,pom檔案自動會根據dependency标簽找到對應的jar包并放入到項目中去