Maven私服倉庫管理: Nexus 3.0

Nexus是一個強大的倉庫管理器,極大地簡化了內部倉庫的維護和外部倉庫的訪問。html

2016年4月6日Nexus 3.0版本發佈,相較2.x版本有了很大的改變:java

  1. 對低層代碼進行了大規模重構,提高性能,增長可擴展性以及改善用戶體驗。node

  2. 升級界面,極大的簡化了用戶界面的操做和管理。nginx

  3. 提供新的安裝包,讓部署更加簡單。npm

  4. 增長對Docker, NeGet, npm, Bower的支持。ubuntu

  5. 提供新的管理接口,以及加強對自動任務的管理。vim

安裝

環境要求

  • Windows / Linux / Mac

  • Java JDK 8+

  • Apache Maven 3.0+

安裝步驟

示例基於Ubuntu 16.04 LTS環境,安裝前檢查JDK:

nexus@ubuntu:~$ java -version
java version "1.8.0_91"
Java(TM) SE Runtime Environment (build 1.8.0_91-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.91-b14, mixed mode)

1. 建立管理用戶

該步驟不是必須的,但爲了更好的管理和維護,建議建立一個管理用戶。

# 使用root權限建立一個用戶
root@ubuntu:~# adduser nexus

## 給nexus用戶添加sudo權限

# 1. 給root寫的權限
root@ubuntu:~# chmod u+w /etc/sudoers

# 2. 編輯/etc/sudoers,在root下添加nexus用戶權限
nexus   ALL=(ALL) ALL

# 3. 保存後撤回寫的權限
root@ubuntu:~# chmod u-w /etc/sudoers

2. 下載安裝包

root@ubuntu:~# su nexus
nexus@ubuntu:~$ wget https://sonatype-download.global.ssl.fastly.net/nexus/3/nexus-3.0.1-01-unix.tar.gz
nexus@ubuntu:~$ ls
nexus-3.0.1-01-unix.tar.gz

3. 解壓和安裝

nexus@ubuntu:~$ tar -zxvf nexus-3.0.1-01-unix.tar.gz

# 查看安裝目錄
nexus@ubuntu:~$ ls nexus-3.0.1-01/
bin  data  deploy  etc  lib  LICENSE.txt  NOTICE.txt  public  system

# bin: 啓動腳本和啓動時的配置文件
# data: 數據存儲目錄
# etc: 配置文件
# lib: Apache Karaf的二進制包
# public: 公共資源
# system: 系統依賴的組件和插件

# 指定JDK版本(可選)
nexus@ubuntu:~$ vim nexus-3.0.1-01/bin/nexus
INSTALL4J_JAVA_HOME_OVERRIDE="/usr/lib/jvm/java-8-oracle"

# 修改使用的用戶(不建議使用root用戶)
nexus@ubuntu:~/nexus-3.0.1-01$ vim bin/nexus.rc
run_as_user="nexus"

4. 運行

nexus@ubuntu:~$ ./nexus-3.0.1-01/bin/nexus start
# 可選的命令:{start|stop|run|run-redirect|status|restart|force-reload}

5. 驗證安裝

打開瀏覽器輸入:http://127.0.0.1:8081

圖片描述

配置

如下配置均爲可選,更詳細的配置請查看官方文檔

1. JVM配置(可選)

Nexus 3.0 的默認配置:

nexus@ubuntu:~$ cat nexus-3.0.1-01/bin/nexus.vmoptions

-Xms1200M
-Xmx1200M
-XX:+UnlockDiagnosticVMOptions
-XX:+UnsyncloadClass
-Djava.net.preferIPv4Stack=true
-Dkaraf.home=.
-Dkaraf.base=.
-Dkaraf.etc=etc
-Djava.util.logging.config.file=etc/java.util.logging.properties
-Dkaraf.data=data
-Djava.io.tmpdir=data/tmp
-Dkaraf.startLocalConsole=false

修改JVM配置:

  1. 請根據系統實際狀況配置

  2. 其餘的JVM參數也能夠添加到這裏

-Xms1500M
-Xmx2G

2. 配置端口號(可選,推薦爲默認端口號:8081)

nexus@ubuntu:~$ vim nexus-3.0.1-01/etc/org.sonatype.nexus.cfg
application-port=9081

重啓後訪問:http://localhost:9081

3. 配置數據存儲目錄(可選)

nexus@ubuntu:~$ vim nexus-3.0.1-01/bin/nexus.vmoptions

# 默認存儲目錄
-Dkaraf.data=data
-Djava.io.tmpdir=data/tmp

4. 配置代理(可選)

Apache httpd.

ProxyRequests Off
ProxyPreserveHost On

<VirtualHost: *:80>
  ServerName www.example.com
  ServerAdmin admin@example.com
  ProxyPass /nexus http://localhost:8081/
  ProxyPassReverse / http://localhost:8081/
  ErrorLog logs/nexus/error.log
  CustomLog logs/nexus/access.log common
</VirtualHost>

nginx.

http {
  proxy_send_timeout 120;
  roxy_read_timeout 300;
  proxy_buffering off;
  keepalive_timeout 5 5;
  tcp_nodelay on;

  server {
    listen *:80;
    server_name www.example.com;

    # allow large uploads of files - refer to nginx documentation
    client_max_body_size 1G

    # optimize downloading files larger than 1G - refer to nginx doc before adjusting
    # proxy_max_temp_file_size 2G

    location /nexus {
       proxy_pass http://localhost:8081/nexus;
       proxy_set_header Host $host;
       proxy_set_header X-Real-IP $remote_addr;
       proxy_set_header X-Forwarded-For
       $proxy_add_x_forwarded_for;
    }
  }
}

5. 配置SSL(可選)

Apache httpd. Ensure Apache httpd is loading mod_ssl.

Listen 443
ProxyRequests Off
ProxyPreserveHost On

<VirtualHost *:443>
  SSLEngine on
  SSLCertificateFile "example.pem"
  SSLCertificateKeyFile "example.key"
  ServerName repo.example.com
  ServerAdmin admin@example.com
  ProxyPass / http://localhost:8081/
  ProxyPassReverse / http://localhost:8081/
  RequestHeader set X-Forwarded-Proto "https"
  ErrorLog logs/repo.example.com/nexus/error.log
  CustomLog logs/repo.example.com/nexus/access.log common
</VirtualHost>

nginx. Make sure nginx is compiled using the --with-http_ssl_module option.

http {
  proxy_send_timeout 120;
  proxy_read_timeout 300;
  proxy_buffering off;
  keepalive_timeout 5 5;
  tcp_nodelay on;
  
  server {
    listen *:443;
    server_name repo.example.com;

    # allow large uploads of files - refer to nginx documentation
    client_max_body_size 1G

    # optimize downloading files larger than 1G - refer to nginx doc before adjusting
    #proxy_max_temp_file_size 2G

    ssl on
    ssl_certificate example.pem;
    ssl_certificate_key example.key;

    location / {
      proxy_pass http://localhost:8081/;
      proxy_set_header Host $host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For ←-

      $proxy_add_x_forwarded_for;
      proxy_set_header X-Forwarded-Proto "https";
    }
  }
}

倉庫介紹

1. 代理倉庫(Proxy Repository)

顧名思義是代理第三方倉庫的,如:

  • maven-central

  • nuget.org-proxy

版本策略(Version Policy):

  • Release: 正式版本

  • Snapshot: 快照版本

  • Mixed: 混合模式

佈局策略(Layout Policy):

  • Strict:嚴格

  • Permissive:寬鬆

2. 宿主倉庫(Hosted Repository)

存儲本地上傳的組件和資源的,如:

  • maven-releases

  • maven-snapshots

  • nuget-hosted

部署策略(Deployment Policy):

  • Allow Redeploy:容許從新部署

  • Disable Redeploy:禁止從新部署

  • Read-Only:只讀

3. 倉庫組(Repository Group)

一般包含了多個代理倉庫和宿主倉庫,在項目中只要引入倉庫組就能夠下載到代理倉庫和宿主倉庫中的包,如:

  • maven-public

  • nuget-group

用戶界面的操做和管理相對簡單,請參考官方文檔

與Maven集成

1. Servers 配置認證信息

在Maven settings.xml中添加Nexus認證信息:

<server>
  <id>nexus-releases</id>
  <username>admin</username>
  <password>admin123</password>
</server>

<server>
  <id>nexus-snapshots</id>
  <username>admin</username>
  <password>admin123</password>
</server>
  • nexus-releases: 用於發佈Release版本

  • nexus-snapshots: 用於發佈Snapshot版本

Release版本與Snapshot版本的區分:

Release: 4.3.0
Snapshot: 4.3.0-SNAPSHOT
  • 在項目POM.xml中設置的版本號添加SNAPSHOT標識的都會發布爲SNAPSHOT版本,沒有SNAPSHOT標識的都會發布爲Release版本。

  • SNAPSHOT版本會自動加一個時間做爲標識,如:4.3.0-SNAPSHOT發佈後爲變成4.3.0-SNAPSHOT-20160712.114532-1.jar

2. 配置自動化部署構件

在POM.xml中添加如下代碼:

<distributionManagement>  
  <repository>  
    <id>nexus-releases</id>  
    <name>Nexus Release Repository</name>  
    <url>http://127.0.0.1:8081/repository/maven-releases/</url>  
  </repository>  
  <snapshotRepository>  
    <id>nexus-snapshots</id>  
    <name>Nexus Snapshot Repository</name>  
    <url>http://127.0.0.1:8081/repository/maven-snapshots/</url>  
  </snapshotRepository>  
</distributionManagement>

注意事項:

  • ID名稱必需要與settings.xml中Servers配置的ID名稱保持一致。

  • 項目版本號中有SNAPSHOT標識的,會發布到Nexus Snapshots Repository, 不然發佈到Nexus Release Repository,並根據ID去匹配受權帳號。

3. 部署到Nexus倉庫

mvn deploy

4. 上傳第三方JAR包

Nexus 3.0不支持頁面上傳,可以使用maven命令:

# 如第三方JAR包:aliyun-sdk-oss-2.2.3.jar
mvn deploy:deploy-file 
  -DgroupId=com.aliyun.oss 
  -DartifactId=aliyun-sdk-oss 
  -Dversion=2.2.3 
  -Dpackaging=jar 
  -Dfile=D:\aliyun-sdk-oss-2.2.3.jar 
  -Durl=http://127.0.0.1:8081/repository/maven-3rd/ 
  -DrepositoryId=nexus-releases

注意事項:

  • 建議在上傳第三方JAR包時,建立單獨的第三方JAR包管理倉庫,便於管理有維護。(maven-3rd)

  • -DrepositoryId=nexus-releases 對應的是settings.xml中Servers配置的ID名稱。(受權)

5. 使用代理中央倉庫

<repositories>
    <repository>
        <id>nexus</id>
        <name>Nexus Repository</name>
        <url>http://127.0.0.1:8081/repository/maven-public/</url>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
        <releases>
            <enabled>true</enabled>
        </releases>
    </repository>
</repositories>
<pluginRepositories>
    <pluginRepository>
        <id>nexus</id>
        <name>Nexus Plugin Repository</name>
        <url>http://127.0.0.1:8081/repository/maven-public/</url>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
        <releases>
            <enabled>true</enabled>
        </releases>
    </pluginRepository>
</pluginRepositories>

注意事項:

  • 在開發中若是有嚴格的管理要求,可將SNAPSHOT和RELEASE單獨配置。

  • 有插件依賴時配置插件倉庫,默認會使用Maven中央倉庫。

相關文章
相關標籤/搜索