Maven and Nexus3 原

Maven and Nexus3

Maven是什麼?

關於maven的原理,詳見:https://my.oschina.net/adailinux/blog/2247017

安裝及配置

安裝Maven

在centos7系統安裝maven很簡單,直接使用yum安裝就可以,不過在安裝maven之前首先要配置系統的 JDK (java)環境。春雨使用ansible進行部署,對應的role是 maven

playbook:

$ cat maven.yml
---
- hosts: ucloud
  gather_facts: False
  roles:
    - role: maven

使用方法:

$ ansible-playbook maven.yml 

安裝Nexus

官方建議 服務器硬件配置:

  • CPU:≥4核
  • memory:≥4G (do not set max heap size larger than 4GB
  • fd:65536

安裝依賴

  • Java 8
  • maven
  • npm

Java和maven在上面的過程已安裝,接下來只需要安裝npm,步驟如下:

# 安裝之前先創建對應的目錄
$ mkdir /home/node
$ cd /home/node

# 使用nodejs管理npm
$ wget https://nodejs.org/dist/v8.12.0/node-v8.12.0-linux-x64.tar.xz

# 解壓
$ tar Jxvf node-v8.12.0-linux-x64.tar.xz
$ mv node-v8.12.0-linux-x64 nodejs

# 加入系統環境
$ ln -s /home/node/nodejs/bin/node /usr/bin/node
$ ln -s /home/node/nodejs/bin/npm /usr/bin/npm

# 升級npm
$ npm install npm@latest -g

安裝Nexus3

# 創建安裝目錄
$ mkdir /home/sonatype
$ cd /home/sonatype
$ wget https://download.sonatype.com/nexus/3/latest-unix.tar.gz
$ tar zxvf latest-unix.tar.gz 

$ [root@host1 sonatype]# ls
nexus-3.13.0-01  sonatype-work

## nexus-3.13.0-01 應用文件目錄
## sonatype-work 數據文件目錄

# 進入應用文件目錄
$ cd /home/sonatype/nexus-3.13.0-01

# 運行sonatype
$ ./bin/nexus run
## 輸出 Started Sonatype Nexus 表示啓動成功!
## 後續會加入systemd管理

啓動成功後在瀏覽器訪問(localhost:8081):http://192.168.228.128:8081/ ,進入web界面

20181008153901220913733.png

使用管理員用戶登錄,賬號:admin 密碼:admin123。如果使用sonatype管理用戶和密碼,可以通過設置——change password來更改密碼,如果集成了ldap用戶,則無法通過此方法更改密碼。

  • 系統優化: 20181009153901441059372.png

    sonatype需要配置系統文件描述符數量爲 65536,配置方法如下:

    # 查看當前系統可打開文件描述符數量
    $ ulimit -n
    
    # 修改文件描述符數量
    ## 臨時修改
    $ ulimit -n 65535
    ## 永久修改
    $ vim /etc/security/limits.conf
    nexus - nofile 65536
    

    如果 加入了systemd管理 nexus,上述方法是不生效的,配置方法如下:

    $ vim /usr/lib/systemd/system/nexus.service
    [Unit]
    Description=nexus service
    After=network.target
    
    [Service]
    Type=forking
    LimitNOFILE=65536
    ExecStart=/home/sonatype/nexus-3.13.0-01/bin/nexus start
    ExecStop=/home/sonatype/nexus-3.13.0-01/bin/nexus stop
    User=nexus
    Restart=on-abort
    
    [Install]
    WantedBy=multi-user.target
    
    $ systemctl daemon-reload
    $ useradd nexus
    $ chown -R nexus:nexus /home/sonatype
    $ systemctl start nexus
    

集成ldap

20181008153901385617533.png

配置信息

20181008153901411252501.png

代理maven和npm組件

配置maven-proxy
  • 更改maven配置 編輯maven的settings.xml文件,更改mirror、profile、activeProfiles模塊的內容如下:

    $ vim /etc/maven/settings.xml
    <settings>
      <mirrors>
    	<mirror>
      	<!--This sends everything else to /public -->
      	<id>nexus</id>
      	<mirrorOf>*</mirrorOf>
      	<url>http://localhost:8081/repository/maven-proxy/</url>
    	</mirror>
      </mirrors>
      <profiles>
    	<profile>
      	<id>nexus</id>
      	<!--Enable snapshots for the built in central repo to direct -->
      	<!--all requests to nexus via the mirror -->
      	<repositories>
        	<repository>
          	<id>central</id>
          	<url>http://central</url>
          	<releases><enabled>true</enabled></releases>
          	<snapshots><enabled>true</enabled></snapshots>
        	</repository>
      	</repositories>
     	<pluginRepositories>
        	<pluginRepository>
          	<id>central</id>
          	<url>http://central</url>
          	<releases><enabled>true</enabled></releases>
          	<snapshots><enabled>true</enabled></snapshots>
        	</pluginRepository>
      	</pluginRepositories>
    	</profile>
      </profiles>
      <activeProfiles>
    	<!--make the profile active all the time -->
    	<activeProfile>nexus</activeProfile>
      </activeProfiles>
    </settings>
    
  • 配置Nexus,到web端配置:

    • 配置repository: 20181009153905348754188.png

    • 選擇maven2(proxy):

      20181009153905361930723.png

    • 點擊“Create repository

  • 創建 POM file (pom.xml)內容如下:

    $ vim /etc/maven/pom.xml
    <project>
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.example</groupId>
      <artifactId>nexus-proxy</artifactId>
      <version>1.0-SNAPSHOT</version>
      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.10</version>
        </dependency>
      </dependencies>
    </project>
    
  • 構建maven-proxy:

    # 構建之前需要先配置一下java環境(mvn命令默認使用/usr/java/latest/bin/java)
    $ ln -s /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.181-3.b13.el7_5.x86_64/jre/bin/java /usr/java/latest/bin/java 
    
    # 開始構建
    $ mvn package
    [INFO] Building jar: /etc/maven/target/nexus-proxy-1.0-SNAPSHOT.jar
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 1:27.411s
    [INFO] Finished at: Tue Oct 09 13:21:33 CST 2018
    [INFO] Final Memory: 8M/20M
    [INFO] ------------------------------------------------------------------------
    # 構建完成!
    

    構建過程可以在web界面查看,點擊 20181009153905419826775.png ——點擊Components——選擇對應的倉庫名稱(maven-proxy)。

配置npm-proxy
  • 備份已有npm配置文件(.npmrc),如果沒有,忽略此步驟;

  • 進入web端,配置Nexus:

    • 配置repository:

      20181009153905348754188.png

    • 選擇npm(proxy): 20181009153906646331864.png

    • 點擊“Create repository

  • 進入命令行,配置npm:

    $ npm config set registry http://localhost:8081/repository/npm-proxy
    
    $ vim package.json
    {
      "name": "npm-proxy",
      "version": "0.0.1",
      "description": "Test Project 1",
      "dependencies" : {
        "commonjs" : "0.0.1"
      }
    }
    
  • 構建npm-proxy:

    $ npm install
    

管理nexus

Nexus倉庫分類
  • hosted 宿主倉庫:主要用於部署無法從公共倉庫獲取的構件(如 oracle 的 JDBC 驅動)以及自己或第三方的項目構件;
  • proxy 代理倉庫:代理公共的遠程倉庫;
  • virtual 虛擬倉庫:用於適配 Maven 1;
  • group 倉庫組:Nexus 通過倉庫組的概念統一管理多個倉庫,這樣我們在項目中直接請求倉庫組即可請求到倉庫組管理的多個倉庫。

20181009153909089499206.png

參考文檔

https://help.sonatype.com/repomanager3

http://www.mdslq.cn/archives/5f9114b.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章