现在的位置: 首页 > 综合 > 正文

项目进阶 之 持续构建环境搭建(三)Maven环境搭建

2013年01月07日 ⁄ 综合 ⁄ 共 3158字 ⁄ 字号 评论关闭

       上次的博文项目进阶 之 持续构建环境搭建(二)Nexus私服器中,我们搭建了一个Nexus的maven私服,这次我们来重点讲解一下Maven的安装和配置。这里说明一下这次的环境搭建,比较基础,但却非常核心,所以了解每一步的操作意义就非常重要。

1、前提

      安装mavne首先需要安装JDK,并配置环境变量。这部分比较简单,就不再做演示了。

2、安装

      直接解压文章末尾提供的apache-maven-3.1.1-bin.zip文件到D:\maven\apache-maven-3.1.1(可自定义文件位置)即可完成安装。

3、配置

      1)、环境变量

       右键 我的电脑->属性->高级系统设置->环境变量,进行如下设置。

      a、在用户变量中添加MAVEN_HOME=D:\maven\apache-maven-3.1.1

      b、修改PATH,在配置中添加%MAVEN_HOME%\bin;

      c、调出命令行窗口(win + r 之后输入cmd)

      d、输入mvn –version,出现界面如下,则安装Maven配置成功


      2)、settings.xml文件配置

      Maven中最重要的配置就是settings.xml文件的配置,其默认位置为%MAVEN_HOME%\conf\settings.xml,相关具体配置如下:

      a、全局settings.xml位于%MAVEN_HOME%\conf\settings.xml,在该文件中配置的任何选项对于使用maven的所有应用程序均会产生影响,且影响力最大。(该文件如果不做任何修改,在第一次启动maven之时会在当前用户的文件夹下建立一个.m2的文件夹,其中存放了maven本地的所有jar文件)



      b、用户settings.xml,拷贝%MAVEN_HOME%\conf\settings.xml文件到当前系统用户文件下的.m2下的settings.xml文件,并在其中修改为自定义的maven本地仓库存放位置(用户settings.xml并非一开始就有,它的意义在于不修改maven全局配置的情况下,更加合理对的配置用户自己的maven配置文件)。


<!--localRepository
| The path to the local repository mavenwill use to store artifacts.
|
| Default: ${user.home}/.m2/repository
<localRepository>/path/to/local/repo</localRepository>
-->
<!-- 自定义本地仓库存放位置 -->
<localRepository>D:/maven/repository</localRepository>

      c、自定义settings.xml文件,即拷贝%MAVEN_HOME%\conf\settings.xml文件到当前自定义用户自定义的maven本地仓库存放位置的同级目录下(自定义settings.xml同用户settings.xml一样,它的出现也是为了更加合理的使用maven的配置文件。)



修改settings.xml内容,具体如下:

<?xmlversion="1.0" encoding="UTF-8"?>
<settingsxmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0http://maven.apache.org/xsd/settings-1.0.0.xsd">

<!-- 设置本地仓库路径 -->
<localRepository>D:/maven/repository</localRepository>
<!-- 设置发布 jar 包时的用户名及密码 -->
<servers>
<server>
<id>releases</id>
<username>admin</username>
<password>admin123</password>
</server>

<server>
<id>snapshots</id>
<username>admin</username>
<password>admin123</password>
</server>               
</servers>
<!-- 设置 maven 的远程仓库为 nexus-->
<mirrors>
<mirror>
<id>nexus</id>
<mirrorOf>*</mirrorOf>
<name>LocalRepository</name>
<url>http://192.168.24.252:8081/nexus/content/groups/public</url>
</mirror>
</mirrors>
<!-- 设置 central 的路径等 -->
<profiles>
<profile>
<id>central</id>
<repositories>
<repository>
<id>central</id>
<name>Central</name>
<!-- 该 url 没有意义,可以随便写,但必须有。 -->
<url>http://localhost:8081/nexus/content/groups/public</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>central</id>
<name>localprivate nexus</name>
<url>http://localhost:8081/nexus/content/groups/public</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>

<!-- 激活 central -->
<activeProfiles>
<activeProfile>central</activeProfile>
</activeProfiles>

<!-- 配置eclipse插件 -->
<pluginGroups>
<pluginGroup>org.mortbay.jetty</pluginGroup>
<pluginGroup>org.codehaus.cargo</pluginGroup>               
</pluginGroups>
</settings>

      
        到此为止,对于Maven的配置我们就讲解完毕了。这里重点给大家强调一点的是,maven只是一个管理型的构建工具,它的功能很多,也有很多丰富的插件,很多人错误的认为maven只是一个构建工具,这一点是非常值得大家借鉴的。

        持续更新中,敬请期待!

apache-maven-3.1.1-bin.zip下载地址如下:

http://mirrors.hust.edu.cn/apache/maven/maven-3/3.1.1/binaries/apache-maven-3.1.1-bin.zip

抱歉!评论已关闭.