Maven 401 Unauthorized: why authentication fails and how to fix it
Codemia Knowledge Hub ·
A "401 Unauthorized" error means Maven tried to access a repository and failed authentication. Your build is talking to a repository that requires credentials, and the ones Maven sent were incorrect, outdated or missing, so the server returned HTTP status 401.
Causes of a 401 in Maven
- Incorrect credentials: the most frequent cause. A wrong or changed username or password stops Maven from authenticating.
- Missing
settings.xmlconfiguration: Maven relies onsettings.xml, usually in the.m2directory. Missing or misconfigured credentials there cause a 401. - Inadequate permissions: sometimes the credentials are fine but the account lacks the rights to access the repository resources.
- Profile misconfiguration: if profiles in
settings.xmlare set up wrong, authentication may not happen under the intended profile. - Legacy username/password: newer repositories may have moved to token-based access, so the old username/password method fails.
Diagnosing the failure
- Verify credentials: double-check username and password. A typo may be the whole problem.
- Check
settings.xml: make sure the credentials are stored in this format:
- Enable debug logs: debug mode prints detailed output that can reveal misconfigurations:
- Log in through the web: try the same credentials in the repository's web interface to confirm whether the problem is in Maven.
- Check the repository URL: a malformed URL or outdated endpoint can also cause authentication failures.
Fixes
- Update credentials: make sure the credentials in
settings.xmlare correct and have the access rights needed. - Use encrypted passwords: Maven supports password encryption:
- Create a master password:
- Use it to encrypt the repository password:
- Fix permissions: ask the repository administrators to verify your account's access rights.
- Switch authentication method: if the repository no longer accepts username/password, switch to API tokens or certificate-based methods as its documentation advises.
To avoid repeats, keep credentials and Maven setup current, back up settings.xml and other key configuration files, and watch repository documentation for changes in authentication methods.
| Issue | Description | Resolution |
| Incorrect Credentials | Credentials stored in settings.xml do not match what's expected by the server | Verify and correct username/password |
| Missing Configurations | The settings.xml lacks necessary server entries | Add the required entries to settings.xml |
| Inadequate Permissions | User lacks permissions to access certain resources | Contact administrator to fix access rights |
| Profile Misconfiguration | Incorrect profiles in settings.xml could prevent proper credentials | Ensure correct profile settings |
| Legacy Access Methods | Repository changes from username/password to token-based | Transition to token-based authentication |
Related reading
- Why am I getting a NoClassDefFoundError in Java?
- Why am I getting Unknown error in line 1 of pom.xml?
- Why am I not getting a java.util.ConcurrentModificationException in this example?
- Why are arrays covariant but generics are invariant?
- Why am I getting an error Failed to locate or generate matching signing assets in Xcode 6?
- Why am I getting an error regarding Bolts framework and FacebookSDK when I'm not even using Bolts?
- Why are interface variables static and final by default?
- Why are Java Streams once-off?
System design interview problems on this topic
- Design an API Rate Limiter: solution and walkthrough
- Design a Distributed Messaging System: solution and walkthrough
- Design an Online Payment Service: solution and walkthrough
- Design a Key Value Store: solution and walkthrough

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.