搭建Maven私服(nexus)-windows版

私服,私有服务器,是公司内部Maven项目经常需要的东东,那这里我们总结一下吧。Nexus是常用的私用Maven服务器,一般是公司内部使用。

Nexus常用功能就是:指定私服的中央地址、将自己的Maven项目指定到私服地址、从私服下载中央库的项目索引、从私服仓库下载依赖组件、将第三方项目jar上传到私服供其他项目组使用。

1.前言

Maven默认提供的中央仓库是在远程网络服务Appache提供的,这对于我们开发时不合理的。如果我们没网了或者什么情况,我们怎么办?也就是说我们队中央仓库的依赖性太大。而Nexus私服则可以解决我们这个问题。
这里写图片描述
这样就相当于在我们本地的局域网搭建了一个类似中央仓库的服务器,我们开始将中央仓库的一些资料下载到私服务器上,然后平时我们的maven项目就是直接访问局域网内的私服即可,既节省了网络带宽也会加速项目搭建的进程,这样对我们开发来说,对公司来说都是非常好的选择。


2.搭建我们自己的私服

1)下载

csdn下载地址https://download.csdn.net/download/m0_37499059/10564295
官网下载地址
这里写图片描述

2)启动服务

这里写图片描述
这里写图片描述

游览器中输入http://127.0.0.1:8081/nexus/,出现如下图所示就代表nexus已经启动成功。
这里写图片描述

8081为默认的端口号,要修改端口号可进入nexus-2.1.2-bundle\nexus-2.1.2\conf\打开nexus.properties文件,修改application-port属性值就可以了。默认的用户名和密码:admin/admin123,登录后看到如下图所示:
这里写图片描述


3.Nexus仓库

nexus的仓库类型分为以下四种:

group: 仓库组
hosted:宿主
proxy:代理
virtual:虚拟

这里写图片描述

PublicRepositories: 仓库组
3rd party: 无法从公共仓库获得的第三方发布版本的构件仓库
Apache Snapshots: 用了代理ApacheMaven仓库快照版本的构件仓库
Central: 用来代理maven中央仓库中发布版本构件的仓库
Central M1 shadow: 用于提供中央仓库中M1格式的发布版本的构件镜像仓库
Codehaus Snapshots: 用来代理CodehausMaven 仓库的快照版本构件的仓库
Releases: 用来部署管理内部的发布版本构件的宿主类型仓库
Snapshots:用来部署管理内部的快照版本构件的宿主类型仓库


4.配置Nexus

1)开启远程索引

新搭建的neuxs环境只是一个空的仓库,需要手动和远程中心库进行同步,nexus默认是关闭远程索引下载,最重要的一件事情就是开启远程索引下载。登陆nexus系统,默认用户名密码为admin/admin123。
点击左边Administration菜单下面的Repositories,找到右边仓库列表中的三个仓库Apache Snapshots,Codehaus Snapshots和Maven Central,然后再没有仓库的configuration下把Download Remote Indexes修改为true。如下图

这里写图片描述

然后在Apache Snapshots,Codehaus Snapshots和Maven Central这三个仓库上分别右键,选择Repari Index,这样Nexus就会去下载远程的索引文件。

这里写图片描述
这样设置以后, Nexus会自动从远程中央仓库下载索引文件, 为了检验索引文件自动下载是否生效,可以却换到Browse Index

这里写图片描述

2)建立宿主仓库

新建公司的内部仓库,步骤为Repositories –> Add –>Hosted Repository,在页面的下半部分输入框中填入Repository ID和Repository Name即可,比如分别填入com.cxx和demo,另外把Deployment Policy设置为Allow Redeploy,点击save就创建完成了。这里我点击添加宿主类型的仓库,在仓库列表的下方会出现新增仓库的配置,如下所示:
这里写图片描述

3)创建Nexus仓库组

Nexus 中仓库组的概念是Maven没有的,在Maven看来,不管你是hosted也好,proxy也好,或者group也好,对我都是一样的,我只管根据 groupId,artifactId,version等信息向你要构件。为了方便Maven的配置,Nexus能够将多个仓库,hosted或者 proxy合并成一个group,这样,Maven只需要依赖于一个group,便能使用所有该group包含的仓库的内容。
neuxs- 1.9.2.3中默认自带了一个名为“Public Repositories”组,点击该组可以对他保护的仓库进行调整,把刚才建立的公司内部仓库zfy repostiory加入其中,这样就不需要再在maven中明确指定内部仓库 的地址了。同时创建一个Group ID为public-snapshots、Group Name为Public SnapshotsRepositories的组,把Apache Snapshots、Codehaus Snapshots、Snapshots和demo repostiory加入其中。
这里写图片描述

4) 创建Nexus代理仓库

点击菜单栏上的Add按钮后选择Proxy Repository,看到如下所示配置界面:
这里写图片描述

5) settings.xml的配置
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" 
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">

  <pluginGroups></pluginGroups>
  <proxies></proxies>

  <servers>
      <!--配置权限-->
      <server>
      <id>nexus-releases</id>
      <username>admin</username>
      <password>admin123</password>
    </server>
    <server>
      <id>nexus-snapshots</id>
      <username>admin</username>
      <password>admin123</password>
    </server>
  </servers>

  <mirrors> 
  //配置镜像,让maven只使用私服获取
    <mirror> 
      <id>nexus-releases</id> 
      <mirrorOf>*</mirrorOf> 
      <url>http://localhost:8081/nexus/content/groups/public</url> 
    </mirror>
    <mirror> 
      <id>nexus-snapshots</id> 
      <mirrorOf>*</mirrorOf> 
      <url>http://localhost:8081/nexus/content/groups/public-snapshots</url> 
    </mirror> 
  </mirrors> 

  <profiles>
   <profile>
      <id>nexus</id>
      <repositories>
       <!--指定仓库-->
        <repository>
          <id>nexus-releases</id>
          <url>http://nexus-releases</url>
          <releases><enabled>true</enabled></releases>
          <snapshots><enabled>true</enabled></snapshots>
        </repository>
        <repository>
          <id>nexus-snapshots</id>
          <url>http://nexus-snapshots</url>
          <releases><enabled>true</enabled></releases>
          <snapshots><enabled>true</enabled></snapshots>
        </repository>
      </repositories>
     //配置仓库车插件,一旦配置了镜像,则 这个配置可忽略
      <pluginRepositories>
         <pluginRepository>
                <id>nexus-releases</id>
                 <url>http://nexus-releases</url>
                 <releases><enabled>true</enabled></releases>
                 <snapshots><enabled>true</enabled></snapshots>
               </pluginRepository>
               <pluginRepository>
                 <id>nexus-snapshots</id>
                  <url>http://nexus-snapshots</url>
                <releases><enabled>true</enabled></releases>
                 <snapshots><enabled>true</enabled></snapshots>
             </pluginRepository>
         </pluginRepositories>
    </profile>
  </profiles>

  <activeProfiles>
      <activeProfile>nexus</activeProfile>
  </activeProfiles>

</settings>

到这就搭建成功了!


5.创建一个项目测试一下

我这里为了快,创建一个springboot core项目,然后deploy我们的jar包到自己刚搭建的私服里面去!(一些细节我们略过)

1)在pom.xml加上下面内容

<distributionManagement>
        <!-- 两个ID必须与 setting.xml中的<server><id>nexus-releases</id></server>保持一致-->
        <repository>
            <id>nexus-releases</id>
            <name>Nexus Release Repository</name>
            <url>http://localhost:8081/nexus/content/repositories/releases</url>
        </repository>
        <snapshotRepository>
            <id>nexus-snapshots</id>
            <name>Nexus Snapshot Repository</name>
            <url>http://localhost:8081/nexus/content/repositories/snapshots</url>
        </snapshotRepository>
</distributionManagement>

2) 打包部署到私服上
这里写图片描述

3) 见证奇迹的时刻
这里写图片描述

http://127.0.0.1:8081/nexus/content/groups/public/
这里写图片描述
这里写图片描述

猜你喜欢

转载自blog.csdn.net/m0_37499059/article/details/81208376
今日推荐