搭建maven私服nexus

搭建maven私服nexus

背景:因工作需要,需要搭建一个maven私服。先在Linux虚拟机安装nexus,本地maven连接上传下载jar。

1 下载nexus

已保存至网盘 nexus-3.19.1-01-unix.tar.gz

2 解压操作

解压后为 nexus-3.19.1-01 及 sonatype-work。

初始密码在sonatype-work/nexus3/admin.password文件中。

3 基础配置

登陆后 修改maven-central的 remote storage : https://maven.aliyun.com/repository/central

4 本地配置

setting.xml

<?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>E:\maven_repository_nexus</localRepository>

    <!--设置私库认证信息-->
    <servers>
        <server>
            <id>maven-releases</id>
            <username>admin</username>
            <password>admin</password>
        </server>
        <server>
            <id>maven-snapshots</id>
            <username>admin</username>
            <password>admin</password>
        </server>
    </servers>

    <!--设置maven私库信息 ?-->
    <profiles>
        <profile>
            <id>nexus</id>
            <repositories>
                <repository>
                    <id>nexus-repository</id>
                    <name>Nexus</name>
                    <url>http://192.168.213.129:8081/repository/maven-public/</url>
                    <releases><enabled>true</enabled></releases>
                    <snapshots><enabled>true</enabled></snapshots>
                </repository>
            </repositories>
            <pluginRepositories>
                <pluginRepository>
                    <id>nexus-pluginRepository</id>
                    <name>Nexus</name>
                    <url>http://192.168.213.129:8081/repository/maven-public/</url>
                    <releases><enabled>true</enabled></releases>
                    <snapshots><enabled>true</enabled></snapshots>
                </pluginRepository>
            </pluginRepositories>
        </profile>
      
    </profiles>
    <!--激活私库信息的配置-->
    <activeProfiles>
        <activeProfile>nexus</activeProfile>
    </activeProfiles>

    <mirrors>
        <mirror>
            <id>maven-releases</id>
            <name>internal nexus repository</name>
            <url>http://192.168.213.129:8081/repository/maven-public/</url>
            <!--此处配置所有的构建均从私有仓库中下载 *代表所有,也可以写central -->
            <mirrorOf>*</mirrorOf>
        </mirror>
    </mirrors>
</settings>

此时idea项目配置如果选择的是此setting文件,此项目已经可以从私服拉取jar。

5 项目配置

idea项目pom内容增加:

  <distributionManagement>
    <repository>
      <!--id的名字可以任意取,但是在setting文件中的属性<server>的ID与这里一致-->
      <id>maven-releases</id>
      <!--指向仓库类型为host(宿主仓库)的储存类型为Release的仓库-->
      <url>http://192.168.213.129:8081/repository/maven-releases/</url>
    </repository>
    <snapshotRepository>
      <id>maven-snapshots</id>
      <!--指向仓库类型为host(宿主仓库)的储存类型为Snapshot的仓库-->
      <url>http://192.168.213.129:8081/repository/maven-snapshots/</url>
    </snapshotRepository>
  </distributionManagement>

此时,mvn deploy 可以将项目传至私服。

6 批量上传jar至私服

我们在/opt新建目录nexus-repo

在nexus-repo目录i下新建脚本 vim bacthimports.sh

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 './bacthimports\.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}/{} ;

将需要上传的jar目录上传至此文件夹执行

./bacthimports.sh -u admin -p admin -r http://192.168.213.128:8081/repository/maven-releases/

运行完刷新浏览器即可看到已经成功。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值