maven 私服搭建

来源:互联网 发布:巡航导弹拦截 知乎 编辑:程序博客网 时间:2024/04/29 19:37

因为经常需要自行编译,每次从maven下载依赖都是一件很头疼的事情,而且不同的网络环境速度也不一样,因此在自己的笔记本(windows 64位)上自行搭建一个nexus oss maven仓库,是一件很必要的事情,本文记录了我搭建全部过程,以及遇到的全部问题:

下载

http://www.sonatype.org/nexus/ 
nexus-2.11.4-01-bundle.tar.gz 
注意下载bundle版本的

安装

解压后会有两个文件夹:nexus-2.11.4-01和sonatype-work,前者包含了运行环境和应用程序,后者是配置和数据。

注意要以管理员方式打开命令提示符: 
进入目录:\nexus-2.11.4-01\bin\jsw\windows-x86-64 
运行:

install-nexus.bat
  • 1
  • 1

然后运行:

start-nexus.bat
  • 1
  • 1

结果如下所示:

wrapper  | Starting the nexus service...wrapper  | Waiting to start...wrapper  | Waiting to start...wrapper  | nexus started.
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

运行

访问:http://localhost:8081/nexus/ 
会有如下所示: 
这里写图片描述

配置

1. 登录

首先点击右上角进行登录:账号:admin 密码 : admin123

2. 配置仓库

登录后,点击左侧Repositories,默认会显示一些仓库, 
这里写图片描述 
如图,标红的这一行public Repository就是我们配的私服,注意他的Type是group,意思就是:所有请求设定仓库的request都会转向我们假设的私服。 在public Repository的Configuration中,左侧的就是我们所设定的仓库,如果我们在项目中设定了这些仓库,那么所有发向这些仓库的请求都会先访问们的私服,如果我们的私服中有想要的jar包等,就直接从私服下载。 
这里写图片描述

3. 验证联通 
我们点击Repositories中的Central,看看是否能联通,在Routing界面中,查看仓库状态是否成功: 
这里写图片描述

如果未成功,看看你是否在代理环境下,如果是的话,请设置代理。在左侧菜单栏,Administration/Server中设置:

4. index下载 
首先在central中的Configuration中,将Downlad Remote Indexes设置为True。 
这里写图片描述 
然后右键Central,Repair Index。 
这里写图片描述 
同样,在Public Repositories,也右键进行Repair。 
这个时候,我们可以通过查看Scheduled Tasks,看到有一个Task正在运行。 
这里写图片描述

稍等片刻,若成功的话。可在Browse Index中查看到列表: 
这里写图片描述

如果你的列表是空的话等一等,因为文件比较大,再加上咱们伟大的墙,所以有时候会很久没反映。(实在不行翻一下吧)

下载的文件可在:sonatype-work\nexus\indexer中找到

5. 连接私服 
终于到了最后一步了,现在我们在项目的pom.xml中加入如下内容:

<repositories>        <repository>            <id>public</id>            <name>Public Repositories</name>            <url>http://localhost:8081/nexus/content/groups/public/</url>        </repository>    </repositories>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

这样我们再刷新项目的时候,就会从私服中下载了,若私服中没有则会从我们之前设置的Ordered Group Repositories从第一个开始寻找,因此我们要将最常用最快的仓库放在第一个。 
下载成功后,我们可以在ui中看到: 
这里写图片描述

当然了你也可以配置全局的,而不需要每个项目都配置,那就是在setting.xml中配置mirror。setting.xml在maven的安装目录的conf目录中(全局),或者在用户的家目录.m2/下(用户特定),在配置文件中加入:

<mirrors> <mirror>    <id>bbq</id>    <mirrorOf>*</mirrorOf>    <name>Nexus osc bbq</name>    <url>http://localhost:8081/nexus/content/groups/public/</url></mirror></mirrors>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

尽情享受高速的maven镜像吧!