Linux Nexus Repository Manager OSS 3.18.1-01 搭建指南

因爲項目需要,需要給開發團隊在內網環境下搭建私服,同時完成相關工作筆記本本地maven倉庫jar的快速遷移到私服。花了點時間查了網上的相關資料,基本上都能找到相應的內容,本人只是將搭建過程中用到的相關資料做了梳理,方便下次使用。

環境說明

官方系統要求文檔

簡單概括下

  1. java 版本 要求jdk 1.8+,jvm按需配置,官方系統要求文檔裏有指導說明
  2. 磁盤空間適當就行,按需配置
  3. 瀏覽器IE11+,其他火狐、谷歌、Safari、Edge用最新版即可

Setp1 nexus3.18.1-01-unix 下載

官網地址 想要下載更新的版本的可以自行去找最新版本
nexus3.18.1-01-unix 官網下載鏈接
爲了避免下載不了,提供百度網盤下載地址:https://pan.baidu.com/s/1ibe4qkzAa2B-wbMApR0i0w 提取碼:c4uq

Setp2 解壓

tar -xvf nexus-3.18.1-01-unix.tar.gz

./nexus-3.18.1-01 程序home路徑
./sonatype-work 工作目錄,包含緩存信息,日誌,上傳到私服的相關包的信息

Step3 環境變量配置

nexus的環境變量可以不配置,java的參數必須配

vim /etc/profile 
#配置JAVA_HOME和NEXUS_HOME
export JAVA_HOME=/usr/java/jdk1.8.0_181
export NEXUS_HOME=/home/nexus/nexus-3.18.1-01
export PATH=$JAVA_HOME/bin/:$PATH:$NEXUS_HOME/bin;
#完成以後使更改後的文件立即生效
source /etc/profile
#檢驗配置是否成功
nexus

在這裏插入圖片描述

Setp4 啓動

使用root賬號來跑服務,這種情況下會給出一個提醒
[外鏈圖片轉存失敗(img-M4fvqar3-1567150210496)(./1567075962913.png)]
nexus run

  • 當你不確定環境是否ok時,建議先用這個,會在界面直接打印程序日誌。類似java -jar 啓動服務,退出當前窗口以後進程被殺死。

nexus start/stop/restart/force-reload

  • 正常後臺啓動服務**/關閉/重啓/** force-reload和restart實際執行的是一樣的操作

日誌路徑:${安裝路徑}/sonatype-work/nexus3/log/
在這裏插入圖片描述

Step5 地址和權限配置

訪問私服地址

訪問地址: http://ip:8081/ 8081是默認程序端口
默認情況下,匿名賬號(不登陸)可以查看私服相關倉庫的地址信息,可以下載到倉庫的jar

admin登陸

從服務器找到admin密碼登錄。
在這裏插入圖片描述
初次登錄以後讓你選擇是否做默認事件:

  • 讓你修改admin密碼
  • 設置是否允許匿名賬號(不登陸)訪問私服倉庫,下載私服的jar

默認建好的倉庫

  • maven-releases (Version policy=Release)默認只允許上傳不帶SNAPSHOT版本尾綴的包,默認部署策略是Disable redeploy 不允許重複上傳相同版本號信息的jar,避免包版本更新以後使用方無法獲取到最新的包。
  • maven-snapshots (Version policy=Snapshot)只允許上傳帶SNAPSHOT版本尾綴的包,默認部署策略是Allow redeploy,允許重複上傳相同版本號信息的jar,每次上傳的時候會在jar的版本號上面增加時間後綴信息。
  • maven-central 中央倉庫的拷貝,如果環境可以訪問中央倉庫,則可以獲取到相關的包,否則沒用
  • maven-public 倉庫組,不是實際個一個倉庫地址,只是將現有的組合到一次,可以通過它看到所屬組內全部倉庫的jar信息
  • 針對當前的情況,需要自建第三方的倉庫3rd-party


Version policy 如果選擇了Release 或者 Snapshot 就會有上述限制,按需選擇

  • 將3rd-party 倉庫加到public組

在這裏插入圖片描述

權限設置

這塊權限設計依照經典的權限Role-based Access Control,基於角色的權限控制模型。

  • Privileges 權限信息

目前分爲頁面編輯部分默認已經建好,而且不允許編輯;倉庫部分,默認倉庫的相關權限也已經建好,並且不允許編輯,可以針對新建的倉庫進行權限的編輯(nexus2.x版本新建倉庫代碼默認生成view查看權限,其他需要新建)

  • nx-repository-admin 開頭的是針對倉庫本身的相關權限
  • nx-repository-view 開頭的是針對倉庫裏jar的相關權限
  • Roles 角色信息

角色擁有相應的具體權限,當前服務默認生產兩種權限

  • nx-admin 超管權限
  • nx-anonymous 匿名用戶權限
  • User 用戶,選擇分配相應的角色,擁有角色所屬的權限

nexus對於上傳Upload權限(Privilege name=nx-component-upload)的控制比較特殊,Web網頁上只允許針對Version policy!=Snapshot類型的倉庫提供操作界面。而且目前該權限無法針對倉庫來控制,只要你有這個權限,就可以看到全部允許做Upload操作的倉庫,但是可以通過針對倉庫包的相關權限來限制


本人按需增加了兩個權限

  • nx-develop 開發者( 匿名權限+Snapshots+3rd party類型倉庫庫的上傳權限)
    在這裏插入圖片描述
  • nx-deployment 運維權限(匿名權限+全類型倉庫的上傳權限)
    在這裏插入圖片描述

新增用戶以及相關權限演示

  • 新增用戶操作界面
    在這裏插入圖片描述

developer(擁有3rd-party 和 snapshot類型倉庫包內容修改的完整權限)

有頁面上傳的權限,但是沒有針對releases倉庫的修改權限,這樣也無法上傳包有頁面上傳的權限,但是沒有針對releases倉庫的修改權限,這樣也無法上傳包
3rd-party 包上傳
3rd-party 包刪除在這裏插入圖片描述


deployper(擁有所有類型倉庫包內容修改的完整權限)

releases倉庫包刪除在這裏插入圖片描述
releases倉庫包上傳在這裏插入圖片描述

Step5 批量上傳本地jar到私服

藉助shell腳本來做相關實現,整體思路就是利用nexus的api結合管道將find 指令查找文件(含路徑)列表信息作爲參數實現快速完成功能

由於腳本中find 後面使用的是 . 路徑,需要進入到jar存放的目錄以後去執行腳本,此處的路徑爲本地倉庫地址(目前這部分API沒有在nexus的管理後臺找到,這塊究竟需要什麼權限也不明確,目前的developer和deployper都有權限可以做這個事 )

執行腳本
在這裏插入圖片描述

#!/bin/bash
# copy and run this script to the root of the repository directory containing files
# this script attempts to exclude uploading itself explicitly so the script name is important
# Get command line params
while getopts ":u:p:" opt; do
	case $opt in
		u) USERNAME="$OPTARG"
		;;
		p) PASSWORD="$OPTARG"
		;;
	esac
done
 
#將第三方的包導入3rd-party倉庫
find . -type f -not -path './maven-auto-put-to-nexus\.sh*' -not -path '*/\.*' -not -path '*/\^archetype\-catalog\.xml*' -not -path '*/\^maven\-metadata\-local*\.xml' -not -path '*/\^maven\-metadata\-deployment*\.xml' |grep -E "jar$|pom$" |grep -viE "snapshot" |grep -Ev "yuntai|hip" | sed "s|^\./||" | xargs -I '{}' curl -u "$USERNAME:$PASSWORD" -X PUT -v -T {} http://ip:8081/repository/3rd-party//{} ;


#將公司相關的jar導入maven-snaphosts倉庫
find . -type f -not -path './maven-auto-put-to-nexus\.sh*' -not -path '*/\.*' -not -path '*/\^archetype\-catalog\.xml*' -not -path '*/\^maven\-metadata\-local*\.xml' -not -path '*/\^maven\-metadata\-deployment*\.xml' |grep -E "jar$|pom$" |grep -iE "snapshot" |grep -E "yuntai|hip"   | sed "s|^\./||" | xargs -I '{}' curl -u "$USERNAME:$PASSWORD" -X PUT -v -T {}  http://ip:8081/repository/maven-snapshots//{} ;


#將公司相關的jar導入maven-releases倉庫(不允許帶SNAPSHOT後綴)
find . -type f -not -path './maven-auto-put-to-nexus\.sh*' -not -path '*/\.*' -not -path '*/\^archetype\-catalog\.xml*' -not -path '*/\^maven\-metadata\-local*\.xml' -not -path '*/\^maven\-metadata\-deployment*\.xml' |grep -E "jar$|pom$" |grep -viE "snapshot"  |grep -E "yuntai|hip"   | sed "s|^\./||" | xargs -I '{}' curl -u "$USERNAME:$PASSWORD" -X PUT -v -T {}  http://ip:8081/repository/maven-releases//{} ;

Step6 maven上傳本地jar到私服

使用maven 也可以實現批量上傳包到私服,只是目前需要編寫腳本來完成參數的拼裝,沒有上面的方式來的方便。

配置setting.xml

setting.xml中 server id、repository id 和maven 倉庫中的名稱要一致
在這裏插入圖片描述

<?xml version="1.0" encoding="UTF-8"?>

<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.
-->

<!--
 | This is the configuration file for Maven. It can be specified at two levels:
 |
 |  1. User Level. This settings.xml file provides configuration for a single user, 
 |                 and is normally provided in ${user.home}/.m2/settings.xml.
 |
 |                 NOTE: This location can be overridden with the CLI option:
 |
 |                 -s /path/to/user/settings.xml
 |
 |  2. Global Level. This settings.xml file provides configuration for all Maven
 |                 users on a machine (assuming they're all using the same Maven
 |                 installation). It's normally provided in 
 |                 ${maven.home}/conf/settings.xml.
 |
 |                 NOTE: This location can be overridden with the CLI option:
 |
 |                 -gs /path/to/global/settings.xml
 |
 | The sections in this sample file are intended to give you a running start at
 | getting the most out of your Maven installation. Where appropriate, the default
 | values (values used when the setting is not specified) are provided.
 |
 |-->
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" 
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
		  <!-- 
  <localRepository>/home/maven/repository</localRepository> 
  -->
  <localRepository>D:\AboutWork\maven\repository</localRepository>
  
  <!--Maven是否需要和用戶交互以獲得輸入。如果Maven需要和用戶交互以獲得輸入,則設置成true,反之則應爲false。默認爲true。 -->  
  <interactiveMode>true</interactiveMode>  
  
   <!--Maven是否需要使用plugin-registry.xml文件來管理插件版本。  -->  
   <!--如果設置爲true,則在{user.home}/.m2下需要有一個plugin-registry.xml來對plugin的版本進行管理  -->  
   <!--默認爲false。 -->  
   <usePluginRegistry>false</usePluginRegistry> 
   
   <!--表示Maven是否需要在離線模式下運行。如果構建系統需要在離線模式下運行,則爲true,默認爲false。  -->  
   <!--當由於網絡設置原因或者安全因素,構建服務器不能連接遠程倉庫的時候,該配置就十分有用。  -->  
   <offline>false</offline>  
   
   <!--當插件的組織Id(groupId)沒有顯式提供時,供搜尋插件組織Id(groupId)的列表。  -->  
   <!--該元素包含一個pluginGroup元素列表,每個子元素包含了一個組織Id(groupId)。  -->  
   <!--當我們使用某個插件,並且沒有在命令行爲其提供組織Id(groupId)的時候,Maven就會使用該列表。  -->  
   <!--默認情況下該列表包含了org.apache.maven.plugins和 org.codehaus.mojo -->  
   <pluginGroups>
		<pluginGroup>org.mortbay.jetty</pluginGroup>
		<pluginGroup>org.codehaus.cargo</pluginGroup>
		<pluginGroup>com.hundsun.scm.maven.plugins</pluginGroup> 
		<pluginGroup>org.apache.maven.plugins</pluginGroup> 
		<pluginGroup>org.codehaus.mojo</pluginGroup>
   </pluginGroups>


  <!--用來配置不同的代理,多代理profiles可以應對筆記本或移動設備的工作環境:通過簡單的設置profile id就可以很容易的更換整個代理配置。  -->  
    <proxies>  
            
        <!--代理元素包含配置代理時需要的信息
        <proxy>  
                
            代理的唯一定義符,用來區分不同的代理元素。 
            <id>myproxy</id>  
                
            該代理是否是激活的那個。true則激活代理。當我們聲明瞭一組代理,而某個時候只需要激活一個代理的時候,該元素就可以派上用處。
            <active>true</active>  
                
            代理的協議。 協議://主機名:端口,分隔成離散的元素以方便配置。
            <protocol>http://…</protocol>  
                
            代理的主機名。協議://主機名:端口,分隔成離散的元素以方便配置。
            <host>proxy.somewhere.com</host>  
                
            代理的端口。協議://主機名:端口,分隔成離散的元素以方便配置。 
            <port>8080</port>  
                
            代理的用戶名,用戶名和密碼錶示代理服務器認證的登錄名和密碼。 
            <username>proxyuser</username>  
                
           代理的密碼,用戶名和密碼錶示代理服務器認證的登錄名和密碼。
            <password>somepassword</password>  
                
            不該被代理的主機名列表。該列表的分隔符由代理服務器指定;例子中使用了豎線分隔符,使用逗號分隔也很常見。
            <nonProxyHosts>*.google.com|ibiblio.org</nonProxyHosts>  
                
        </proxy>  
           -->
    </proxies>  

<!-- 	<server> -->
<!-- 		<id>nexus</id> -->
<!-- 		<username>readOnly</username> -->
<!-- 		<password>hscmreadonly</password> -->
<!-- 	</server> -->
 <!-- Another sample, using keys to authenticate.
    <server>
      <id>siteServer</id>
      <privateKey>/path/to/private/key</privateKey>
      <passphrase>optional; leave empty if not used.</passphrase>
    </server>
    -->
    
	<servers>
		<server>
			<id>maven-snapshots</id>
			<username>username</username>
			<password>password</password>
		</server>
		<server>
			<id>maven-releases</id>
			<username>username</username>
			<password>password</password>
		</server>
	</servers>
    <!-- maven 默認的中央倉庫 -->
    <!--
	<repositories>
		<repository>
			<id> central</id>
			<name> Maven Repository Switchboard</name>
			<layout> default</layout>
			<url> http://repo1.maven.org/maven2</url>
			<snapshots>
				<enabled> false</enabled>
			</snapshots>
		</repository>
	</repositories> 
    -->
   
   <!-- 鏡像倉庫,將releases snapshots thirdparty的jar同步到一起-->
	<mirrors>
		<mirror>
			<!--This sends everything else to /public -->
			<id>public</id>
			<name> Maven Hundusn Yuntai Mirror(zjrc)</name>
			<mirrorOf>*</mirrorOf>
			<url>http://nexusIp:8081/repository/maven-public/</url>
		</mirror>   
	</mirrors>
	

    
  <!-- profiles
   | This is a list of profiles which can be activated in a variety of ways, and which can modify
   | the build process. Profiles provided in the settings.xml are intended to provide local machine-
   | specific paths and repository locations which allow the build to work in the local environment.
   |
   | For example, if you have an integration testing plugin - like cactus - that needs to know where
   | your Tomcat instance is installed, you can provide a variable here such that the variable is 
   | dereferenced during the build process to configure the cactus plugin.
   |
   | As noted above, profiles can be activated in a variety of ways. One way - the activeProfiles
   | section of this document (settings.xml) - will be discussed later. Another way essentially
   | relies on the detection of a system property, either matching a particular value for the property,
   | or merely testing its existence. Profiles can also be activated by JDK version prefix, where a 
   | value of '1.4' might activate a profile when the build is executed on a JDK version of '1.4.2_07'.
   | Finally, the list of active profiles can be specified directly from the command line.
   |
   | NOTE: For profiles defined in the settings.xml, you are restricted to specifying only artifact
   |       repositories, plugin repositories, and free-form properties to be used as configuration
   |       variables for plugins in the POM.
   |
   |-->
  <profiles>
	<profile>  
	    <id>jdk-1.8</id>  
	    <activation>  
			<activeByDefault>true</activeByDefault>  
			<jdk>1.8</jdk>  
		</activation>  
		<properties>  
			<maven.compiler.source>1.8</maven.compiler.source>  
			<maven.compiler.target>1.8</maven.compiler.target>  
			<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>  
		</properties>  
	</profile> 

   <!-- maven開發庫 -->
   <profile>
       <id>dev</id>
          <repositories>

			<repository>
                 <id>maven-releases</id>
                 <url>http://nexusIp:8081/repository/maven-releases/</url>
                 <releases>
                    <enabled>true</enabled>
					<updatePolicy>always</updatePolicy> 
                 </releases>
                 <snapshots>
                    <enabled>false</enabled>
                 </snapshots>
              </repository>
			  <repository>
                 <id>maven-snapshots</id>
				 <url>http://nexusIp:8081/repository/maven-snapshots/</url>
                 <releases>
                    <enabled>false</enabled>
                 </releases>
                 <snapshots>
                    <enabled>true</enabled>
					<updatePolicy>always</updatePolicy> 
                 </snapshots>
              </repository>
			</repositories>
        </profile>
  </profiles>
	<activeProfiles>
        <activeProfile>dev</activeProfile>
    </activeProfiles>
  <!-- activeProfiles
   | List of profiles that are active for all builds.
   |
  <activeProfiles>
    <activeProfile>alwaysActiveProfile</activeProfile>
    <activeProfile>anotherAlwaysActiveProfile</activeProfile>
  </activeProfiles>
  -->
</settings>

cmd/shell 命令行 mvn deploy

注意需要配置好環境變量(注意不要再本地maven倉庫下目錄下執行)
PS:當前本人暫時只能使用admin的賬號來實現這部分能,其他非admin賬號的暫時無權限做該項任務

mvn deploy:deploy-file -DgroupId=com.hip -DartifactId=mfc-core -Dversion=1.0 -Dpackaging=jar -Dfile=./mfc-core-1.0.jar -Durl=http://ip:8081/repository/maven-releases/ -DrepositoryId=maven-releases --settings C:\Users\yttiany\Desktop\settings.xml

在這裏插入圖片描述

開發工具mvn deploy

pom.xml 中增加倉庫地址配置,同樣需要注意id

<distributionManagement>
      <snapshotRepository>
        <id>maven-snapshots</id>
        <url>http://ip:8081/repository/maven-snapshots/</url>
      </snapshotRepository>
     <repository>
        <id>maven-releases</id>
         <url>http://ip:8081/repository/maven-releases/</url>
      </repository>
</distributionManagement>     

idea中deploy在這裏插入圖片描述

設置開啓啓動

官網介紹設置開機啓動方案
本人做法:在/etc/rc.local 文件最底下加入行 nexus start

參考資料

[1] 批量導入本地倉庫的jar到私服(參考腳本)https://blog.csdn.net/u014468095/article/details/87261817

[2] nexus 3.x 類似2.x默認新建的deployment權限描述 https://blog.csdn.net/diaoge_v5/article/details/84584382

發佈了22 篇原創文章 · 獲贊 1 · 訪問量 4979
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章