安装Maven以前要确保已经安装好了jdk,而且配置好了环境变量JAVA_HOME。具体安装步骤以下:html
通过以上几步以后Maven就安装成功了。接下来咱们能够在命令窗口使用mvn --version来验证一下Maven是否安装成功。如能正确输出Maven的安装版本,则表示它安装成功了。linux
在Maven中提供了一个settings.xml文件来定义Maven的全局环境信息。这个文件会存在于Maven的安装目录的conf子目录下面,或者是用户家目录的.m2子目录下面。咱们能够经过这个文件来定义本地仓库、远程仓库和联网使用的代理信息等。apache
其实相对于多用户的PC机而言,在Maven安装目录的conf子目录下面的settings.xml才是真正的全局的配置。而用户家目录的.m2子目录下面的settings.xml的配置只是针对当前用户的。当这两个文件同时存在的时候,那么对于相同的配置信息用户家目录下面的settings.xml中定义的会覆盖Maven安装目录下面的settings.xml中的定义。用户家目录下的settings.xml文件通常是不存在的,可是Maven容许咱们在这里定义咱们本身的settings.xml,若是须要在这里定义咱们本身的settings.xml的时候就能够把Maven安装目录下面的settings.xml文件拷贝到用户家目录的.m2目录下,而后改为本身想要的样子。windows
先来看一个基本的settings.xml的样子:服务器
1 <?xml version="1.0" encoding="UTF-8"?> 2 <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd"> 5 6 <localRepository>D:\\develop\\mavenRepository</localRepository> 7 <interactiveMode>true</interactiveMode> 8 <offline>false</offline> 9 <pluginGroups> 10 11 </pluginGroups> 12 13 <proxies> 14 <proxy> 15 <id>optional</id> 16 <active>true</active> 17 <protocol>http</protocol> 18 <username>proxyuser</username> 19 <password>proxypass</password> 20 <host>proxy.host.net</host> 21 <port>80</port> 22 <nonProxyHosts>local.net|some.host.com</nonProxyHosts> 23 </proxy> 24 </proxies> 25 26 <servers> 27 <server> 28 <id>deploymentRepo</id> 29 <username>repouser</username> 30 <password>repopwd</password> 31 </server> 32 </servers> 33 34 <mirrors> 35 <mirror> 36 <id>mirrorId</id> 37 <mirrorOf>repositoryId</mirrorOf> 38 <name>Human Readable Name for this Mirror.</name> 39 <url>http://my.repository.com/repo/path</url> 40 </mirror> 41 </mirrors> 42 43 <profiles> 44 <profile> 45 <id>jdk-1.5</id> 46 <activation> 47 <jdk>1.5</jdk> 48 </activation> 49 <repositories> 50 <repository> 51 <id>jdk15</id> 52 <name>jdk1.5</name> 53 <url>http://www.myhost.com/maven/jdk15</url> 54 <layout>default</layout> 55 <snapshotPolicy>always</snapshotPolicy> 56 </repository> 57 </repositories> 58 </profile> 59 </profiles> 60 <activeProfiles> 61 <activeProfile>jdk-1.5</activeProfile> 62 </activeProfiles> 63 </settings>
settings.xml中主要包括如下元素:maven
localRepository:表示Maven用来在本地储存信息的本地仓库的目录。默认是用户家目录下面的.m2/repository目录。ide
interactiveMode:表示是否使用交互模式,默认是true;若是设为false,那么当Maven须要用户进行输入的时候,它会使用一个默认值。this
offline:表示是否离线,默认是false。这个属性表示在Maven进行项目编译和部署等操做时是否容许Maven进行联网来下载所须要的信息。url
pluginGroups:在pluginGroups元素下面能够定义一系列的pluginGroup元素。表示当经过plugin的前缀来解析plugin的时候到哪里寻找。pluginGroup元素指定的是plugin的groupId。默认状况下,Maven会自动把org.apache.maven.plugins和org.codehaus.mojo添加到pluginGroups下。spa
proxies:其下面能够定义一系列的proxy子元素,表示Maven在进行联网时须要使用到的代理。当设置了多个代理的时候第一个标记active为true的代理将会被使用。下面是一个使用代理的例子:
1 <proxies> 2 <proxy> 3 <id>xxx</id> 4 <active>true</active> 5 <protocol>http</protocol> 6 <username>用户名</username> 7 <password>密码</password> 8 <host>代理服务器地址</host> 9 <port>代理服务器的端口</port> 10 <nonProxyHosts>不使用代理的主机</nonProxyHosts> 11 </proxy> 12 </proxies>
servers:其下面能够定义一系列的server子元素,表示当须要链接到一个远程服务器的时候须要使用到的验证方式。这主要有username/password和privateKey/passphrase这两种方式。如下是一个使用servers的示例:
1 <servers> 2 <server> 3 <id>id</id> 4 <username>用户名</username> 5 <password>密码</password> 6 </server> 7 </servers>
mirrors:用于定义一系列的远程仓库的镜像。咱们能够在pom中定义一个下载工件的时候所使用的远程仓库。可是有时候这个远程仓库会比较忙,因此这个时候人们就想着给它建立镜像以缓解远程仓库的压力,也就是说会把对远程仓库的请求转换到对其镜像地址的请求。每一个远程仓库都会有一个id,这样咱们就能够建立本身的mirror来关联到该仓库,那么之后须要从远程仓库下载工件的时候Maven就能够从咱们定义好的mirror站点来下载,这能够很好的缓解咱们远程仓库的压力。在咱们定义的mirror中每一个远程仓库都只能有一个mirror与它关联,也就是说你不能同时配置多个mirror的mirrorOf指向同一个repositoryId。
看如下是一个使用mirrors的例子:
1 <mirrors> 2 <mirror> 3 <id>mirrorId</id> 4 <mirrorOf>repositoryId</mirrorOf> 5 <name>定义一个容易看懂的名称 </name> 6 <url>http://my.repository.com/repo/path</url> 7 </mirror> 8 </mirrors>
1》 id:是用来区别mirror的,全部的mirror不能有相同的id
2》 mirrorOf:用来表示该mirror是关联的哪个仓库,其值为其关联仓库的id。当要同时关联多个仓库时,这多个仓库之间能够用逗号隔开;当要关联全部的仓库时,可使用“*”表示;当要关联除某一个仓库之外的其余全部仓库时,能够表示为“*,!repositoryId”;当要关联不是localhost或用file请求的仓库时,能够表示为“external:*”。
3》 url:表示该镜像的url。当Maven在创建系统的时候就会使用这个url来链接到咱们的远程仓库。
profiles:用于指定一系列的profile。profile元素由activation、repositories、pluginRepositories和properties四个元素组成。当一个profile在settings.xml中是处于活动状态而且在pom.xml中定义了一个相同id的profile时,settings.xml中的profile会覆盖pom.xml中的profile。
(1)activation:这是profile中最重要的元素。跟pom.xml中的profile同样,settings.xml中的profile也能够在特定环境下改变一些值,而这些环境是经过activation元素来指定的。
看下面一个例子:
1 <profiles> 2 <profile> 3 <id>test</id> 4 <activation> 5 <activeByDefault>false</activeByDefault> 6 <jdk>1.6</jdk> 7 <os> 8 <name>Windows 7</name> 9 <family>Windows</family> 10 <arch>x86</arch> 11 <version>5.1.2600</version> 12 </os> 13 <property> 14 <name>mavenVersion</name> 15 <value>2.0.3</value> 16 </property> 17 <file> 18 <exists>${basedir}/file2.properties</exists> 19 <missing>${basedir}/file1.properties</missing> 20 </file> 21 </activation> 22 ... 23 </profile> 24 </profiles>
在上面这段代码中,当全部的约束条件都知足的时候就会激活这个profile。
1》 jdk:表示当jdk的版本知足条件的时候激活,在这里是1.6。这里的版本还能够用一个范围来表示,如
<jdk>[1.4,1.7)</jdk>表示1.四、1.5和1.6知足;
<jdk>[1.4,1.7]</jdk>表示1.四、1.五、1.6和1.7知足;
2》 os:表示当操做系统知足条件的时候激活。
3》 property:property是键值对的形式,表示当Maven检测到了这样一个键值对的时候就激活该profile。
(1)下面的示例表示当存在属性hello的时候激活该profile。
1 <property> 2 <name>hello</name> 3 </property>
(2)下面的示例表示当属性hello的值为world的时候激活该profile。
1 <property> 2 <name>hello</name> 3 <value>world</value> 4 </property>
这个时候若是要激活该profile的话,能够在调用Maven指令的时候加上参数hello并指定其值为world,如:
mvn compile –Dhello=world
1》 file:表示当文件存在或不存在的时候激活,exists表示存在,missing表示不存在。以下面的例子表示当文件hello/world不存在的时候激活该profile。
1 <profile> 2 <activation> 3 <file> 4 <missing>hello/world</missing> 5 </file> 6 </activation> 7 </profile>
2》 activeByDefault:当其值为true的时候表示若是没有其余的profile处于激活状态的时候,该profile将自动被激活。
(2)properties:用于定义属性键值对的。当该profile是激活状态的时候,properties下面指定的属性均可以在pom.xml中使用。
(3)repositories:用于定义远程仓库的,当该profile是激活状态的时候,这里面定义的远程仓库将做为当前pom的远程仓库。
1 <repositories> 2 <repository> 3 <id>codehausSnapshots</id> 4 <name>Codehaus Snapshots</name> 5 <releases> 6 <enabled>false</enabled> 7 <updatePolicy>always</updatePolicy> 8 <checksumPolicy>warn</checksumPolicy> 9 </releases> 10 <snapshots> 11 <enabled>true</enabled> 12 <updatePolicy>never</updatePolicy> 13 <checksumPolicy>fail</checksumPolicy> 14 </snapshots> 15 <url>http://snapshots.maven.codehaus.org/maven2</url> 16 <layout>default</layout> 17 </repository> 18 </repositories>
1》 releases、snapshots:这是对于工件的类型的限制。
2》 enabled:表示这个仓库是否容许这种类型的工件
3》 updatePolicy:表示多久尝试更新一次。可选值有always、daily、interval:minutes(表示每多久更新一次)和never。
4》 checksumPolicy:当Maven在部署项目到仓库的时候会连同校验文件一块儿提交,checksumPolicy表示当这个校验文件缺失或不正确的时候该如何处理,可选项有ignore、fail和warn。
(4)pluginRepositories:在Maven中有两种类型的仓库,一种是存储工件的仓库,另外一种就是存储plugin插件的仓库。pluginRepositories的定义和repositories的定义相似,它表示Maven在哪些地方能够找到所须要的插件。
activeProfiles:底包含一系列的activeProfile元素,表示对于全部的pom都处于活跃状态的profile。如:
1 <activeProfiles> 2 <activeProfile>alwaysActiveProfile</activeProfile> 3 <activeProfile>anotherAlwaysActiveProfile</activeProfile> 4 </activeProfiles>
参考转载:haohaoxuexi.iteye.com/blog/1827778