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 · 访问量 3233

猜你喜欢

转载自blog.csdn.net/tian_111222333/article/details/100159983