Nexus 私服搭建&Maven使用教程
Nexus搭建教程
By:wumeng
前言:
私服是架设在局域网的一种特殊的远程仓库,目的是代理远程仓库及部署第三方构件。有了私服之后,当 Maven 需要下载构件时,直接请求私服,私服上存在则下载到本地仓库;否则,私服请求外部的远程仓库,将构件下载到私服,再提供给本地仓库下载。
公司一般都有个自己的私服来管理各种jar包,原因大概有这么3个,分别是:
1、有的公司不能访问外网,只能通过私服来管理jar包和插件;
2、公司网速比较慢,通过公司的私服来获取jar包比较快;
3、内部的的一些jar包放在私服上,比较方便的大家使用。
Nexus环境的搭建:
该搭建环境是windows环境,linux也是一样步骤。
1. 下载Nexus安装文件:https://www.sonatype.com/download-oss-sonatype
http://www.sonatype.org/nexus/
NEXUS OSS [OSS = Open Source Software,开源软件——免费]
NEXUS PROFESSIONAL -FREE TRIAL [专业版本——收费]。
所以选择NEXUS OSS
2.
最新版本是:nexus-2.14.3-02bundle,点击下载。
3. 将压缩文件解压缩到任意文件或者磁盘目录,解压缩获得两个文件,如图:
4. 在任意磁盘或文件夹下创建nexus文件夹,将两个文件拷贝到这个文件夹中。
5. 然后进入nexus-2.14.3-02\bin\jsw下选择对应的系统,如图:
6. 我的系统是windows 64位的(该windows包含xp,win7,win8,win10),进入该目录。
如需要安装位windows服务,需要在命令cmd中运行 nexus install
7. 启动nexus或者服务
8. nexus私服就已经搭建并部署完成,现在可以在任意局域网机器中访问该私服
9. 浏览器访问地址:
10. 端口为8081,需要修改端口请进入conf-nexus.properties中修改配置端口
11.
统一账号 admin admin123
下面进行仓库的配置
12. 如图所示
Nexus 的仓库分为这么几类:
hosted 类型的仓库,内部项目的发布仓库
releases 内部的模块中release模块的发布仓库
snapshots 发布内部的SNAPSHOT模块的仓库
3rd party 第三方依赖的仓库,这个数据通常是由内部人员自行下载之后发布上去
proxy 类型的仓库,从远程中央仓库中寻找数据的仓库
group 类型的仓库,组仓库用来方便我们开发人员进行设置的仓库
以下是现在已知的代理仓库地址:
共有的仓库
http://repo1.maven.org/maven2/
http://repository.jboss.com/maven2/
http://repository.sonatype.org/content/groups/public/
http://mirrors.ibiblio.org/pub/mirrors/maven2/org/acegisecurity/
私有的仓库
http://repository.codehaus.org/
http://snapshots.repository.codehaus.org/
http://people.apache.org/repo/m2-snapshot-repository
http://people.apache.org/repo/m2-incubating-repository/
添加代理仓库
比如我们添加一个jboss代理仓库,
现在将jboss代理仓库加入仓库组中
为了使用nexus的索引 查找功能,将所有的代理仓库的远程索引打开
现在为代理仓库下载索引
等待一段时间后,查看browse Index查看索引是否加载。
如图所示,索引就已经加载完毕
Maven使用教程
私服搭建成功,配置maven'使用权,以后下载构件、部署构件,都通过私服来管理。
在maven项目中的pom中添加如下代码
<repositories>
<repository>
<id>nexus</id>
<name>nexus</name>
<url>http://xxxxxx:8081/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
以及使用私服插件代码
<pluginRepositories>
<pluginRepository>
<id>nexus</id>
<name>nexus</name>
<url>http://xxxxxxx:8081/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
多个项目全局性设置settings.xml
友情提醒:请在使用nexus之前,将之前的repository文件夹(本地仓库)备份到其他磁盘下,或者你可以大胆一点,直接干掉.....,然后在你想存放本地仓库的磁盘下创建repository文件夹,将maven中的settings.xml(全局性配置那一个,在文档下面)拷贝到该目录下,然后修改。修改完成之后启动eclipse,在windows--》Preferences--》maven--》User settings,在右边的User settings 选择Browse,找到你创建的repository中的settings.xml,然后OK。
找到maven中的settings.xml,为所有仓库配置一个镜像仓库,镜像仓库的地址即私服的地址(这儿我们使用私服公共仓库组 Public Repositories 的地址):
*表示让所有在此仓库组下的仓库使用此镜像
Url的地址请使用仓库组的地址,即上图的地址
**启用上述配置
如何将项目打包发布到私服中供其它人使用
项目发布到私服,maven项目使用命令:mvn clean deploy;需要在pom文件中配置一下代码;
<distributionManagement>
<repository>
<id>release</id> <!-- 当前项目中定义Release的项目发布地址 -- >
<name>Release</name>
<url>http://xxxxxxx:8081/nexus/content/repositories/releases/</url>
</repository>
<snapshotRepository>
<id>snapshots</id> <!-- 当前项目中定义SNAPSHOTS的项目发布地址 -- >
<name>SNAPSHOTS</name>
<url>http://xxxxxx:8081/nexus/content/repositories/snapshots/</url>
</snapshotRepository>
</distributionManagement>
项目的发布权限配置
在settings.xml中配置权限,其中id要与pom文件中的id一致 (上图中的id)
<server>
<id>release</id>
<username>admin</username>
<password>admin123</password>
</server>
<server>
<id>snapshots</id>
<username>admin</username>
<password>admin123</password>
</server>
项目发布到私服,maven项目使用命令:mvn clean deploy
当项目发布后就可以去私服中对应的仓库查看。