Nexus Repository3安裝和maven,npm配置(Linux)


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資源庫做發布


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM