maven2之m2eclipse使用手册之一简介与配置文件settings.xml

来源:互联网 发布:西安智汇诚网络怎么样 编辑:程序博客网 时间:2024/05/22 12:02

这是我在无意之中发现的一系列好文章,现在给大家转载过来。

近期因朋友项目中要用到maven2,所以顺带学习了一下,由于个人比较讨厌cmd的命令使用maven,所以没有去apache下在maven的安装文件使用,而eclipse正好提供了关于maven2的插件,结果发现不需要安装maven2的安装包即可拥有maven2的命令功能.本文章基于maven-2.2.1和m2eclipse0.10.0版本和eclipse-reporting-galileo-SR2版本(以包含eclipse-jee-galileo-SR2) 所写,如有跟其他版本有所出入请见谅

m2eclipse在线安装地址如下:

http://m2eclipse.sonatype.org/sites/m2e

安装前提必须Eclipse要求已经安装了以下插件:

subclipse(svn) 在线安装地址:http://subclipse.tigris.org/update_1.6.x

Mylyn在线安装地址:http://download.eclipse.org/tools/mylyn/update/e3.4/
Mylyn Extras (JIRA 支持): http://download.eclipse.org/tools/mylyn/
update/extras

AspectJ Tools Platform (AJDT)在线安装地址:http://download.eclipse.org/tools/ajdt/35/dev/update

Web Tools Platform (WTP)在线安装地址:http://download.eclipse.org/webtools/updates/

由于本机没有独立安装maven2的安装包,所以安装完毕后是没有setting.xml这个配置文件的,启动eclipse后如果没有之前没有指定本地jdk路径的话,会提示要求指定jdk在那个地方,编辑eclipse.ini加入以下两段文字:

-vm
C:/Program Files/Java/jdk1.6.0_20/bin

-vm:是指定当前虚拟机的位置,默认安装好的eclipse是没有该项的,记住-vm标记必须要在-vmargs前面加否则会出现报错,

C:/Program Files/Java/jdk1.6.0_20/bin:就是你本机jdk bin的位置了

对于eclipse.ini的位置是在你所解压的eclipse中的根目录例如我的就是在E:\JavaWorkingTools\IDETools\Eclipse\IDE\eclipse\eclipse.ini

还有一个很重要的东西,m2eclipse已经为你创建了一个{user.dir}/.m2/repository的本地中央仓库的文件夹,你可以通过配置settings.xml来更改本地中央仓库的文件夹

{user.dir}:就是你当前用户下的文件夹,例如我的是Edward

相对于maven2来说maven2的设置比maven1简单多了只有setting.xml和pom.xml。setting.xml用于配置对于仓库的设置和代理仓库等设置,而pom.xml则对当前项目的管理。

由于没有使用到maven2的安装包,装完m2eclipse是没有settings.xml的文件的,需要自己手动新建一个settings.xml文件.

对于settings.xml文件

settings.xml基本结构如下:

 <settings 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/settings-1.0.0.xsd">  <localRepository/>  <interactiveMode/>  <usePluginRegistry/>  <offline/>  <pluginGroups/>  <servers/>  <mirrors/>  <proxies/>  <profiles/>  <activeProfiles/></settings> 


 

settings.xml标签说明:

如下图:

image

主要的配置因素:

localRepository:表示本地库的保存位置,也就是maven2主要的jar保存位置,默认在${user.dir}/.m2/repository,如果需要另外设置,就换成其他的路径。

offline:如果不想每次编译,都去查找远程中心库,那就设置为true。当然前提是你已经下载了必须的依赖包。

Servers
在POM中的 distributionManagement元素定义了开发库。然而,特定的username和pwd不能使用于pom.xml,所以通过此配置来保存server信息

<servers>    <server>      <id>server001</id>      <username>test</username>      <password>test</password>      <privateKey>${usr.home}/.ssh/id_dsa</privateKey>      <passphrase>some_passphrase</passphrase>      <filePermissions>664</filePermissions>      <directoryPermissions>775</directoryPermissions>      <configuration></configuration>    </server>  </servers> 

 

  • id:server 的id,用于匹配distributionManagement库id,比较重要。
  • username, password:用于登陆此服务器的用户名和密码
  • privateKey, passphrase:设置private key,以及passphrase
  • filePermissions, directoryPermissions:当库文件或者目录创建后,需要使用权限进行访问。参照unix文件许可,如664和775

Mirrors 表示镜像库,指定库的镜像,用于增加其他库(非常有用的一个设置)

<mirrors>    <mirror>      <id>planetmirror.com</id>      <name>PlanetMirror Australia</name>   <url>http://downloads.planetmirror.com/pub/maven2</url>      <mirrorOf>central</mirrorOf>    </mirror>  </mirrors> 


 

  • id,name:唯一的标志,用于区别镜像
  • url:镜像的url
  • mirrorOf:身份认证有 中心的:central 单一: *对于2.0.9版本以上还有
    • * = everything
    • external:* = everything not on the localhost and not file based.
    • repo,repo1 = repo or repo1
    • *,!repo1 = everything except repo1

Proxies
此设置,主要用于无法直接访问中心的库用户配置。

<proxies>    <proxy>      <id>myproxy</id>  <active>true</active>      <protocol>http</protocol>     <host>proxy.somewhere.com</host>      <port>8080</port>      <username>proxyuser</username>      <password>somepassword</password>      <nonProxyHosts>*.google.com|ibiblio.org</nonProxyHosts>    </proxy>  </proxies> 


 

  • id:代理的标志
  • active:是否激活代理
  • protocol, host, port:protocol://host:port 代理
  • username, password:用户名和密码
  • nonProxyHosts: 不需要代理的host

Profiles
  类似于pom.xml中的profile元素,主要包括activation,repositories,pluginRepositories 和properties元素
  刚开始接触的时候,可能会比较迷惑,其实profiles是maven2中比较强大的功能。从字面上来说,就是个性配置。
  单独定义profile后,并不会生效,需要通过满足条件来激活。

repositories 和pluginRepositories
定义其他开发库和插件开发库。对于团队来说,肯定有自己的开发库。可以通过此配置来定义。
如下的配置,定义了本地开发库,用于release 发布。

<repositories>        <repository>          <id>repo-local</id>       <name>Internal 开发库</name>    <url>http://localhost:8081/repo-local</url>          <releases>            <enabled>true</enabled>            <updatePolicy>never</updatePolicy>            <checksumPolicy>warn</checksumPolicy>          </releases>          <snapshots>            <enabled>false</enabled>          </snapshots>          <layout>default</layout>        </repository>      </repositories>      <pluginRepositories>    <pluginRepository>    <id>repo-local</id>    <name>Internal 开发库</name>    <url>http://localhost:8081/repo-local</url>    <releases>            <enabled>true</enabled>            <updatePolicy>never</updatePolicy>            <checksumPolicy>warn</checksumPolicy>    </releases>    <snapshots>    <enabled>false</enabled>    </snapshots>    <layout>default</layout>    </pluginRepository>    </pluginRepositories>


releases, snapshots:每个产品的版本的Release或者snapshot(注:release和snapshot的区别,release一般是比较稳定的版本,而snapshot基本上不稳定,只是作为快照)
properties
  maven 的properties作为placeholder值,如ant的properties

包括以下的5种类型值:

  1. env.X:返回当前的环境变量
  2. project.x:返回pom中定义的元素值,如project.version
  3. settings.x:返回settings.xml中定义的元素
  4. java 系统属性:所有经过java.lang.System.getProperties()返回的值
  5. x:用户自己设定的值

Activation
  用于激活此profile

<activation>        <activeByDefault>false</activeByDefault>        <jdk>1.5</jdk>        <os>          <name>Windows Seven</name>          <family>Windows</family>          <arch>x86</arch>          <version>5.1.2600</version>        </os>        <property>          <name>mavenVersion</name>          <value>2.0.9</value>        </property>        <file>          <exists>${basedir}/file2.properties</exists>          <missing>${basedir}/file1.properties</missing>        </file>      </activation> 


 

  • jdk:如果匹配指定的jdk版本,将会激活
  • os:操作系统
  • property:如果maven能检测到相应的属性
  • file: 用于判断文件是否存在或者不存在


除了使用activation来激活profile,同样可以通过activeProfiles来激活
Active Profiles

表示激活的profile,通过profile id来指定。

<activeProfiles>    <activeProfile>env-test</activeProfile> 指定的profile id  </activeProfiles>


 

原创粉丝点击