What This Error Means
The repository requires authentication, but Maven did not send valid credentials for the repository <id> being used.
How to Fix It
- Identify which repository URL is returning 401 (use
mvn -Xif needed). - Add or fix the matching
<server>entry in~/.m2/settings.xml(same<id>as the repository<id>), using the correct username/password or token. - If you use encrypted passwords, ensure
~/.m2/settings-security.xmlis present and correct on the machine running Maven. - If you're in CI, provide a dedicated settings file and pass it explicitly:
mvn -s /path/to/settings.xml .... - Retry the build. If it still fails, confirm the credentials work by authenticating to the repository using the same account/token outside Maven.
Why It Happens
- Missing or incorrect credentials in
~/.m2/settings.xml. - Repository
<id>mismatch betweenpom.xmlandsettings.xml<servers>. - Using the wrong
settings.xml(especially in CI) or not passing-swhen expected. - Credentials are present but expired/rotated (tokens) or require updated scopes.
How to Verify
- Re-run the original Maven goal and confirm artifact downloads succeed without 401 errors.
- Confirm the previously failing artifact now exists in
~/.m2/repository/.
Manual authentication checks
- Print effective settings and confirm the
<server>entry exists:mvn -q help:effective-settings. - Confirm the repository
<id>in yourpom.xml(or parent POM) matches the<server><id>insettings.xmlexactly. - If your CI uses a custom settings file, confirm it's actually being used (for example:
mvn -s /path/to/settings.xml -q help:effective-settings). - Re-run with debug logs to confirm which repository URL is returning 401:
mvn -X -DskipTests package.
Common CLI Output
[ERROR] Failed to transfer file: https://repo.example.com/repository/maven-releases/... Return code is: 401, ReasonPhrase: Unauthorized.[ERROR] Failed to transfer artifact com.example:private-lib:jar:1.0.0 from/to internal (https://repo.example.com/maven): Return code is: 401, ReasonPhrase: Unauthorized. How Maven uses repository credentials
- Maven reads credentials from
~/.m2/settings.xmlunder<servers>. - A
<server><id>...</id></server>entry must match the repository<id>used by your build (repositories/pluginRepositories/distributionManagement). - If the ids don't match (or the settings file isn't being used), Maven sends no credentials and the server responds with 401.
Prevention Tips
- Use dedicated read-only tokens for CI and rotate them on a schedule.
- Standardize repository ids across builds so
<servers>mapping is consistent. - Prefer a single repository manager endpoint so credentials are managed in one place.
Where This Can Be Triggered
github.com/apache/maven/blob/maven-3.9.6/maven-core/src/main/java/org/apache/maven/project/DefaultProjectDependenciesResolver.java
HTTP status failures (401/403/407) are surfaced to Maven as a transfer failure message from the underlying resolver/wagon layer (e.getMessage()). - GitHub
String msg = "Could not resolve dependencies for project " + project.getId() + ": " + e.getMessage();
DependencyResolutionException dex = new DependencyResolutionException(msg, e);
dex.setResult(e.getResult());
throw dex;