将Java仓库推送到Maven时持续出现401未授权错误的解决咨询
问题场景
我正在将项目中的通用模型抽离到独立仓库,使用的pom.xml配置如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.mua.common</groupId> <artifactId>models</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>models</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.22</version> </dependency> </dependencies> <distributionManagement> <repository> <id>models</id> <url>https://nexus.myhost.bd/repository/maven-central/</url> </repository> <snapshotRepository> <id>models-snapshots</id> <url>https://nexus.myhost.bd/repository/maven-central/</url> </snapshotRepository> </distributionManagement> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-deploy-plugin</artifactId> <version>3.0.0-M1</version> <executions> <execution> <id>deploy-artifacts</id> <phase>deploy</phase> <goals> <goal>deploy</goal> </goals> </execution> </executions> </plugin> </plugins> </build> <repositories> <repository> <id>alt-models</id> <url>https://nexus.myhost.bd/repository/maven-central/</url> </repository> </repositories> </project>
同时在C:\Users\<username>\.m2目录下配置了settings.xml:
<settings> <servers> <server> <id>alt-models</id> <username>username</username> <password>password</password> </server> </servers> </settings>
但在IntelliJ中执行deploy操作时,始终抛出401 - UnAuthorized异常,使用的是自建Nexus仓库。
已尝试的修改
后来我更新了settings.xml,添加了对应仓库的认证信息:
<?xml version="1.0" encoding="UTF-8"?> <settings> <servers> <server> <id>models</id> <username>username</username> <password>password</password> </server> <server> <id>models-snapshot</id> <username>username</username> <password>password</password> </server> </servers> </settings>
但问题依然存在。
完整失败日志
C:\Users\User\.jdks\corretto-17.0.9\bin\java.exe -Dmaven.multiModuleProjectDirectory=K:\repos\models\models -Djansi.passthrough=true "-Dmaven.home=C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2023.2.3\plugins\maven\lib\maven3" "-Dclassworlds.conf=C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2023.2.3\plugins\maven\lib\maven3\bin\m2.conf" "-Dmaven.ext.class.path=C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2023.2.3\plugins\maven\lib\maven-event-listener.jar" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2023.2.3\lib\idea_rt.jar=51830:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2023.2.3\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2023.2.3\plugins\maven\lib\maven3\boot\plexus-classworlds-2.7.0.jar;C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2023.2.3\plugins\maven\lib\maven3\boot\plexus-classworlds.license" org.codehaus.classworlds.Launcher -Didea.version=2023.2.5 deploy [INFO] Scanning for projects... [INFO] [INFO] ---------------------< com.mua.common:models >---------------------- [INFO] Building models 1.0-SNAPSHOT [INFO] from pom.xml [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- resources:3.3.0:resources (default-resources) @ models --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory K:\repos\models\models\src\main\resources [INFO] [INFO] --- compiler:3.10.1:compile (default-compile) @ models --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- resources:3.3.0:testResources (default-testResources) @ models --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory K:\repos\models\models\src\test\resources [INFO] [INFO] --- compiler:3.10.1:testCompile (default-testCompile) @ models --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- surefire:3.0.0:test (default-test) @ models --- [INFO] Using auto detected provider org.apache.maven.surefire.junit.JUnit3Provider [INFO] [INFO] ------------------------------------------------------- [INFO] T E S T S [INFO] ------------------------------------------------------- [INFO] Running com.mua.common.AppTest [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.01 s - in com.mua.common.AppTest [INFO] [INFO] Results: [INFO] [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] [INFO] --- jar:3.3.0:jar (default-jar) @ models --- [INFO] [INFO] --- install:3.1.0:install (default-install) @ models --- [INFO] Installing K:\repos\models\models\pom.xml to C:\Users\User\.m2\repository\com\mua\common\models\1.0-SNAPSHOT\models-1.0-SNAPSHOT.pom [INFO] Installing K:\repos\models\models\target\models-1.0-SNAPSHOT.jar to C:\Users\User\.m2\repository\com\mua\common\models\1.0-SNAPSHOT\models-1.0-SNAPSHOT.jar [INFO] [INFO] --- deploy:3.0.0-M1:deploy (default-deploy) @ models --- Downloading from models-snapshots: https://nexus.myhost.bd/repository/maven-central/com/mua/common/models/1.0-SNAPSHOT/maven-metadata.xml [WARNING] Could not transfer metadata com.mua.common:models:1.0-SNAPSHOT/maven-metadata.xml from/to models-snapshots (https://nexus.myhost.bd/repository/maven-central/): status code: 401, reason phrase: Unauthorized (401) [INFO] ------------------------------------------------------------------------ [INFO] BUILD FAILURE [INFO] ------------------------------------------------------------------------ [INFO] Total time: 6.281 s [INFO] Finished at: 2024-02-12T16:27:42+06:00 [INFO] ------------------------------------------------------------------------ [WARNING] [WARNING] Plugin validation issues were detected in 3 plugin(s) [WARNING] [WARNING] * org.apache.maven.plugins:maven-compiler-plugin:3.10.1 [WARNING] * org.apache.maven.plugins:maven-deploy-plugin:3.0.0-M1 [WARNING] * org.apache.maven.plugins:maven-resources-plugin:3.3.0 [WARNING] [WARNING] For more or less details, use 'maven.plugin.validation' property with one of the values (case insensitive): [BRIEF, DEFAULT, VERBOSE] [WARNING] [ERROR] Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:3.0.0-M1:deploy (default-deploy) on project models: ArtifactDeployerException: Failed to retrieve remote metadata com.mua.common:models:1.0-SNAPSHOT/maven-metadata.xml: Could not transfer metadata com.mua.common:models:1.0-SNAPSHOT/maven-metadata.xml from/to models-snapshots (https://nexus.myhost.bd/repository/maven-central/): status code: 401, reason phrase: Unauthorized (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 Process finished with exit code 1
Maven版本
Apache Maven 3.9.2 (c9616018c7a021c1c39be70fb2843d6f5f9b8a1c)
问题排查与解决步骤
1. 修正Server ID匹配问题
从失败日志可以看到,Maven尝试访问的是models-snapshots仓库,但你修改后的settings.xml中配置的是models-snapshot(少了末尾的s)。Maven通过ID关联仓库和认证信息,ID必须完全一致。
修改settings.xml中的对应Server ID:
<?xml version="1.0" encoding="UTF-8"?> <settings> <servers> <server> <id>models</id> <username>username</username> <password>password</password> </server> <server> <id>models-snapshots</id> <!-- 这里修正为和pom.xml一致的ID --> <username>username</username> <password>password</password> </server> </servers> </settings>
2. 检查Nexus仓库权限与类型
- 仓库类型确认:如果
maven-central是Nexus的代理仓库,代理仓库只能用于拉取依赖,无法部署自己的包。需要创建一个宿主仓库(Hosted Repository),专门存放自己的快照和发布版本,然后修改pom.xml中的仓库URL为宿主仓库的地址。 - 用户权限检查:登录Nexus后台,确认你的账号对目标仓库拥有Deploy(部署)权限,而不仅仅是只读权限。
3. 验证账号密码有效性
用curl命令直接测试账号是否能访问仓库:
curl -u username:password https://nexus.myhost.bd/repository/maven-central/com/mua/common/models/1.0-SNAPSHOT/
如果返回401,说明账号密码错误或者权限不足;如果能正常返回目录内容,再回到Maven配置排查。
4. 更换稳定版Maven插件
日志中提示maven-deploy-plugin:3.0.0-M1是里程碑版本,可能存在兼容性问题。建议更换为稳定版,比如3.1.1:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-deploy-plugin</artifactId> <version>3.1.1</version> </plugin>
5. 确认IntelliJ使用的Maven配置文件
在IntelliJ的File > Settings > Build, Execution, Deployment > Build Tools > Maven中,检查User settings file是否指向你修改的C:\Users\<username>\.m2\settings.xml,避免IDE使用了其他配置文件。
内容的提问来源于stack exchange,提问作者Maifee Ul Asad

