Sonatype Nexus: How to set a single server credentials for multiple repositories in maven's settings.xml?

18,668

Solution 1

Just use one entry in setttings.xml like that

<server>   
    <id>nexus</id>   
    <username>deployment</username>   
    <password>deployment123</password>      
</server>  

and then in distributionManagement in your pom.xml's you use something like that

<distributionManagement>
<repository>
  <id>nexus</id>
  <name>Nexus Releases</name>
  <url>http://localhost:8081/nexus/content/repositories/releases</url>
</repository>
<snapshotRepository>
  <id>nexus</id>
  <name>Nexus Snapshot</name>
  <url>http://localhost:8081/nexus/content/repositories/snapshots</url>
</snapshotRepository>
</distributionManagement>

For fully working setup with this look at the Nexus Book Examples project that are used in the trial guide. You can add a site with the same id as well, of course. Keep in mind that there is no problem if the id;s are the same as they just detail the identifier of the server element in settings to look for and are NOT an id element for the repository. Imho it should be called serverId or something to be clearer, but thats a different story.

Solution 2

Not a solution but a workaround:

settings.xml will handle system properties and environment variables. So if you're not fussed about putting your server authentication details in a script or in your environment, you can stick with three server credentials but eliminate the need to update all three of them in favour of updating your script or environments (I've put examples for both options in this snippet):

<servers>
  <server>
    <id>releases</id>
    <username>${env.NEXUS_USERNAME}</username> <!-- Env var -->
    <password>${nexus.password}</password> <!-- System (-D) var -->
  </server>
  <server>
    <id>snapshots</id>
    <username>${env.NEXUS_USERNAME}</username> <!-- Env var -->
    <password>${nexus.password}</password> <!-- System (-D) var -->
  </server>
  <server>
    <id>site</id>
    <username>${env.NEXUS_USERNAME}</username> <!-- Env var -->
    <password>${nexus.password}</password> <!-- System (-D) var -->
  </server>
</servers>

Unfortunately there's no <properties> element supported in settings.xml!

Aside: maven already handles the snapshots and releases repositories within nexus, and that is the better way to do things. Your posted settings.xml even enables them already. Why do you need separate repository entries for snapshots and releases?

Solution 3

It looks like you mistaken things here. The given credentials and id's are for the distributionManagement and not for the access to the Nexus in this case. Apart from that you need three different username, password combinations cause you have three possible things releases, snapshots and site. So not a big deal.

Aprt from that i would suggest to upgrade Maven into Maven 3.X line cause Maven 2.2.1 is a little bit out of date.

Share:
18,668
Dinesh
Author by

Dinesh

Software Engineer

Updated on July 23, 2022

Comments

  • Dinesh
    Dinesh almost 2 years

    We have multiple repositories in Nexus (i.e., releases, snapshot and site). All 3 repos are under public group and users uses the same credentials to access all these repositories. Providing the same username and password in settings.xml for each repository makes it redundant and hard to maintain them.

    Could you please suggest an elegant way to describe one server credential for all the 3 repositories?

    Any help is greatly appreciated.

    We are using maven 2.2.1 and Nexus OSS 2.7.1

    Here is my settings.xml

    <settings>    
    <servers>   
    <server>   
        <id>snapshot</id>   
        <username>deployment</username>   
        <password>deployment123</password>      
    </server>  
    <server>   
           <id>release</id>   
           <username>deployment</username>  
           <password>deployment123</password>      
    </server>  
    <server>  
          <id>site</id>  
          <username>deployment</username>  
          <password>deployment123</password>   
    </server>  
     </servers>  
      <mirrors>  
        <mirror>  
          <!--This sends everything else to /public -->  
          <id>nexus</id>  
          <mirrorOf>*</mirrorOf>  
          <url>http://localhost:8081/nexus/content/groups/public</url>  
        </mirror>  
      </mirrors>  
      <profiles>  
        <profile>  
          <id>nexus</id>  
          <!--Enable snapshots for the built in central repo to direct -->  
          <!--all requests to nexus via the mirror -->  
          <repositories>  
            <repository>  
              <id>central</id>  
              <url>http://central</url>  
              <releases><enabled>true</enabled></releases>  
              <snapshots><enabled>true</enabled></snapshots>  
            </repository>  
          </repositories>  
          <pluginRepositories>  
            <pluginRepository>  
              <id>central</id>  
              <url>http://central</url>  
              <releases><enabled>true</enabled></releases>  
              <snapshots><enabled>true</enabled></snapshots>  
            </pluginRepository>  
           </pluginRepositories>  
     </profile>  
      </profiles>  
      <activeProfiles>  
        <!--make the profile active all the time -->  
        <activeProfile>nexus</activeProfile>  
      </activeProfiles>  
    </settings>