Maven 搭建私服(nexus)

下载安装nexus

  • 教程
下载地址:https://www.sonatype.com/download-oss-sonatype
安装地址:https://help.sonatype.com/display/NXRM3/Installation
  • 启动nexus
命令:sh bin/nexus(安装目录的bin目录) start | restart | stop
可以将bin设置到path环境。
  • 访问
    这里写图片描述
默认用户名/密码:admin/admin123

这里写图片描述

这里有几种类型的仓库:
1, group: 是群组仓库,就是多个仓库的一个组合包;(可以是hosted,proxy仓库的组合)
2, proxy: 代理仓库,例如:central 中央仓库,当本地仓库访问时,会将请求转发到中央仓库;
3, hosted: 私有(本地仓库),就是自己提供的仓库,例如:第三方包的仓库,自行管理;

可以自行添加、修改、删除仓库(这里不做介绍,so easy,按照界面操作一下就可以搞定)

配置私服环境

前一篇,讲述了maven仓库的基本内容,私服的概念这里就不累赘了。我们可以通过两种方式来达到私服提供maven服务的效果。
1、设置私有仓库;
2、设置镜像仓库;

设置私服仓库

  • 项目级别(pom.xml 配置)
<repositories>
        <repository>
            <id>nexus-private-repo</id>
            <name>learn nexus</name>
            <!-- nexus group 仓库的地址 -->
            <url>http://localhost:8081/nexus/content/groups/public/</url>
        </repository>
</repositories>
  • 用户或者全局配置(settings.xml)
<profiles>
    <profile>
        <id>nexus-private-repo</id>
        <repositories>
            <repository>
                <id>nexus-private-repo</id>
                <name>learn nexus</name>
                <!-- nexus group 仓库的地址 -->
                <url>http://localhost:8081/nexus/content/groups/public/</url>
            </repository>
        </repositories>
    </profile>
</profiles>
<activeProfiles>
    <activeProfile>nexus-private-repo</activeProfile>
</activeProfiles>

使用镜像模式

<mirrors>
    <mirror>
      <id>mirrorId</id>
      <!-- 匹配所有仓库 -->      
      <mirrorOf>*</mirrorOf>
      <name>Mirror implements private store</name>
      <url>http://localhost:8081/nexus/content/groups/public/</url>
    </mirror>
</mirrors>

案例测试

下载

  • 项目级别pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <groupId>study.wzp.maven</groupId>
    <version>1.0-SNAPSHOT</version>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>maven-nexus</artifactId>

    <repositories>
        <repository>
            <id>nexus-private-repo</id>
            <name>learn nexus</name>
            <!-- nexus group 仓库的地址 -->
            <url>http://localhost:8081/nexus/content/groups/public/</url>
            <layout>default</layout>
        </repository>
    </repositories>

    <dependencies>
        <!-- 本地不存在,会从私服nexus下载,nexus从central下载 -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.9</version>
        </dependency>
    </dependencies>

</project>

这里写图片描述

  • 用户配置仓库
<?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">

<profiles>
    <profile>
      <id>nexus-private-repo-profile</id>
      <repositories>
        <repository>
          <id>nexus-private-repo</id>
          <name>nexus private repository test</name>
          <url>http://localhost:8081/nexus/content/groups/public/</url>
          <layout>default</layout>
          <snapshotPolicy>always</snapshotPolicy>
        </repository>
      </repositories>
    </profile>

  </profiles>

  <activeProfiles>
    <activeProfile>nexus-private-repo-profile</activeProfile>
  </activeProfiles>
</settings>

这里写图片描述

  • 私服配置
<?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">

    <mirrors>
        <mirror>
          <id>nexus-private-repo</id>
          <name>nexus private repository test</name>
          <url>http://localhost:8081/nexus/content/groups/public/</url>
          <!-- 拦截所有仓库的访问 -->
          <mirrorOf>*</mirrorOf>
        </mirror>

    </mirrors>

</settings>

这里写图片描述

发布上传

所谓的发布上传,目的就是将自己开发的项目发布到nexus,然后提供给其他项目使用;
  • pom.xml
增加发布时的仓库配置:
<distributionManagement>
        <repository>
            <id>releases</id>
            <name>Public Repositories</name>
            <url>http://localhost:8081/nexus/content/repositories/releases/</url>
        </repository>
</distributionManagement>
  • settings.xml
配置服务器的权限:
<servers>
        <server>
          <id>releases</id>
          <username>admin</username>
          <password>admin123</password>
        </server>
</servers>
  • mvn deploy
    这里写图片描述
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值