Maven 本地 Jar 依赖上传至远程仓库

受“删库跑路”影响,一些 “稀有” 依赖无法下载,只能借助 mvn deploy:deploy 临时将本地 Jar 包上传到远程仓库。

💡 TL;DR

  • 完形填空下面的关键命令
  • 不要直接上传本地 Maven 仓库的文件 (避免 400)
  • repositoryId 指定远程仓库账号密码 (避免401)

💻 关键命令

# @see: http://maven.apache.org/guides/mini/guide-3rd-party-jars-remote.html
mvn deploy:deploy-file -DgroupId=<group-id> \
  -DartifactId=<artifact-id> \
  -Dversion=<version> \
  -Dpackaging=<type-of-packaging> \
  -Dfile=<path-to-file> \
  -DrepositoryId=<id-to-map-on-server-section-of-settings.xml> \
  -Durl=<url-of-the-repository-to-deploy>

🌰 举个栗子

服务用到 CDH 的 Hive 和 Hadoop 依赖,因此以 hadoop-client 为例,针对关键命令完形填空

依赖标识

作为参考,我们首先看下该 jar 的 groupIdartifact-idversion

<!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-client -->
<dependency>
    <groupId>org.apache.hadoop</groupId>
    <artifactId>hadoop-client</artifactId>
    <version>2.6.0-cdh5.7.2</version>
</dependency>
参数
  • group-id: org.apache.hadoop
  • artifact-id: hadoop-client
  • version: 2.6.0-cdh5.7.2
  • type-of-packaging: jar

本地路径

本地 Jar 包的具体路径,注意不要直接使用 ~/.m2/repository(以 settings.xml 配置为准) 路径下的 jar,可以把文件复制一份到其他目录再上传,避免 400 问题上传失败。

参数

具体路径因人而异,不在本地 maven 仓库且指定 jar 文件即可

  • path-to-file: /Users/victor/test/maven/apache/hadoop/hadoop-client/2.6.0-cdh5.7.2/hadoop-client-2.6.0-cdh5.7.2.jar

远程地址

公司内部远程仓库地址可能有多个,分别对应不同仓库和功能,因此要确定正确上传的 repositoryIdurl

repositoryId

根据官方文档,repositoryId 应该叫 serverId 更合适?主要是用来指定上传时使用的账号密码,而非上传到哪个仓库。

# @see: https://maven.apache.org/plugins-archives/maven-deploy-plugin-2.8.2/deploy-file-mojo.html
Server Id to map on the <id> under <server> section of settings.xml In most cases, this parameter will be required for authentication.
Type: java.lang.String
Required: Yes
Expression: ${repositoryId}
Default: remote-repository

因此,repositoryId 的值为 settings.xml 文件 server 标签的 id,而非 repository 标签的 id。

即:

    <!-- maven 仓库用户认证信息, username / password 分别替换真实的账号密码 -->
    <servers>
        <server>
            <id>releases</id>
            <username>username</username>
            <password>password</password>
        </server>
        <server>
            <id>snapshots</id>
            <username>username</username>
            <password>password</password>
        </server>
    </servers>

非:

    <profiles>
        <profile>
            <id>xxx_nexus</id>
            <repositories>
                <repository>
                    <id>nexus</id>
                    <name>nexus</name>
                    <url>http://xxx.com/yyy/</url>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                    </snapshots>
                </repository>
            </repositories>
            <!-- others ... -->
        </profile>
      <!-- profiles ... -->
    </profiles>
url

url 指定上传到哪个仓库,我最初指定了公司的 public 地址,一直提示 401 上传失败;后来参考服务的打包上传 url,因为 mvn deploy:deploy-file 的地址和 mvn deploy总归是上传到同一个地方,于是成功找到并替换。

➜  xxx git:(master) ✗ cd xxx # 进入服务代码的本地路径
➜  xxx git:(master) ✗ mvn clean deploy
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO]
[INFO] foo                                                 [pom]
[INFO] foo-interface                                       [jar]
[INFO] foo-server                                          [jar]
[INFO]
[INFO] --------------< com.xxx:foo >--------------
[INFO] Building foo 1.0.1-SNAPSHOT                         [1/3]
[INFO] --------------------------------[ pom ]---------------------------------
...
[INFO] --- maven-deploy-plugin:2.8.2:deploy (default-deploy) @ foo-interface ---
Downloading from snapshots: http://xxx.com/yyy/zzz/releases/maven-metadata.xml
Downloaded from snapshots: http://xxx.com/yyy/zzz/releases/maven-metadata.xml (1.0 kB at 32 kB/s)
Uploading to snapshots: http://xxx.com/yyy/zzz/releases/foo-interface-1.0.1-20200310.153646-12.jar
Uploaded to snapshots: http://xxx.com/yyy/zzz/releases/foo-interface-1.0.1-20200310.153646-12.jar (76 kB at 163 kB/s)
Uploading to snapshots: http://xxx.com/yyy/zzz/releases/foo-interface-1.0.1-20200310.153646-12.pom
Uploaded to snapshots: http://xxx.com/yyy/zzz/releases/foo-interface-1.0.1-20200310.153646-12.pom (2.5 kB at 9.9 kB/s)

从上面可以看到 jar 和 pom 都上传到了下面的 url:

  • url: http://xxx.com/yyy/zzz/releases/
参数
  • repositoryId: releases (视情况而定)
  • url: http://xxx.com/yyy/zzz/releases/ (视情况而定)

🚀 最终命令

mvn \
   deploy:deploy-file -DgroupId=org.apache.hadoop \
  -DartifactId=hadoop-client \
  -Dversion=2.6.0-cdh5.7.2 \
  -Dpackaging=jar \
  -Dfile=/Users/victor/test/maven/apache/hadoop/hadoop-client/2.6.0-cdh5.7.2/hadoop-client-2.6.0-cdh5.7.2.jar \
  -DrepositoryId=releases \
  -Durl=http://xxx.com/yyy/zzz/releases/

⚠️ 注意事项

  • 不能直接上传本地 Maven 仓库的文件,可拷贝文件/目录到其他路径后 deploy

  • repositoryId 实际上是 server:id

  • url 可以参考本地项目 deploy 的地址

❌ 可能报错

400: 直接使用本地仓库文件

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------< org.apache.maven:standalone-pom >-------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] --------------------------------[ pom ]---------------------------------
[INFO]
[INFO] --- maven-deploy-plugin:2.7:deploy-file (default-cli) @ standalone-pom ---
Uploading to releases: http://xxx.com/yyy/zzz/releases/org/apache/hadoop/hadoop-client/2.6.0-cdh5.7.2/hadoop-client-2.6.0-cdh5.7.2.jar
Uploading to releases: http://xxx.com/yyy/zzz/releases/org/apache/hadoop/hadoop-client/2.6.0-cdh5.7.2/hadoop-client-2.6.0-cdh5.7.2.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  1.306 s
[INFO] Finished at: 2020-03-10T23:54:57+08:00
[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 org.apache.hadoop:hadoop-client:jar:2.6.0-cdh5.7.2 from/to releases (http://xxx.com/yyy/zzz/releases/): Failed to transfer file http://xxx.com/yyy/zzz/releases/org/apache/hadoop/hadoop-client/2.6.0-cdh5.7.2/hadoop-client-2.6.0-cdh5.7.2.jar with status code 400 -> [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

401: repositoryId 或 url 不正确

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------< org.apache.maven:standalone-pom >-------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] --------------------------------[ pom ]---------------------------------
[INFO]
[INFO] --- maven-deploy-plugin:2.7:deploy-file (default-cli) @ standalone-pom ---
Uploading to nexus: http://xxx.com/yyy/zzz/releases/org/apache/hadoop/hadoop-client/2.6.0-cdh5.7.2/hadoop-client-2.6.0-cdh5.7.2.jar
Uploading to nexus: http://xxx.com/yyy/zzz/releases/org/apache/hadoop/hadoop-client/2.6.0-cdh5.7.2/hadoop-client-2.6.0-cdh5.7.2.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  1.073 s
[INFO] Finished at: 2020-03-10T23:51:25+08:00
[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 org.apache.hadoop:hadoop-client:jar:2.6.0-cdh5.7.2 from/to nexus (http://xxx.com/yyy/zzz/releases/): Failed to transfer file http://xxx.com/yyy/zzz/releases/org/apache/hadoop/hadoop-client/2.6.0-cdh5.7.2/hadoop-client-2.6.0-cdh5.7.2.jar with status code 401 -> [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
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值