Why am I getting a “401 Unauthorized” error in Maven?

前端 未结 21 1643
栀梦
栀梦 2020-11-29 18:06

Why am I getting a \"401 Unauthorized\" error in Maven?

Here\'s the error I\'m getting when calling mvn deploy (full logs at the bottom):

相关标签:
21条回答
  • 2020-11-29 18:20

    Also had 401's from Nexus. Having tried all the suggestions above and more without success I eventually found that it was a Jenkins setting that was in error.

    In the Jenkins configuration for the failing project, we have a section in the 'Post Build' actions entitled 'Deploy Artifacts To Maven Repository'. This has a 'Repository ID' field which was set to the wrong value. It has to be the same as the repository ID in settings.xml for Jenkins to read the user and password fields:

     <servers>
        <server>
          <id>snapshot-repository</id>  <!-- must match this -->
          <username>deployment</username>
          <password>password</password>
        </server>
      </servers>
    
    0 讨论(0)
  • 2020-11-29 18:21

    Also, after you've updated your repository ids, make sure you run clean as release:prepare will pick up where it left off. So you can do:

    mvn release:prepare -Dresume=false or

    mvn release:clean release:prepare

    0 讨论(0)
  • 2020-11-29 18:23

    I was dealing with this running Artifactory version 5.8.4. The "Set Me Up" function would generate settings.xml as follows:

    <servers>
        <server>
          <username>${security.getCurrentUsername()}</username>
          <password>${security.getEscapedEncryptedPassword()!"AP56eMPz8L12T5u4J6rWdqWqyhQ"}</password>
          <id>central</id>
        </server>
        <server>
          <username>${security.getCurrentUsername()}</username>
          <password>${security.getEscapedEncryptedPassword()!"AP56eMPz8L12T5u4J6rWdqWqyhQ"}</password>
          <id>snapshots</id>
        </server>
    </servers>
    

    After using the mvn deploy -e -X switch, I noticed the credentials were not accurate. I removed the ${security.getCurrentUsername()} and replaced it with my username and removed ${security.getEscapedEncryptedPassword()!""} and just put my encrypted password which worked for me:

    <servers>
        <server>
          <username>username</username>
          <password>AP56eMPz8L12T5u4J6rWdqWqyhQ</password>
          <id>central</id>
        </server>
        <server>
          <username>username</username>
          <password>AP56eMPz8L12T5u4J6rWdqWqyhQ</password>
          <id>snapshots</id>
        </server>
    </servers>
    

    Hope this helps!

    0 讨论(0)
  • 2020-11-29 18:23

    In my case I removed the server logon credentials for central from my setting.

        <server> 
            <id>central</id>
            <username>admin</username> 
            <password>******</password> 
        </server>
    
       <mirror>
            <id>central</id>
            <mirrorOf>central</mirrorOf>
            <name>maven-central</name>
            <url>http://www.localhost:8081/repository/maven-central/</url>
       </mirror> 
    

    I don't know why I did that, but its completely wrong since the central maven repo can be accessed anonymously. See my debug output that led to my error identification and resolution.

    [DEBUG] Using connector BasicRepositoryConnector with priority 0.0 for http://www.localhost:8081/repository/maven-central/ with username=admin, password=***
    
    0 讨论(0)
  • 2020-11-29 18:23

    I got the same error when trying to deploy to a Artifactory repository, the following solved the issue for me:

    Go to the repository setting in artifactory and enable the point "Force Maven Authentication" and the 401 "Unauthorized" error should be gone. (Of course you need to supply your credentials in the settings.xml file at best in plain text to prevent issues)

    I guess by default, even through you supply the right credentials in the settings.xml file, they don't get used and you get the Unauthorized exception.

    0 讨论(0)
  • 2020-11-29 18:24

    I got 401 error when used mvn gpg:sign-and-deploy-file command and the reason was that <MVN_HOME>/conf/settings.xml does not include <server> tag that you can get via https://oss.sonatype.org/#profile;User%20Token where <id> is the same as -DrepositoryId

    To get <MVN_HOME> run mvn --version

    0 讨论(0)
提交回复
热议问题