Linux下Nexus Repository3安装和maven,npm配置


Nexus Repository下载

根据操作系统选择指定版本,本文针对Linux安装,其他的安装过程可能有所差异。

https://help.sonatype.com/repomanager3/download/download-archives---repository-manager-3

安装

建议先创建一个nexus用户,将文件拷贝到/home/nexus


# 按具体下载文件名
tar -zxvf nexus-3.14.0-04-unix.tar.gz

# 按具体解压目录
cd nexus-3.14.0-04/bin


## 启动
./nexus start
# ./nexus stop #停止
# ./nexus status # 查看状态

配置修改

NexusRepository 默认占用8081端口,如果与其他服务有冲突,需要在sonatype-work/etc目录下创建一个nexus.properties,

注意不是nexus-3.14.0-04下的etc
具体配置参考nexus-default.properties

## DO NOT EDIT - CUSTOMIZATIONS BELONG IN $data-dir/etc/nexus.properties
##
# Jetty section
application-port=8081
application-host=0.0.0.0
nexus-args=${jetty.etc}/jetty.xml,${jetty.etc}/jetty-http.xml,${jetty.etc}/jetty-requestlog.xml
nexus-context-path=/

# Nexus section
nexus-edition=nexus-pro-edition
nexus-features=\
 nexus-pro-feature

Nexus不建议在root用户下运行,需要创建用户nexus用户,修改bin/nexus.rc文件

run_as_user="nexus"

开机自启动

开启自启动有init.d和systemd两种方式。

init.d

1.创建一个nexus用户

2.拷贝nexus的软链接到init.d目录
注意:最好在root做自启动配置,省的没权限

# 原文件路径按实际
ln -s /home/nexus/nexus-3.14.0-04/bin/nexus /etc/init.d/nexus

3.1 chkconfig 写法(和3.2二选一)

cd /etc/init.d
chkconfig --add nexus
chkconfig --levels 345 nexus on

3.2 update-rc.d写法(和3.1 二选一)

cd /etc/init.d
update-rc.d nexus defaults
service nexus start

4.切换到nexus用户窗口,启动服务

su nexus

service nexus start

systemd

前提是服务器有安装systemd工具,一般cenos7默认安装。

创建一个nexus用户

在/etc/systemd/system/ 目录添加如下文件

nexus.service

[Unit]
Description=nexus service
After=network.target
  
[Service]
Type=forking
LimitNOFILE=65536
ExecStart=/opt/nexus/bin/nexus start
ExecStop=/opt/nexus/bin/nexus stop
User=nexus
Restart=on-abort
  
[Install]
WantedBy=multi-user.target
Activate the service with the following commands:

启动服务命令

systemctl daemon-reload
systemctl enable nexus.service
systemctl start nexus.service

查看服务运行日志

tail -f /opt/sonatype-work/nexus3/log/nexus.log

Maven仓库配置

admin账户登录,访问Repository >Repositories,仓库默认配置maven本地库和代理库

默认代理maven库地址是https://repo1.maven.org/maven2/,可以修改

npm仓库配置

npm仓库不是内置的,需要手动配置,需要配置release,snapshots,proxy三个资源库,同时通过public合并起来,对外提供服务

具体配置见下图:




Maven私服使用

maven私服使用有两种方式,你可以任选一种

1.在maven的setting.xml配置mirror

<mirrors>
     <mirror>
        <id>alimaven</id>
        <name>aliyun maven</name>
        <url>http://maven.aliyun.com/mvn/view</url>
        <mirrorOf>central</mirrorOf>
    </mirror>
    <mirror>
        <id>jboss-public-repository-group</id>
        <mirrorOf>central</mirrorOf>
        <name>JBoss Public Repository Group</name>
        <url>http://repository.jboss.org/nexus/content/groups/public</url>
    </mirror>
    <mirror>
        <id>ibiblio</id>
        <mirrorOf>central</mirrorOf>
        <name>Human Readable Name for this Mirror.</name>
        <url>http://mirrors.ibiblio.org/pub/mirrors/maven2/</url>
    </mirror>
    <mirror>
        <id>central</id>
        <name>Maven Repository Switchboard</name>
        <url>http://repo1.maven.org/maven2/</url>
        <mirrorOf>central</mirrorOf>
    </mirror>
    <mirror>
        <id>repo2</id>
        <mirrorOf>central</mirrorOf>
        <name>Human Readable Name for this Mirror.</name>
        <url>http://repo2.maven.org/maven2/</url>
    </mirror>
</mirrors>

2.在具体项目的pom.xml文件配置资源库

    <repositories>
        <repository>
            <id>nexus</id>
            <name>Nexus Repository</name>
            <url>http://192.168.3.28:8084/repository/maven-public/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <!--snapshots默认是关闭的,需要开启  -->
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
    </repositories>  

npm私服使用

如上npm,npm的私服地址为http://192.168.3.28:8084/repository/npm-public/

建议安装nrm工具,这样可以快速切换npm的资源库


# 安装nrm
npm install -g nrm

# 添加资源库
nrm add test http://192.168.3.28:8084/repository/npm-public/

# 切换资源库
nrm use test

maven私服发布

在pom.xml配置maven私服仓库地址,有snapshot和release两个仓库

<distributionManagement>
        <repository>
            <id>nexus-maven-repository-releases</id>
            <url>http://192.168.3.28:8084/repository/maven-releases/</url>
        </repository>
        <snapshotRepository>
            <id>nexus-maven-repository-snapshots</id>
            <url>http://192.168.3.28:8084/repository/maven-snapshots/</url>
        </snapshotRepository>
    </distributionManagement>

npm私服发布

自己开发的组件通过如下命令发布到npm私服

#  用户登录
npm login 

# 编译打包
npm init

# 发布
npm publish

这时候你会发现发布不上去,报401,即使你的用户名和密码都是对的

你需要将npm的认证组件在nexus中开启

maven和npm发布注意事项

npm和maven发布都不能用group类型的资源库,他们是没有发布权限,只有拉取权限,你需要找到对应group的hostd资源库做发布。

linuxboy的RSS地址:https://www.linuxboy.net/rssFeed.aspx

本文永久更新链接地址:https://www.linuxboy.net/Linux/2018-12/155990.htm

相关内容