Java-Maven-解决maven deploy时报 401 Reason Phrase Unauthorized 错误
环境
- Java JDK 1.8
- Maven 3.3.9
引言
项目需要打成jar包上传到私服,供其它项目引用。此时需要执行 mvn clean deploy 命令,执行过程中报 401 错误。
解决401错误
报错信息
执行命令:mvn clean deploy -DskipTests
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.8.2:deploy (default-deploy) on project xxxxx: Failed to deploy artifacts: Could not transfer artifact com.xxxxx:xxxxx:jar:spring-1.0.10-20210812.035258-1 from/to nexus-snapshots (http://xxxx.com/nexus/content/repositories/snapshots): Failed to transfer file: http://xxxx.com/nexus/content/repositories/snapshots/com/.../spring-1.0.10-SNAPSHOT/xxxxx-spring-1.0.10-20210812.035258-1.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/MojoExecutionExceptionPS D:\...\xxxxx> mvn clean deploy -DskipTests
关键错误信息
- 关键错误信息1: Failed to deploy artifacts
- 关键错误信息2: Could not transfer artifact com.*:*:pom:* from/to snapshots (http://dev-mvn.***.com/content/repositories/snapshots/)
- 关键错误信息3: Return code is: 401, ReasonPhrase: Unauthorized. -> [Help 1]
- 提取关键错误信息:Could not transfer …… from/to snapshots …… Return code is: 401, ReasonPhrase: Unauthorized.
- 很明显了,是由于权限的问题导致服务器拒绝了本次推送。
解决方案
- 检查开发环境使用的maven配置文件
- 我用的是idea
- maven配置位置:File/Settings; Build,Execution,Deployment/Build Tools/Maven;
- User settings file: D:/java/maven/apache-maven-3.3.9\conf\settings.xml
- 修改settings.xml ,加入如下配置:
nexus-snapshots deployment 123456 ...
- 再次执行 mvn clean deploy -DskipTests 命令,显示上传成功!
[INFO] --- maven-deploy-plugin:2.8.2:deploy (default-deploy) @ mytest ---Downloading: http://dev.xxx.com/nexus/content/repositories/snapshots/com/xxxx/spring-1.0.10-SNAPSHOT/maven-metadata.xmlUploading: http://dev.xxx.com/nexus/content/repositories/snapshots/com/xxxx/spring-1.0.10-SNAPSHOT/mytest-spring-1.0.10-20210812.035231-1.jarUploaded: http://dev.xxx.com/nexus/content/repositories/snapshots/com/xxxx/spring-1.0.10-SNAPSHOT/mytest-spring-1.0.10-20210812.035231-1.jar (127643 KB at 13522.8 KB/sec)Uploading: http://dev.xxx.com/nexus/content/repositories/snapshots/com/xxxx/spring-1.0.10-SNAPSHOT/mytest-spring-1.0.10-20210812.035231-1.pomUploaded: http://dev.xxx.com/nexus/content/repositories/snapshots/com/xxxx/spring-1.0.10-SNAPSHOT/mytest-spring-1.0.10-20210812.035231-1.pom (7 KB at 53.9 KB/sec)Downloading: http://dev.xxx.com/nexus/content/repositories/snapshots/com/xxxx/maven-metadata.xmlUploading: http://dev.xxx.com/nexus/content/repositories/snapshots/com/xxxx/spring-1.0.10-SNAPSHOT/maven-metadata.xmlUploaded: http://dev.xxx.com/nexus/content/repositories/snapshots/com/xxxx/spring-1.0.10-SNAPSHOT/maven-metadata.xml (803 B at 8.2 KB/sec)Uploading: http://dev.xxx.com/nexus/content/repositories/snapshots/com/xxxx/maven-metadata.xmlUploaded: http://dev.xxx.com/nexus/content/repositories/snapshots/com/xxxx/maven-metadata.xml (297 B at 2.5 KB/sec)Uploading: http://dev.xxx.com/nexus/content/repositories/snapshots/com/xxxx/spring-1.0.10-SNAPSHOT/mytest-spring-1.0.10-20210812.035231-1-sdk.jarUploaded: http://dev.xxx.com/nexus/content/repositories/snapshots/com/xxxx/spring-1.0.10-SNAPSHOT/mytest-spring-1.0.10-20210812.035231-1-sdk.jar (602 KB at 3039.0 KB/sec)Uploading: http://dev.xxx.com/nexus/content/repositories/snapshots/com/xxxx/spring-1.0.10-SNAPSHOT/maven-metadata.xmlUploaded: http://dev.xxx.com/nexus/content/repositories/snapshots/com/xxxx/spring-1.0.10-SNAPSHOT/maven-metadata.xml (1021 B at 8.4 KB/sec)[INFO] ------------------------------------------------------------------------[INFO] BUILD SUCCESS[INFO] ------------------------------------------------------------------------[INFO] Total time: 37.694 s[INFO] Finished at: 2023-08-22T11:56:51+08:00[INFO] Final Memory: 73M/974M[INFO] ------------------------------------------------------------------------
mvn package、install、deploy命令的区别
mvn package 命令:在项目的target目录下生成java包
当我们使用package来打包项目的时候,就会在我们的项目目录下生成一个target文件夹。
如果是web项目比如MVC项目就会在该文件夹下生成一个war包,放在服务器中的tomcat/webapps目录里即可运行。
如果是Java项目的话就会在该文件夹下生成一个jar包,放在服务器中指定目录下使用"java -jar jar包名"命令运行。
一般我们服务器部署服务都是使用package方式打包项目。
mvn install 命令: 将java包安装到本地maven仓
使用install方式就是将项目转换成为maven依赖存放在本地maven仓库中。
就比如我的spring boot项目执行mvn install 命令后,将把jar存在我本地maven仓库中。
比如我自己配置的maven路径是:D:\java\maven\repository… 。
一般是多个项目中,例如a项目中使用了b项目的一些方法需要添加b项目作为依赖,此时就需要将b项目使用install方式打包到本地[maven]仓库中,以此来给a项目依赖导入调用。
mvn deploy 命令:将java包发布到中央仓(注:需要在maven中配置有权限的推送账户名+密码)
deploy与install的主要区别就是install是将项目jar包放在本地仓库,而deploy是放在私服仓库。
在本地的pom文件配置好私服仓库的信息之后,执行deploy命令,可以将maven所打的jar包上传到远程的repository,便于其他开发者和工程共享。
执行maven deploy命令成功之后,登录私服进行查询,即可看到对应的jar包。
mvn 跳过测试代码
直接运行上面的命令,默认会执行单元测试代码,跳过单元测试使用如下命令:
- mvn -DskipTests
- mvn -Dmaven.test.skip=true
区别
- mvn clean package -DskipTests
- 不执行测试用例,但编译测试用例类,将在target/test-classes目录生成相应的class文件。
- 使用 mvn package -DskipTests 跳过单元测试,但是会继续编译。
- mvn clean package -Dmaven.test.skip=true
- 不执行测试用例,也不编译测试用例类;
- 使用maven.test.skip不但跳过单元测试的运行,也跳过测试代码的编译;
来源地址:https://blog.csdn.net/xxj_jing/article/details/132430681