---
title: "【连载】Maven系列（四）——配置私服"
author: "ITsource源代码"
date: "2015-07-24T09:42:55Z"
tags: []
categories: ["编程语言"]
slug: "maven"
draft: false
lastmod: "2015-07-24T09:42:55Z"
id: 2229837
url: "https://it-source.iteye.com/blog/2229837"
---

## 相关文章

[1.《用起来超爽的Maven——入门篇》](/admin/blogs/2211033 "用起来超爽的Maven——入门篇")

[2.《用起来超爽的Maven——进阶篇》](/admin/blogs/2212395 "用起来超爽的Maven——进阶篇")

[3.《Maven系列（三） 进阶》](/admin/blogs/2229369 "Maven系列（三）——进阶")

 

## 一、为什么需要私服

有些公司并不提供外网给程序员，因此不能使用maven访问远程的仓库地址，所以很有必要在局域网里找一台有外网权限的机器；搭建nexus私服，然后程序员连到这台私服上，这样的话就可以通过这台搭建了nexus私服的电脑访问maven的远程仓库。

![](http://www.itsource.cn/upload/news/2015-07-22/f158eef3-5b64-456c-bba9-c548369e9dad.png) 

## 二、配置步骤

 

第一步：下载nexus-2.6.4-02.war包，然后拷贝到tomcat下的webapps目录中

[http://www.sonatype.org/nexus/go](http://www.sonatype.org/nexus/go)     [Download the Nexus WAR distribution](http://www.sonatype.org/downloads/nexus-latest.war)

第二步：启动tomcat

第三步：访问[http://localhost:8088/nexus-2.6.4-02](http://localhost:8080/nexus/)显示如下：

 

![](http://www.itsource.cn/upload/news/2015-07-22/a4fd531a-a81f-4d34-ae10-a53c0c281429.png) 

 

第四步：点击右上角“log in” ，输入username：admin 和Password：admin123登录

![](http://www.itsource.cn/upload/news/2015-07-22/0468e5e5-7043-4ac5-b541-0f3103651dc4.png) 

 

 

第五步：登录成功

![](http://www.itsource.cn/upload/news/2015-07-22/d64f0641-5f0e-43f9-83ef-0ea5eec94224.png) 

 

第六步：点击Views/Repositories 中Repositories

 

![](http://www.itsource.cn/upload/news/2015-07-22/49a35026-c12d-4332-8c07-5a5ccbf262fe.png) 

 

Nexus内置仓库说明：

（1）Maven Central：该仓库代理Maven中央仓库，其策略为Release，因此只会下载和缓存中央仓库中的发布版本构件。

（2）Releases：这是一种策略为Release的宿主类型仓库，用来部署组织内部的发布版本构件。

（3）Snapshots：这是一个策略为Snapshot的宿主类型仓库，用来部署组织内部的快照版本构件。

（4）3rd party：这是一个策略为Release的宿主类型仓库，用来部署无法从公共仓库获得的第三方发布版本构件。

（5）Public Repositories：该仓库组将上述所有策略为Release的仓库聚合并通过一致的地址提供服务。

 

第七步：创建宿主目录和代理仓库

 

Hosted：本地仓库，通常我们会部署自己的构件到这一类型的仓库。 包括3rd party仓库，Releases仓库，Snapshots仓库

Proxy：代理仓库，它们被用来代理远程的公共仓库，如maven中央仓库。 

Group：仓库组，用来合并多个hosted/proxy仓库，通常我们配置maven依赖仓库组。 

 

第八步：创建仓库组

 

点击Public Repositories仓库，在Configurations栏中选取需要合并的仓库,点击箭头加到左边保存即可

![](http://www.itsource.cn/upload/news/2015-07-22/d39fd95a-376a-41e3-8466-2cb70ee31b8c.png) 

 

第九步：下载Index索引并进行构建搜索（GAV搜索）

点击Views/Repositories菜单下面的Repositories，将这三个仓库 Apache Snapshots，Codehaus Snapshots，Central的Download Remote Indexes修改为 true。然后在这三个仓库上分别右键，选择Repari index，这样Nexus就会去下载远程的索引文件。

 

第十步：配置所有构建均从私服下载，在~/.m2/setting.xml中配置如下：

&lt;settings&gt;

 &lt;mirrors&gt;

 &lt;mirror&gt;

 &lt;!--此处配置所有的构建均从私有仓库中下载 \*代表所有，也可以写central --&gt;

 &lt;id&gt;nexus&lt;/id&gt;

 &lt;mirrorOf&gt;\*&lt;/mirrorOf&gt;

 &lt;url&gt;http://localhost:8088/nexus-2.6.4-02/content/groups/public&lt;/url&gt;

 &lt;/mirror&gt;

 &lt;/mirrors&gt;

 &lt;profiles&gt;

 &lt;profile&gt;

&lt;id&gt;nexus&lt;/id&gt;

&lt;repositories&gt;

&lt;repository&gt;

&lt;id&gt;nexus&lt;/id&gt;

&lt;name&gt;local private nexus&lt;/name&gt;

&lt;url&gt;http://localhost:8088/nexus-2.6.4-02/content/groups/public&lt;/url&gt;

&lt;/repository&gt;

&lt;/repositories&gt;

&lt;/profile&gt;

&lt;profile&gt;

&lt;id&gt;nexus-snapshots&lt;/id&gt;

&lt;repositories&gt;

&lt;repository&gt;

&lt;id&gt;nexus-snapshots&lt;/id&gt;

&lt;name&gt;local private nexus snapshots&lt;/name&gt;

&lt;url&gt;http://localhost:8088/nexus-2.6.4-02/content/groups/public-snapshots&lt;/url&gt;

&lt;/repository&gt;

&lt;/repositories&gt;

&lt;/profile&gt;

 &lt;/profiles&gt;

 

&lt;activeProfiles&gt;

 &lt;!--make the profile active all the time --&gt;

 &lt;activeProfile&gt;nexus&lt;/activeProfile&gt;

 &lt;/activeProfiles&gt;

 

 

第十一步：Nexus的访问权限控制，在~/m2/setting.xml中配置如下：

&lt;!-- 设置发布时的用户名 --&gt;

 &lt;servers&gt;

  &lt;server&gt;

  &lt;id&gt; releases &lt;/id&gt;

&lt;username&gt;admin&lt;/username&gt;

&lt;password&gt;admin123&lt;/password&gt;

&lt;/server&gt;

&lt;server&gt;

&lt;id&gt; snapshots &lt;/id&gt;

&lt;username&gt;admin&lt;/username&gt;

&lt;password&gt;admin123&lt;/password&gt;

 &lt;/server&gt;

 &lt;/servers&gt;

 

第十二步：

 

部署构建到Nexus，包含Release和Snapshot， 在项目根目录中pom.xml中配置：

&lt;distributionManagement&gt; 

&lt;repository&gt; 

    &lt;id&gt;releases&lt;/id&gt; 

    &lt;name&gt;Internal Releases&lt;/name&gt; 

    &lt;url&gt;http://localhost:8088/nexus-2.6.4-02/content/repositories/releases/&lt;/url&gt; 

&lt;/repository&gt; 

&lt;snapshotRepository&gt; 

    &lt;id&gt;snapshots&lt;/id&gt; 

    &lt;name&gt;Internal Snapshots&lt;/name&gt; 

    &lt;url&gt;http://localhost:8088/nexus-2.6.4-02/content/repositories/snapshots/&lt;/url&gt; 

&lt;/snapshotRepository&gt; 

  &lt;/distributionManagement&gt;

 

第十三步：pom.xml

&lt;distributionManagement&gt; 

&lt;repository&gt; 

    &lt;id&gt;releases&lt;/id&gt; 

    &lt;name&gt;Internal Releases&lt;/name&gt; 

    &lt;url&gt;http://localhost:8088/nexus-2.6.4-02/content/repositories/releases/&lt;/url&gt; 

&lt;/repository&gt; 

&lt;snapshotRepository&gt; 

    &lt;id&gt;snapshots&lt;/id&gt; 

    &lt;name&gt;Internal Snapshots&lt;/name&gt; 

    &lt;url&gt;http://localhost:8088/nexus-2.6.4-02/content/repositories/snapshots/&lt;/url&gt; 

&lt;/snapshotRepository&gt; 

  &lt;/distributionManagement&gt;

 

 

你还在等什么，赶快试试吧。

![](http://www.itsource.cn/upload/news/2015-07-22/0072a106-cb8a-4dd7-bb73-26c3024e3c5e.jpg)

 

**关注作者**

**[![](http://www.itsource.cn/upload/news/2015-07-22/c41dc280-760d-4ffd-94b9-45b93507d984.png)](http://weibo.com/lsqlester)      [![](http://www.itsource.cn/upload/news/2015-07-22/34c51b48-fffa-4167-a0c9-f99e285c1c35.png)](http://t.qq.com/itsource_lansq)**