使用Maven構(gòu)建項目
一、Maven的下載
Maven目前最新版本為2.08,下載apache-maven-2.0.8-bin.zip,將其解壓到某一目錄下如d:"。設(shè)置以下環(huán)境變量:
1) M2_HOME:maven安裝目錄,d:\ apache-maven-2.0.8
2) M2: %M2_HOME%"\bin
3) MAVEN_OPTS:可選項, Xms256m -Xmx512m
4) 更新或創(chuàng)建 PATH: %M2%;%Path%
5) 如果你還沒有創(chuàng)建JAVA_HOME,PATH,請創(chuàng)建它們
JAVE_HOME:JAVA安裝目錄,Path:%JAVA_HOME%"\bin
測試Maven是否安裝成功:mvn --version,如果輸出類似下面的信息,則表示安裝成功:Maven version: 2.0.8
Java version: 1.6.0_02
OS name: "windows xp" version: "5.1" arch: "x86" Family: "windows"
二、創(chuàng)建項目
1) 創(chuàng)建普通的應(yīng)用,使用下面的命令:
mvn archetype:create -DgroupId=com.mycompany.app -DartifactId=my-app
2)創(chuàng)建WEB應(yīng)用,使用下面的命令:
mvn archetype:create "
-DarchetypeGroupId=org.apache.maven.archetypes "
-DarchetypeArtifactId=maven-archetype-webapp "
-DgroupId=com.mycompany.app "
-DartifactId=my-webapp
簡單解釋一下:
archetype 是一個內(nèi)建插件,他的create任務(wù)將建立項目骨架archetypeArtifactId 項目骨架的類型可用項目骨架有:
* maven-archetype-archetype
* maven-archetype-j2ee-simple
* maven-archetype-mojo
* maven-archetype-portlet
* maven-archetype-profiles (currently under development)
* maven-archetype-quickstart
* maven-archetype-simple (currently under development)
* maven-archetype-site
* maven-archetype-site-simple
* maven-archetype-webapp
groupId 項目的java包結(jié)構(gòu),可修改
artifactId 項目的名稱,生成的項目目錄也是這個名字,可修改
三、Maven常見命令
1)validate:驗證項目是否正確和所有需要的信息是否可用
2)compile:編譯項目源代碼
3)test :對所有編譯過的源代碼進(jìn)行單元測試,這些測試不需要代碼被打包或發(fā)布
4)package把所有編譯后的源代碼和包打到一個包中,像jar包
5)integration-test : 處理所有發(fā)布的包如果需要的話
6)verify : 驗證包是否符合標(biāo)準(zhǔn)
7)install :安裝包到當(dāng)?shù)氐膬Υ鎺熘?/span>
8)deploy : 拷貝最早的包到遠(yuǎn)程儲存庫中,以便項目的開發(fā)者共享使用
9)clean : 清除創(chuàng)建
10)site :生成站點文檔
11)jetty:run或tomcat:run運行web項目
12)eclipse:eclipse生成可導(dǎo)入eclipse的項目
四、POM.xml介紹
pom作為項目對象模型。通過xml表示maven項目,使用pom.xml來實現(xiàn)。主要描述了項目:包括配置文件;開發(fā)者需要遵循的規(guī)則,缺陷管理系統(tǒng),組織和licenses,項目的url,項目的依賴性,以及其他所有的項目相關(guān)因素。
快速察看:
<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>
基本內(nèi)容:
POM包括了所有的項目信息。
maven 相關(guān):
pom定義了最小的maven2元素,允許groupId,artifactId,version。所有需要的元素
groupId:項目或者組織的唯一標(biāo)志,并且配置時生成的路徑也是由此生成,如org.codehaus.mojo生成的相對路徑為:/org/codehaus/mojo
artifactId: 項目的通用名稱
version:項目的版本
packaging: 打包的機(jī)制,如pom, jar, maven-plugin, ejb, war, ear, rar, par
classifier: 分類
POM關(guān)系:
主要為依賴,繼承,合成
依賴關(guān)系:
<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:描述了依賴的項目唯一標(biāo)志
可以通過以下方式進(jìn)行安裝:
使用以下的命令安裝:
mvn install:install-file ?Dfile=non-maven-proj.jar ?DgroupId=some.group ?DartifactId=non-maven-proj ?Dversion=1
創(chuàng)建自己的庫,并配置,使用deploy:deploy-file
設(shè)置此依賴范圍為system,定義一個系統(tǒng)路徑。不提倡。
type:相應(yīng)的依賴產(chǎn)品包形式,如jar,war
scope:用于限制相應(yīng)的依賴范圍,包括以下的幾種變量:
compile :默認(rèn)范圍,用于編譯
provided:類似于編譯,但支持你期待jdk或者容器提供,類似于classpath
runtime:在執(zhí)行時,需要使用
test:用于test任務(wù)時使用
system:需要外在提供相應(yīng)得元素。通過systemPath來取得
systemPath: 僅用于范圍為system。提供相應(yīng)的路徑
optional: 標(biāo)注可選,當(dāng)項目自身也是依賴時。用于連續(xù)依賴時使用
獨占性
外在告訴maven你只包括指定的項目,不包括相關(guān)的依賴。此因素主要用于解決版本沖突問題
<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
繼承關(guān)系
另一個強(qiáng)大的變化,maven帶來的是項目繼承。主要的設(shè)置:
定義父項目
<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和合成多個項目。我們需要增加相應(yīng)的值給父pom,用于子項目繼承。主要的元素如下:
依賴型
開發(fā)者和合作者
插件列表
報表列表
插件執(zhí)行使用相應(yīng)的匹配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:
用于父項目配置共同的依賴關(guān)系,主要配置依賴包相同因素,如版本,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 設(shè)置
主要用于編譯設(shè)置,包括兩個主要的元素,build和report
build
主要分為兩部分,基本元素和擴(kuò)展元素集合
注意:包括項目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: 定義默認(rèn)的目標(biāo)或者階段。如install
directory: 編譯輸出的目錄
finalName: 生成最后的文件的樣式
filter: 定義過濾,用于替換相應(yīng)的屬性文件,使用maven定義的屬性。設(shè)置所有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: 指定目標(biāo)路徑,用于放置資源,用于build
filtering: 是否替換資源中的屬性placehold
directory: 資源所在的位置
includes: 樣式,包括那些資源
excludes: 排除的資源
testResources: 測試資源列表
插件
在build時,執(zhí)行的插件,比較有用的部分,如使用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,是否裝載插件擴(kuò)展。默認(rèn)false
inherited: true or false,是否此插件配置將會應(yīng)用于poms,那些繼承于此的項目
configuration: 指定插件配置
dependencies: 插件需要依賴的包
executions: 用于配置execution目標(biāo),一個插件可以有多個目標(biāo)。
如下:
<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:規(guī)定execution 的唯一標(biāo)志
goals: 表示目標(biāo)
phase: 表示階段,目標(biāo)將會在什么階段執(zhí)行
inherited: 和上面的元素一樣,設(shè)置false maven將會拒絕執(zhí)行繼承給子插件
configuration: 表示此執(zhí)行的配置屬性
插件管理
pluginManagement:插件管理以同樣的方式包括插件元素,用于在特定的項目中配置。所有繼承于此項目的子項目都能使用。主要定義插件的共同元素
擴(kuò)展元素集合
主要包括以下的元素:
Directories
用于設(shè)置各種目錄結(jié)構(gòu),如下:
<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
表示需要擴(kuò)展的插件,必須包括進(jìn)相應(yīng)的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 插件能輸出相應(yīng)的定制和配置報表。
<reporting>
<plugins>
<plugin>
<outputDirectory>${basedir}/target/site</outputDirectory>
<artifactId>maven-project-info-reports-plugin</artifactId>
<reportSets>
<reportSet></reportSet>
</reportSets>
</plugin>
</plugins>
</reporting>
Report Sets
用于配置不同的目標(biāo),應(yīng)用于不同的報表
<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:創(chuàng)始年份
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
配置開發(fā)者信息
<developers>
<developer>
<id>eric</id>
<name>Eric</name>
<email>eredmond@codehaus.org</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>some.name@gmail.com</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>some.name@gmail.com</gtalk>
</properties>
</contributor>
</contributors>
環(huán)境設(shè)置
Issue Management
定義相關(guān)的bug跟蹤系統(tǒng),如bugzilla,testtrack,clearQuest等
<issueManagement>
<system>Bugzilla</system>
<url>http://127.0.0.1/bugzilla</url>;
</issueManagement>
Continuous Integration Management
連續(xù)整合管理,基于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>continuum@127.0.0.1</address></configuration>
</notifier>
</notifiers>
</ciManagement>
Mailing Lists
<mailingLists>
<mailingList>
<name>User List</name>
<subscribe>user-subscribe@127.0.0.1</subscribe>
<unsubscribe>user-unsubscribe@127.0.0.1</unsubscribe>
<post>user@127.0.0.1</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中的開發(fā)庫
Plugin Repositories
配置同 repositories
Distribution Management
用于配置分發(fā)管理,配置相應(yīng)的產(chǎn)品發(fā)布信息,主要用于發(fā)布,在執(zhí)行mvn deploy后表示要發(fā)布的位置
1 配置到文件系統(tǒng)
<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配置
編譯擴(kuò)展用于指定使用wagon外在ssh提供,用于提供你的文件到相應(yīng)的遠(yuǎn)程服務(wù)器。
<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 對應(yīng)于你的開發(fā)庫,用戶信息通過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>
參考鏈接:http://www.sta5.com/html/95/t-1295.html