1. 程式人生 > >Linux下Nexus Repository3安裝和maven,npm配置

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資源庫做釋出。