背景

自己所研发的项目是内外网分离的,所以需要在内网就需要搭建maven私服。之前的老哥们用的是nexus2+,想着以后可能会升级版本以及自己的兴趣,所以预研一波nexus3+,终归还是被我搭建好了

操作步骤

1.安装

1.下载安装包,我使用的是nexus3+
2.拖到linux目录下解压
3 .启动服务,查看前台是否可以访问(这里可以配置环境变量,但没必要)
cd /home/centos7_enviorment/nexus/nexus-3.18.1-01/bin
./nexus start
前台默认密码是admin/admin123

nexus 配置的maven 路径 nexus导入本地maven仓库_jar包

2.启动关闭
# 开启nexus
./nexus start 
# 关闭nexus
./nexus stop 
# 开启nexus,会打印出堆栈信息(如果启动失败可以使用这个命令进行定位)
./nexus run
3.新建用户名和仓库
新建用户名:

nexus 配置的maven 路径 nexus导入本地maven仓库_nexus 配置的maven 路径_02

nexus 配置的maven 路径 nexus导入本地maven仓库_jar包_03


新建仓库

需要创建三个仓库


lgw-group(Maven2 group) --用来管理下面两个,并且


lgw-rep(Maven2 hosted)–自己定义的仓库,放一些内部jar包


lgw-proxy(Maven2 proxy)–代理,配置远程仓库源(比如阿里云)


最终在mavensetting文件里面应用的是lgw-group的地址


nexus 配置的maven 路径 nexus导入本地maven仓库_java_04


nexus 配置的maven 路径 nexus导入本地maven仓库_maven_05


nexus 配置的maven 路径 nexus导入本地maven仓库_nexus 配置的maven 路径_06

4.批量上传jar包
4.1 jar包上传有两种方式,前台页面上传,也可以通过脚本在后台批量上传。
这里要注意的是nexus2+上传的jar包在服务器就是以jar包保存的,而nexus3+是以字节文件保存的,所以find / -name “{jar名称}”在nexus3+是找不到的。
4.2 操作步骤是:
将要上传的jar包文件和脚本放在同一目录,给脚本赋可执行权限,然后执行脚本命令

nexus 配置的maven 路径 nexus导入本地maven仓库_xml_07


命令如下,上传成功截图如下:


./mavenimport.sh -u lgw -p 123456 -r http://192.168.207.102:8089/repository/lgw-rep/

nexus 配置的maven 路径 nexus导入本地maven仓库_maven_08

#!/bin/bash
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}/{} ;

5.1.本地配置私服地址,引用jar包,这里主要修改maven的settings文件

<?xml version="1.0" encoding="UTF-8"?>
<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>D:\lgw_repository</localRepository>
  <servers>
    <server>
      <id>lgw</id>
      <username>lgw</username>
      <password>123456</password>
    </server>
  </servers>
<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>
    <profile>
      <id>lgw-profile</id>
       <!-- 远程仓库列表 -->
      <repositories>
        <repository>
          <id>lgw</id>
          <name>lgw Central</name>
         <!-- 虚拟的URL形式,指向镜像的URL-->
          <url>http://192.168.207.102:8089/repository/lgw-group/</url>
        </repository>
      </repositories>
   </profile>
</profiles>
  <activeProfiles>
     <!--需要激活 <profile>中的ID才生效-->  
    <activeProfile>lgw-profile</activeProfile>
    <activeProfile>jdk-1.8</activeProfile>
  </activeProfiles>
</settings>

修改完之后,idea配置maven,看能否导入jar包如下,测试成功

nexus 配置的maven 路径 nexus导入本地maven仓库_xml_09