Maven
401 Unauthorized
error handling
troubleshooting
software development

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

  1. Incorrect credentials: the most frequent cause. A wrong or changed username or password stops Maven from authenticating.
  2. Missing settings.xml configuration: Maven relies on settings.xml, usually in the .m2 directory. Missing or misconfigured credentials there cause a 401.
  3. Inadequate permissions: sometimes the credentials are fine but the account lacks the rights to access the repository resources.
  4. Profile misconfiguration: if profiles in settings.xml are set up wrong, authentication may not happen under the intended profile.
  5. Legacy username/password: newer repositories may have moved to token-based access, so the old username/password method fails.

Diagnosing the failure

  1. Verify credentials: double-check username and password. A typo may be the whole problem.
  2. Check settings.xml: make sure the credentials are stored in this format:
xml
1    <settings>
2      <servers>
3        <server>
4          <id>your-repository-id</id>
5          <username>your-username</username>
6          <password>your-password</password>
7        </server>
8      </servers>
9    </settings>
  1. Enable debug logs: debug mode prints detailed output that can reveal misconfigurations:
bash
    mvn -X clean install
  1. Log in through the web: try the same credentials in the repository's web interface to confirm whether the problem is in Maven.
  2. Check the repository URL: a malformed URL or outdated endpoint can also cause authentication failures.

Fixes

  • Update credentials: make sure the credentials in settings.xml are correct and have the access rights needed.
  • Use encrypted passwords: Maven supports password encryption:
    • Create a master password:
bash
      mvn --encrypt-master-password
  • Use it to encrypt the repository password:
bash
      mvn --encrypt-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.

IssueDescriptionResolution
Incorrect CredentialsCredentials stored in settings.xml do not match what's expected by the serverVerify and correct username/password
Missing ConfigurationsThe settings.xml lacks necessary server entriesAdd the required entries to settings.xml
Inadequate PermissionsUser lacks permissions to access certain resourcesContact administrator to fix access rights
Profile MisconfigurationIncorrect profiles in settings.xml could prevent proper credentialsEnsure correct profile settings
Legacy Access MethodsRepository changes from username/password to token-basedTransition to token-based authentication

Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Data 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.

Practice algorithms