Nexus 私服搭建&Maven使用教程

Nexus搭建教程

By:wumeng

前言

私服是架设在局域网的一种特殊的远程仓库目的是代理远程仓库及部署第三方构件有了私服之后 Maven 需要下载构件时直接请求私服私服上存在则下载到本地仓库否则私服请求外部的远程仓库将构件下载到私服再提供给本地仓库下载

 

公司一般都有个自己的私服来管理各种jar原因大概有这么3分别是

    1、有的公司不能访问外网只能通过私服来管理jar包和插件

    2、公司网速比较慢通过公司的私服来获取jar包比较快

    3、内部的的一些jar包放在私服上比较方便的大家使用

 

Nexus 私服搭建&Maven使用教程

 

 

Nexus 私服搭建&Maven使用教程

 

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 私服搭建&Maven使用教程

最新版本是:nexus-2.14.3-02bundle,点击下载

Nexus 私服搭建&Maven使用教程

 

3.  将压缩文件解压缩到任意文件或者磁盘目录解压缩获得两个文件如图

Nexus 私服搭建&Maven使用教程

 

4. 在任意磁盘或文件夹下创建nexus文件夹将两个文件拷贝到这个文件夹中

5. 然后进入nexus-2.14.3-02\bin\jsw下选择对应的系统如图

Nexus 私服搭建&Maven使用教程

6.  我的系统是windows 64位的windows包含xp,win7,win8,win10),进入该目录

Nexus 私服搭建&Maven使用教程

如需要安装位windows服务需要在命令cmd中运行 nexus install

 

7.  启动nexus或者服务

Nexus 私服搭建&Maven使用教程

 

8.  nexus私服就已经搭建并部署完成现在可以在任意局域网机器中访问该私服

9.  浏览器访问地址

10.  端口为8081,需要修改端口请进入conf-nexus.properties中修改配置端口

Nexus 私服搭建&Maven使用教程

 

11.  

 

Nexus 私服搭建&Maven使用教程

统一账号   admin  admin123

Nexus 私服搭建&Maven使用教程

下面进行仓库的配置

Nexus 私服搭建&Maven使用教程

 

12.  如图所示

Nexus 私服搭建&Maven使用教程

Nexus 的仓库分为这么几类

            hosted   类型的仓库内部项目的发布仓库 

      releases 内部的模块中release模块的发布仓库

      snapshots 发布内部的SNAPSHOT模块的仓库  

      3rd party 第三方依赖的仓库这个数据通常是由内部人员自行下载之后发布上去 

      proxy   类型的仓库从远程中央仓库中寻找数据的仓库

      group   类型的仓库组仓库用来方便我们开发人员进行设置的仓库

 

Nexus 私服搭建&Maven使用教程

 

以下是现在已知的代理仓库地址

共有的仓库
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代理仓库

 

Nexus 私服搭建&Maven使用教程

 

Nexus 私服搭建&Maven使用教程

Nexus 私服搭建&Maven使用教程

现在将jboss代理仓库加入仓库组中

Nexus 私服搭建&Maven使用教程

 

为了使用nexus的索引 查找功能将所有的代理仓库的远程索引打开

Nexus 私服搭建&Maven使用教程

Nexus 私服搭建&Maven使用教程

 

 

现在为代理仓库下载索引

Nexus 私服搭建&Maven使用教程

 

等待一段时间后查看browse Index查看索引是否加载

 

如图所示索引就已经加载完毕

Nexus 私服搭建&Maven使用教程

 

 

 

                                         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 的地址):

Nexus 私服搭建&Maven使用教程

 

 

*表示让所有在此仓库组下的仓库使用此镜像

Url的地址请使用仓库组的地址即上图的地址

Nexus 私服搭建&Maven使用教程

Nexus 私服搭建&Maven使用教程

 

**启用上述配置

Nexus 私服搭建&Maven使用教程

 

 

 

如何将项目打包发布到私服中供其它人使用

 项目发布到私服,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

当项目发布后就可以去私服中对应的仓库查看