您的位置:首页 > 其它

使用nexus搭建maven仓库(本地私服)

2016-09-01 11:13 411 查看
我们在使用maven的时候,对于项目所依赖的jar包,maven默认会在中央仓库下载jar包,到本地的磁盘目录(如果没有配置则是用户目录下/.m2/repository文件夹下)。如果公司内部搭了一个maven私服的话,开发人员将仓库地址指向内网仓库地址,需要的jar包可直接在私服取,下载速度比远程要快,如果本地仓库没有则自动从远程下载并保存在本地。本文使用nexus搭建一个私服。      一、下载nexus      在浏览器中搜索 Sonatype Nexus 进入官网,找到下载 。下载之后解压压缩包如下:   二、安装nexus进入nexus/bin/jws文件夹下看到如下图:我的电脑是win7 32位,所以点开windows-x86-32 文件夹,双击install-nexus.bat 把nexus安装成一个服务,完成之后,在windows服务里边将看到如下图所示:这时候在浏览器输入http://localhost:8081/nexus  将看到如下页面,说明安装成功 三、配置nexus 在第二步完成之后,点击nexus主要右上角的 Log In按钮,管理员初始密码为 admin/adming123  登陆进去之点击 左侧的 【Repositories】菜单,会看的如下几个仓库右键单击 Apache Snapshots ,和central 仓库,在右键菜单中点击 Update Index 更新jar索引默认情况下,nexus下载的索引,和jar包会保存在 nexus 同级目录下sonatype-work文件夹下例如我的nexus安装目录为:D:\Devolopment\nexus-2.7   则 下载的jar和索引会保存在D:\Devolopment\sonatype-work 下,点开snoatype-work -->nexus 会看到 indexer、storage文件夹,可点进去看看一些简单配置:点击Apache Snapshots仓库,进行如下配置,开启远程索引下载,点击save按钮将现有的仓库,Apache snapshots ,3dpart,central 等仓库 加入 public 组中,直接在在界面中 将右侧的仓库移左边,效果如下:四、在maven中使用nexus 找到maven的配置文件,即:apache-maven-3.1.1/conf 下的settings.xml在mirrors节点下加入如下配置 

<mirrors>
<!-- mirror
| Specifies a repository mirror site to use instead of a given repository. The repository that
| this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
| for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
|
<mirror>
<id>mirrorId</id>
<mirrorOf>repositoryId</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://my.repository.com/repo/path</url>
</mirror>
-->

<mirror>
<id>nexus</id>
<mirrorOf>central</mirrorOf>
<name>internal nexus repository</name>
<url>http://localhost:8081/nexus/content/groups/public/</url>这一段配置,描述的便是 上一步中提到的那个Public Reposiories的信息,这个地址便是他的地址,这些写都可以再界面上看到
</mirror>这里的是localhost是在本机搭建测试用的,如果是公司内部的仓库的话,可自行修改成公司内网ip地址。
</mirrors>

   如此,配置之后,在我们使用maven的时候,就会通过私服了。而不是直接从远程取了。(没有配置之前,maven默认在远程的中央仓库下载) 在这个配置文件中,有个节点 叫localRepository 是用来配置 maven下载的jar包存放的地址的,如果不配置的话,jar包将会下载到c盘用户文件夹下.m2 文件夹下。此处可指定目录,如下
则maven下载的jar包会保存在 D:/Devolopment/MavenRepository 下 总结,大家nexus之后使用情形如下图 
转载自:使用nexus搭建maven仓库(本地私服)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: