Maven私仓 — Nexus搭建与使用

Maven私服-Nexus搭建与使用

    • 一、安装Nexus
      • 1、解压nexus压缩包
      • 2、打开window的命令窗口
      • 3、启动Nexus服务
      • 4、打开网站
      • 5、登入网站
    • 二、搭建maven私仓
      • 1、创建Blob Stores
      • 2、添加新的仓库
      • 3、创建release发布库
      • 4、创建snapshot快照库
      • 5、创建一个proxy类型的仓库
      • 6、创建一个group类型的仓库
    • 三、上传资源插件到私有仓库
      • 1、编写批量上传脚本
      • 2、打开Git Bash 客户端命令工具
    • 四、maven配置文件settings.xml


一、安装Nexus

为所有来自中央仓库的构建安装提供本地缓存。

下载网站:https://help.sonatype.com/repomanager3/download/download-archives—repository-manager-3

注意:安装JDK1.8以上的版本

1、解压nexus压缩包

Maven私仓 — Nexus搭建与使用_第1张图片

2、打开window的命令窗口

提示:用管理员身份运行cmd窗口,否则注册服务时会报错

切换到 nexus-3.15.2-01 文件夹中的bin目录下,运行 nexus.exe /install nexus 命令,注册服务

Maven私仓 — Nexus搭建与使用_第2张图片

3、启动Nexus服务

在bin文件夹下运行nexus /run命令

在这里插入图片描述

注意:启动命令会因为版本不同,指令也不同

4、打开网站

http://localhost:8081

Maven私仓 — Nexus搭建与使用_第3张图片

5、登入网站

用户名:admin

密码:admin123

查看现有仓库

Maven私仓 — Nexus搭建与使用_第4张图片

  • proxy 表示远程获取

  • group 组

  • hosted 本机,第三方jar,如数据库驱动等,需要手动添加

  • maven-central:maven中央库,默认从https://repo1.maven.org/maven2/获取jar

  • maven-releases:私库发行版jar

  • maven-snapshots:私库快照(调试版本)jar

  • maven-public:仓库分组,把上面三个仓库组合在一起对外提供服务,在本地maven基础配置settings.xml中使用


二、搭建maven私仓

1、创建Blob Stores

在这里插入图片描述

Maven私仓 — Nexus搭建与使用_第5张图片

  • Type:Type的select选项框中有"File"和"S3"两个选项,这里选File就行。"S3"是亚马逊S3云存储,一般用不到

  • Name:自定义

  • Enable Soft Quota:限制目录的大小,如果要限制的话,就勾选上,并填上限制的条件和限制的值就OK了。

  • Path:在填入Name之后,path会自动生成

2、添加新的仓库

Maven私仓 — Nexus搭建与使用_第6张图片

3、创建release发布库

Maven私仓 — Nexus搭建与使用_第7张图片

Maven私仓 — Nexus搭建与使用_第8张图片

  • Name:nexus-releases-my

  • Online:勾选,可以设置这个仓库是在线还是离线。

  • Mavne 2
    Version Policy:版本策略,有三个选项

    • Release:正式版本(选择)
    • Snapshot:快照版本
    • Mixed:混合模式
  • Layout Policy:布局策略

    • Strict:严格(选择默认)
    • Permissive:宽松
  • Storage

    • Blob store:选择此仓库使用的Blob存储,这里选择之前创建的 lk-maven。
    • Strict Content Type Validation:验证上传内容格式,这里不勾选。
  • Hosted
    Deployment Policy:部署策略,有三个选项:

    • Allow Redeploy:允许重新部署
    • Disable Redeploy:禁止重新部署(选择默认)
    • Read-Only:只读

4、创建snapshot快照库

点击"Repository"–>“Repositories”–>“Create repository”,选择maven2(hosted)。

Maven私仓 — Nexus搭建与使用_第9张图片

  • Name:nexus-snapshot-my
  • Mavne 2:选择 Snapshot(快照版本)

5、创建一个proxy类型的仓库

点击"Repository"–>“Repositories”–>“Create repository”,选择maven2(proxy)。

Maven私仓 — Nexus搭建与使用_第10张图片

  • Name:个人要代理阿里云的maven,自定义"maven-proxy-aliyun"

  • Online:勾选,设置成在线

  • Proxy

    Remote storage:设置远程中央仓库的地址,

    这里设置成阿里云的maven仓库地址—http://maven.aliyun.com/nexus/content/groups/public

    其他的用默认值即可。

  • Storage

    Blob store:选择 lk-maven
    Strict Content Type Validation:不勾选

6、创建一个group类型的仓库

点击"Repository"–>“Repositories”–>“Create repository”,选择maven2(gruop)。

Maven私仓 — Nexus搭建与使用_第11张图片

  • Group
    将左侧的Available中的仓库列表添加到右侧的Members中,顺序建议阿里云的代理仓库放第一,因为maven代理仓库下载顺序是从上往下的,建议将速度快的仓库放在最前面。

三、上传资源插件到私有仓库

说明:该部分内容转载自:


Nexus3.x批量导入本地库:https://blog.csdn.net/u014468095/article/details/87261817

1、编写批量上传脚本

mavenimport.sh

#!/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 ":r:u:p:" opt; do
	case $opt in
		r) REPO_URL="$OPTARG"
		;;
		u) USERNAME="$OPTARG"
		;;
		p) PASSWORD="$OPTARG"
		;;
	esac
done
 
find . -type f -not -path './mavenimport\.sh*' -not -path '*/\.*' -not -path '*/\^archetype\-catalog\.xml*' -not -path '*/\^maven\-metadata\-local*\.xml' -not -path '*/\^maven\-metadata\-deployment*\.xml' | sed "s|^\./||" | xargs -I '{}' curl -u "$USERNAME:$PASSWORD" -X PUT -v -T {} ${REPO_URL}/{} ;

注 意 : \color{red}{注意:}

(1)运行 mavenimport.sh 脚本前请先安装Git(Git的安装及使用请自行百度)

(2)请将编写好的 mavenimport.sh 放在本地仓库目录下(参考E:\Maven\apache-maven-3.6.3\repository)

Maven私仓 — Nexus搭建与使用_第12张图片

2、打开Git Bash 客户端命令工具

进入到本地仓库目录下(见上图),执行如下命令(后面地址自行更改):

Maven私仓 — Nexus搭建与使用_第13张图片

./mavenimport.sh -u admin -p admin123 -r http://localhost:8081/repository/nexus-releases-my/

待本地仓库jar包批量上传完毕即可(过程可能有点慢)


四、maven配置文件settings.xml


<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>E:\Maven\apache-maven-3.6.3\repositorylocalRepository>

  <pluginGroups>
    
  pluginGroups>

  <proxies>
    
  proxies>


  <servers>
      
    <server>
      <id>nexus-groupid>
      <username>adminusername>
      <password>admin123password>
    server>
      
	
    <server>
      <id>releaseid>
      <username>adminusername>
      <password>admin123password>
    server>
      
    <server>
      <id>snapshotsid>
      <username>adminusername>
      <password>admin123password>
    server>

  servers>

  <mirrors>
  
  	<mirror>
		<id>nexus-groupid>
		<name>internal nexus repositoryname>
		
		<url>http://IP:8081/repository/nexus-groupurl>
		<mirrorOf>!internal.repo,*mirrorOf>
	mirror>
  mirrors>

  <profiles>
     <profile>
      <id>default_profileid>
      <repositories>
        
        <repository>
          <id>nexusid>
          <name>Nexus Centralname>
            
          <releases>
            <enabled>trueenabled>
            <updatePolicy>neverupdatePolicy>
            <checksumPolicy>warnchecksumPolicy>
          releases>

          <snapshots>
            <enabled>trueenabled>
            <updatePolicy>alwaysupdatePolicy>
            <checksumPolicy>warnchecksumPolicy>
          snapshots>

          <url>http://IP:8081/repository/nexus-groupurl>
          <layout>defaultlayout>
        repository>
      repositories>
      
      <pluginRepositories>  
        <pluginRepository>  
          <id>nexusid>  
          <name>Nexus Centralname>  
          <url>http://IP:8081/repository/nexus-groupurl>  
          <releases>  
            <enabled>trueenabled>  
          releases>  
          <snapshots>  
            <enabled>trueenabled>  
          snapshots>      
        pluginRepository>  
      pluginRepositories> 
  
    profile>
  profiles>

  <activeProfiles>
    <activeProfile>default_profileactiveProfile>
  activeProfiles>

settings>

如需转载,请标明文章出处,感谢!

你可能感兴趣的:(Maven,maven)