Maven私有仓库本地配置

介绍

Maven仓库用来存放Maven管理的所有Jar包。分为:本地仓库 和 中央仓库。

本地仓库:Maven本地的Jar包仓库,默认地址:${maven_home}/.m2/

中央仓库:Maven官方提供的远程仓库,或者是公司私有仓库(Nexus)

当项目编译时,Maven首先从本地仓库中寻找项目所需的Jar包,若本地仓库没有,再到Maven的中央仓库下载所需Jar包。当执行Install或者deploy时,本地打出的Jar包就会更新到本地仓库或者是远程私有仓库,供其它项目依赖。

本文涉及的配置操作基于maven版本: 3.6.3验证通过。

使用

一、操作步骤

1.在本地maven的 settings.xml 配置使用公司maven仓库时,配置个人账号,在 servers 节点增加配置(需要则配置,不需要则跳过)

本地密码如果不想以明文方式暴露可以参考: maven仓库密码加密

<server>

    <id>pig-repo</id>

    <username>个人账号</username>

    <password>个人账号的密码</password>

</server>

附:maven仓库密码加密

a.设置加密盐值

> mvn --encrypt-master-password Master password: // 输入盐值 {sc8pIfgAx+kHX9p3pJLwIX6JR7v6F5sLzLMLFQFiE9w=} // 此处为给密码加密的加密盐值

b.配置${maven_home}/.m2/settings-security.xml文件,文件不存在则手动创建,加密盐值配置

<settingsSecurity>

    <master>{sc8pIfgAx+kHX9p3pJLwIX6JR7v6F5sLzLMLFQFiE9w=}</master> </settingsSecurity>

c.对实际使用的密码进行加密,生成密文

> mvn --encrypt-password Password: // 输入明文密码 {OaNCAPjeNW4HwnLPIpwcU9P2B3hBAWpWJTXJqd/KyrQka9axIaNZcHCNH+KRjxDi} //加密后的密码

d.将上一步生成的密码配置setting.xml

<server>

   <id>pig-repo</id>

    <username>admin</username>

   <password{OaNCAPjeNW4HwnLPIpwcU9P2B3hBAWpWJTXJqd/KyrQka9axIaNZcHCNH+KRjxDi</password> </server>

2.在本地maven的 settings.xml 配置使用公司maven仓库,在 mirrors 节点增加配置,settings文件的默认地址:${maven_home}/.m2/config

注意:要使用用户名密码的话 mirror 的 id 就需要跟 server 的 id 保持一致

<mirrors>
    <!-- 私库镜像
    如果私库要使用账号密码那么这里 mirror中 的id就要跟 server 中的id保持一致
    因为 Maven 是通过 id 来对比变量和匹配的
    mirrorOf 替代方式, 
      *: 匹配所有,所有内容都从这个镜像拉取
      external:*: 除了本地缓存的所有从镜像仓库拉取;
      repo,repo1: repo或者repo1从这个镜像进行拉取,这里的repo指的profile的ID
      *,!repo1: 除了repo1的所有仓库;
    -->
    <mirror>
      <id>pig-repo</id>
      <!-- dev 的从这个镜像拉取, 这里的名称跟下面对应的 profile 的id保持一致 -->
      <mirrorOf>pig</mirrorOf>
      <name>公司私库</name>
      <url>http://192.10.10.72/repository/maven-public/</url>
    </mirror>

    <!-- 阿里云镜像 -->
    <mirror>
      <id>aliyunmaven</id>
      <!-- aliyun 的从这个镜像拉取, 这里的名称跟下面对应的 profile 的id保持一致 -->
      <mirrorOf>aliyun</mirrorOf>
      <name>阿里云公共仓库</name>
      <!-- <url>https://maven.aliyun.com/repository/public</url> -->
      <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
    </mirror>
</mirrors>

在本地maven的 settings.xml 配置支持从snapshots仓库拉取快照,在 profiles 节点增加配置

<profiles>
    <!-- 阿里云仓库配置 -->
    <profile>
      <!-- 这里的id名称跟上面对应的 mirror 镜像的 mirrorOf 名称保持一致 -->
      <id>aliyun</id> 
      <repositories>
        <repository>
          <id>aliyun</id>
          <!-- <url>https://maven.aliyun.com/repository/public</url> -->
          <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
          <releases>
            <enabled>true</enabled>
          </releases>
          <snapshots>
            <enabled>true</enabled>
          </snapshots>
        </repository>
      </repositories>
    </profile>

   <!-- 私库仓库配置 -->
    <profile>
        <!-- 这里的id名称跟上面对应的 mirror 镜像的 mirrorOf 名称保持一致 -->
         <id>pig</id>
            <repositories>
                <repository>
                  <!-- 如果要使用用户名密码的话 repository 的id就要和mirror的id保持一致 -->
                  <id>pig-repo</id>
                  <url>http://192.10.10.70:8081/repository/maven-public/</url>
                  <releases>
                    <enabled>true</enabled>
                  </releases>
                  <snapshots>
                    <enabled>true</enabled>
                  </snapshots>
                </repository>
            </repositories>     
    </profile>
</profiles>

3.在本地maven的 settings.xml 激活配置,在 activeProfiles 节点增加配置

<activeProfiles>
    <activeProfile>pig</activeProfile>
</activeProfiles>

二、注意事项

1、报错 401 ReasonPhrase: Unauthorized 的解决方法

[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.999 s
[INFO] Finished at: 2021-07-20T15:40:42+08:00
[INFO] Final Memory: 7M/164M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy-file (default-cli) on project standalone-pom: Failed to deploy artifacts: Could not transfer artifact com.xxx.xx.core:xxx-core:jar:2.0.7-20210720.074039-15 from/to public (http://192.79.172.97:9000/nexus/content/repositories/public/): Failed to transfer file: http://192.79.172.97:9000/nexus/content/repositories/public/com/xxx/xx/xx/xx-

core/2.0.7/xxx-core-2.0.7-20210720.074039-15.jar. Return code is: 401, ReasonPhrase: Unauthorized. -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

解决方法一: 在setting.xml 配置权限

注:id与命令中的repositoryId值一致

username:即 你的私库账号

password:即 你的私库密码

<server> 
    <id>snapshots</id> 
    <username>username</username>
    <password>password</password>
</server>

解决方法二: 直接在命令 url中 带权限执行

我采用的是这种

解决方法三:远程仓库设置匿名模式

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值