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

Dinesh picture Dinesh · Feb 17, 2014 · Viewed 16.1k times · Source

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>  

Answer

Manfred Moser picture Manfred Moser · Feb 18, 2014

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.