1-Maven是什么?从专业名次解释的话,就是基于项目对象模型(POM),可以通过一小段描述信息来管理项目的构建,报告和文档的软件项目管理工具。

我们可以利用Maven来管理我们的项目,在配置文件pom.xml中配置我们项目需要的各种jar包,及版本信息;当然,这是需要一个‘中央仓库’来给我们提供这些jar包的。

如果你的eclipse或MyEclipse没有自带安装maven插件的话,也就是说,在菜单栏Window\preferences下找不到Maven选项,如图示-

Maven使用_eclipse

那你就的自己安装maven插件了,下载插件,我下的是:eclipse-maven3-plugin.rar

然后在你的IDE安装根目录下,建立两个目录- links 和 mavenPlugins,如图示-

Maven使用_Maven_02

然后,将插件解压,将插件包中的features和 plugins 两个目录,拷贝到mavenPlugins目录下。然后,在links目录下建立一个以link为扩展名的文件-maven.link,里面内容为:mavenPlugins目录的磁盘绝对路径,如:path=D:/eclipse/mavenPlugins

OK,将IDE重新启动,你就会发现,maven插件安装完成!

 

2-选择-Window\preferences\Maven\Installations,添加(Add)maven目录,如图示-

Maven使用_eclipse_03

[注]1:UserSettings选项可以自己选择,如图:

Maven使用_ide_04

[注]2:默认,情况下maven下载的jar包都是放在系统用户目录下,如果你想更改的话,可以修改

settings.xml文件中的<localRepository/>本地仓库属性,如:

<localRepository>E:\eclipseWorkspace1\mavenRepository</localRepository>

 

然后,你就可以建立Maven Project了;

 

3- 建立Maven工程

-|New 一个Maven Project

Maven使用_eclipse_05

-|使用默认工作空间 或是 新建空间 或是 导入maven工程 都可以

 

Maven使用_eclipse_06

-|模版选择,选择基于web的模版

 

Maven使用_Maven_07

-|Maven项目 信息

Maven使用_ide_08

Group Id:项目的普遍唯一识别符。

Artifact Id:在给groupID的group里面为artifact指定的标识符是唯一的artifact代表的是被制作或者被一个project应用的组件。

Version:项目产生的artifact的当前版本。

Packaging:项目生产出来的artifact类型。

如图示-

 

Maven使用_ide_09

一个Maven工程,就创建OK了

 

附- settings.xml基本属性

localRepository:本地仓库位置,默认在操作系统,用户目录下的 .m2/repository,可以更改,如上所述;

<!-- localRepository
| The path to the localrepository maven will use to store artifacts.
|
| Default:${user.home}/.m2/repository
<localRepository>/path/to/local/repo</localRepository>
-->
offline:离线开关,设置是否每次构建都从远程仓库下载,默认为false;

<!-- offline
| Determines whether maven shouldattempt to connect to the network when executing a build.
| This will have an effect onartifact downloads, artifact deployment, and others.
|
| Default: false
<offline>false</offline>
-->
servers:对应POM文件的distributionManagement元素里定义id,和登陆服务器的用户名、密码;
<servers>
<!-- server
| Specifies the authenticationinformation to use when connecting to a particular server, identified by
| a unique name within thesystem (referred to by the 'id' attribute below).
|
| NOTE: You should either specify username/password ORprivateKey/passphrase, since these pairings are
| used together.
|
<server>
<id>deploymentRepo</id>
<username>repouser</username>
<password>repopwd</password>
</server>
-->

<!-- Another sample, usingkeys to authenticate.
<server>
<id>siteServer</id>
<privateKey>/path/to/private/key</privateKey>
<passphrase>optional;leave empty if not used.</passphrase>
</server>
-->
</servers>
mirrors:仓库镜像,用于将仓库地址指向自定义仓库地址(id:新的镜像ID,name:镜像名称,url:镜像地址,mirrorOf:以那个地址做镜像,默认为central-中央仓库)
<mirrors>
<!-- mirror
| Specifies a repository mirrorsite to use instead of a given repository. The repository that
| this mirror serves has an IDthat matches the mirrorOf element of this mirror. IDs are used
| for inheritance and directlookup purposes, and must be unique across the set of mirrors.
|
<mirror>
<id>mirrorId</id>
<mirrorOf>repositoryId</mirrorOf>
<name>Human ReadableName for this Mirror.</name>
<url>http://my.repository.com/repo/path</url>
</mirror>
-->
</mirrors>

Maven使用_Maven_10