内网本地maven仓库nexus

发布时间 2023-05-03 17:17:10作者: Esquecer

需要git,nexus,maven。

 nexux版本:nexus-3.52

解压后:

 到创建用户名过程省略。

登陆网址:http://localhost:8081/

一:新建bolb store,存放上传的jar依赖

 

二:新建一个maven仓库

因为是本地,所以用hosted

                        创建仓库按钮

                          选择仓库类型

 

 

 三:上传jar包到仓库中

 ①:新建一个文件夹,把外网的.m2文件拷贝到该文件下。

 

 ②:在该文件夹下新建一个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}/{} ;

③:git批量上传到仓库

./mavenimport.sh -u 用户名 -p 密码 -r http://服务器IP:8081/repository/zxyy_maven/

其中用户名/密码是登录nexus的用户名密码,仓库位置从nexus上复制。如下图,一般Localhost要换成ip

 

④:最后输入上述命令上传到nexus的仓库中,至此nexus已经搭建本地仓库

 

 四:使用nexus仓库更新本地仓库

 maven的setting文件参考

<?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">
    <pluginGroups>
  </pluginGroups>
  <proxies>
  </proxies>

 <servers>
    <!--第一个nexus-xu要和下面的mirror中的id一致,代表拉取是也需要进行身份校验-->
    <server>
      <id>zxyy_maven</id>
      <username>admin</username>
      <password>admin123</password>
    </server>
     <server>
      <!--这两个前面讲过,是jar上传时候进行的验证,id对应的是pom中id属性的值-->
      <id>releases</id>
      <username>admin</username>
      <password>admin123</password>
    </server>
     <server>
      <id>snapshots</id>
      <username>admin</username>
      <password>admin123</password>
    </server>
  </servers>

  <mirrors>
    <mirror>
        <id>zxyy_maven</id>
        <name>internal nexus repository</name>
        <!--镜像采用配置好的组的地址-->
        <url>http://192.168.106.125:8081/repository/zxyy_maven/</url>
        <mirrorOf>!internal.repo,*</mirrorOf>
    </mirror>
  </mirrors>

  <profiles>
<profile>
  <!--ID用来确定该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>nexus-pr</id>
   <!-- 远程仓库列表 -->
  <repositories>
    <repository>
      <id>zxyy_maven</id>
      <name>Nexus Central</name>
     <!-- 虚拟的URL形式,指向镜像的URL-->
      <url>http://192.168.106.125:8081/repository/zxyy_maven/</url>
      <layout>default</layout>
     <!-- 表示可以从这个仓库下载releases版本的构件-->  
      <releases>
        <enabled>true</enabled>
      </releases>
     <!-- 表示可以从这个仓库下载snapshot版本的构件 -->  
      <snapshots>
        <enabled>true</enabled>
      </snapshots>
    </repository>
  </repositories>
   <!-- 插件仓库列表 -->
  <pluginRepositories>
    <pluginRepository>
      <id>zxyy_maven</id>
      <name>Nexus Central</name>
      <url>http://47.96.44.110:8081/repository/java-group/</url>
      <layout>default</layout>
      <snapshots>
        <enabled>true</enabled>
      </snapshots>
      <releases>
         <enabled>true</enabled>
      </releases>
    </pluginRepository>
  </pluginRepositories>
   </profile>
  </profiles>

  <activeProfiles>
     <!--需要激活 <profile>中的ID才生效-->  
    <activeProfile>nexus-pr</activeProfile>
    <activeProfile>jdk-1.8</activeProfile>
  </activeProfiles>
</settings>
View Code

 

五:内网项目的pom.xml文件需要更新

 

    <repositories>
<!--        <repository>-->
<!--            <id>public</id>-->
<!--            <name>aliyun nexus</name>-->
<!--            <url>https://maven.aliyun.com/repository/public</url>-->
<!--            <releases>-->
<!--                <enabled>true</enabled>-->
<!--            </releases>-->
<!--        </repository>-->
        <repository>
            <id>zxyy_maven</id>
            <name>zxyy_maven</name>
            <url>http://192.168.106.125:8081/repository/zxyy_maven/</url>
        </repository>
    </repositories>
View Code

 

六:idea尝试拉取远程的内网maven仓库,测试可以拉取到本地