Maven构建报错:maven-dependency-plugin:3.1.1无法解析(401未授权)
先明确你遇到的错误核心:访问私有Nexus仓库时未授权,导致无法拉取依赖插件:
[ERROR] Plugin org.apache.maven.plugins:maven-dependency-plugin:3.1.1 or one of its dependencies could not be resolved: Failed to read artifact descriptor for org.apache.maven.plugins:maven-dependency-plugin:jar:3.1.1: The following artifacts could not be resolved: org.apache.maven.plugins:maven-dependency-plugin:pom:3.1.1 (absent): Could not transfer artifact org.apache.maven.plugins:maven-dependency-plugin:pom:3.1.1 from/to nexus-thirdparty (https://nexus.audatex.com/nexus/content/repositories/thirdparty): status code: 401, reason phrase: Unauthorized (401) -> [Help 1]
以下是针对性的解决建议:
校验Nexus仓库的认证配置
打开Maven的settings.xml(路径一般是~/.m2/settings.xml,或Maven安装目录下的conf/settings.xml),检查对应nexus-thirdparty仓库的服务器认证信息是否正确:<servers> <server> <id>nexus-thirdparty</id> <username>你的Nexus账号</username> <password>你的Nexus密码</password> </server> </servers>关键注意:
<server>的id必须和你pom.xml或settings.xml中配置的仓库id完全一致,否则认证不会生效。确认仓库访问权限
联系公司的Nexus管理员,确认你的账号是否拥有nexus-thirdparty仓库的读取权限。部分私有仓库会限制特定用户或角色才能访问,权限不足会直接返回401。更换插件版本或调整仓库源
如果暂时无法获取私有仓库的权限,可以尝试两种方案:- 在项目
pom.xml中升级maven-dependency-plugin版本,比如3.6.0(该版本在Maven中央仓库可直接获取):<build> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>3.6.0</version> </plugin> </plugins> </pluginManagement> </build> - 在
settings.xml中添加Maven中央仓库作为备选,确保当私有仓库无法拉取时能从公共仓库获取依赖:<repositories> <repository> <id>central</id> <url>https://repo1.maven.org/maven2</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </repository> <!-- 保留原有nexus-thirdparty仓库配置 --> </repositories>
- 在项目
排查Maven镜像配置问题
如果settings.xml中有<mirrors>配置,检查镜像的mirrorOf属性是否包含nexus-thirdparty。如果镜像覆盖了该仓库,需要确保镜像对应的<server>认证信息也正确配置,否则会导致原仓库的认证失效。
内容的提问来源于stack exchange,提问作者Prasu

