nexus(Maven仓库私服)安装与配置

介绍

Nexus 是Maven仓库管理器,如果你使用Maven,你可以从Maven中央仓库 下载所需要的构件(artifact),但这通常不是一个好的做法,你应该在本地架设一个Maven仓库服务器,在代理远程仓库的同时维护本地仓库,以节省带宽和时间,Nexus就可以满足这样的需要。此外,他还提供了强大的仓库管理功能,构件搜索功能,它基于REST,友好的UI是一个extjs的REST客户端,它占用较少的内存,基于简单文件系统而非数据库。这些优点使其日趋成为最流行的Maven仓库管理器。

下载

开源版本–免费,专业版本–收费。下面为开源版本2.14.3-02
https://sonatype-download.global.ssl.fastly.net/repository/downloads-prod-group/oss/nexus-2.14.3-02-bundle.tar.gz

安装

要注意nexus启动需要2核2G以上的配置,否则启动不了。

解压

# 进入安装目录
cd /home/nexus
# 下载安装包
wget https://sonatype-download.global.ssl.fastly.net/repository/downloads-prod-group/oss/nexus-2.14.3-02-bundle.tar.gz
# 解压到当前文件夹
tar -zxvf nexus-2.14.3-02-bundle.tar.gz

解压之后可以看到如下两个目录:
在这里插入图片描述
第一个文件夹nexus-2.14.3-02是核心文件,第二个文件夹sonatype-work是用来存储下载下来的jar的。

修改配置

核心配置文件是/home/nexus/nexus-2.14.3-02/conf/nexus.properties
在这里插入图片描述
修改完成之后即可启动。

启动

cd /home/nexus/nexus-2.14.3-02
./bin/nexus start

这时候有可能会报错,
在这里插入图片描述
那么这时候修改运行文件nexusvim nexus,添加一行代码RUN_AS_USER=root。然后重新启动即可。

打开web页面

打开浏览器,访问地址:http://ip:port/nexus
在这里插入图片描述
点击右上角登录按钮,默认用户名:admin,默认密码:admin123

使用

功能说明

用途

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

仓库

  • hosted 类型的仓库,内部项目的发布仓库
  • releases 内部的模块中release模块的发布仓库
  • snapshots 发布内部的SNAPSHOT模块的仓库
  • 3rd party 第三方依赖的仓库,这个数据通常是由内部人员自行下载之后发布上去
  • proxy 类型的仓库,从远程中央仓库中寻找数据的仓库
  • group 类型的仓库,组仓库用来方便我们开发人员进行设置的仓库

配置

nexus配置大部分使用默认配置即可,主要是配置一个项目索引
选择Central仓库,设置Download Remote Indexes:True

maven配置nexus

项目使用nexus私服

  1. 使用nexus私服的jar包,在项目的pom.xml文件中指定私服仓库
<repositories>
    <repository>
        <id>nexus</id>
        <name>nexus</name>
        <url>http://10.1.24.226:8081/nexus/content/groups/public/</url>
        <releases>
            <enabled>true</enabled>
        </releases>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>
</repositories>
  1. 使用nexus私服的插件,在项目的pom.xml文件中指定插件仓库
<pluginRepositories>
    <pluginRepository>
        <id>nexus</id>
        <name>nexus</name>
        <url>http://10.1.24.226:8081/nexus/content/groups/public/</url>
        <releases>
            <enabled>true</enabled>
        </releases>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </pluginRepository>
</pluginRepositories>

本机所有maven项目都使用nexus私服的组件

可以在maven的设置文件settings.xml中添加属性,并激活

<profiles>
    <profile>
        <id>nexusProfile</id>
        <repositories>
            <repository>
                <id>nexus</id>
                <name>nexus</name>
                <url>http://10.1.24.226:8081/nexus/content/groups/public/</url>
                <releases>
                    <enabled>true</enabled>
                </releases>
                <snapshots>
                    <enabled>true</enabled>
                </snapshots>
            </repository>
        </repositories>
    </profile>
</profiles>
<!-- 激活 -->
<activeProfiles>
    <activeProfile>nexusProfile</activeProfile>
</activeProfiles>

项目发布到私服

maven项目使用命令:mvn clean deploy;需要在pom文件中配置一下代码;

<distributionManagement>
    <repository>
        <id>user-release</id>
        <name>User Project Release</name>
        <url>http://10.1.24.226:8081/nexus/content/repositories/releases/</url>
    </repository>

    <snapshotRepository>
        <id>user-snapshots</id>
        <name>User Project SNAPSHOTS</name>
        <url>http://10.1.24.226:8081/nexus/content/repositories/snapshots/</url>
    </snapshotRepository>
</distributionManagement>

注意还需要配置mvn发布的权限,否则会报401错误,在settings.xml中配置权限,其中id要与pom文件中的id一致

<server>
    <id>user-release</id>
    <username>admin</username>
    <password>admin123</password>
</server>
<server>
    <id>user-snapshots</id>
    <username>admin</username>
    <password>admin123</password>
</server>

完成之后即可在仓库中看到你上传的jar包
在这里插入图片描述
这时候在别的项目中就可以直接引用了

<dependency>
  <groupId>person.bo</groupId>
  <artifactId>just-test</artifactId>
  <version>1.0-SNAPSHOT</version>
</dependency>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章