19

Can I have multiple repositories in Maven settings.xml with a single server credentials?

We're using Maven 3.0.4 to deploy artifacts to Nexus pro version 2.2.1

We have multiple repositories on the same server, and a user uses the same credentials to access all these repositories.

The settings.xml multiple repositories with the same credentials:

<repositories>
<repository>
   <id>Staging-group</id>
   <url>http://server/nexus/content/groups/Staging-group</url>
<repository>
   <id>RELEASES</id>
   <url>http://server/nexus/content/repositories/RELEASES</url>
</repository>
<repository>
   <id>INTERNALS</id>
   <url>http://server/nexus/content/repositories/INTERNALS</url>
</repository>
<repository>
   <id>SNAPSHOTS</id>
   <url>http://server/nexus/content/repositories/SNAPSHOTS</url>
</repository>
</repositories>
.....
<servers>
<server>
   <id>Staging-group</id>
   <username>user</username>
   <password>password</password>
</server>
<server>
   <id>RELEASES</id>
   <username>user</username>
   <password>password</password>
</server>
<server>
   <id>SNAPSHOTS</id>
   <username>user</username>
   <password>password</password>
</server>
<server>
   <id>INTERNALS</id>
   <username>user</username>
   <password>password</password>
</server>
</servers>

Nexus uses Active Directory authentication. So every time a user changes his or her Windows password, they need to change all four entries in the settings.xml file

Is there a way to declare the credentials once for all the repositories?

Thanks, Eyal

Robert
  • 39,162
  • 17
  • 99
  • 152
Eyal Azran
  • 379
  • 1
  • 3
  • 14

3 Answers3

10

Yes, at least I believe this should work.

Instead of

<repository>
   <id>SNAPSHOTS</id>
   <url>http://server/nexus/content/repositories/SNAPSHOTS</url>
</repository>

Use

<repository>
  <id>nexus</id>
  <name>SNAPSHOTS</name>
  <url>http://server/nexus/content/repositories/SNAPSHOTS</url>
</repository>

Then just reference nexus as your server id.

John Ament
  • 11,595
  • 1
  • 36
  • 45
  • 1
    looks like this is working. However, the "Validate" phase produces this warning when building any project: `[WARNING] 'profiles.profile[development].repositories.repository.id' must be unique but found duplicate repository with id nexus @ C:\Documents and Settings\user\.m2\settings.xml` – Eyal Azran Jul 16 '13 at 07:27
  • considering it's a warning, you should be fine. internally, the name is the unique entry for repositories under distribution management, mostly to separate snapshot from release artifacts. – John Ament Jul 16 '13 at 11:26
  • 4
    this is not working for me on mvn 3.5.2, if i define multiple repositories with the same id only the last repository is used by maven to resolve the dependencies, has anyone figured out how to solve this? – davide bubz Mar 08 '18 at 09:38
  • @davidebubz same thing happening here. found any solution for that? – nisanarz Jan 24 '19 at 12:56
  • I'll get back to you, I ended up using the security mechanisms plus a bash script that updates all the settings together asking just once for the new password – davide bubz Jan 24 '19 at 13:50
  • For some reasons i cant edit my previous comment, i ended up using the solution in the accepted answer of this question: https://stackoverflow.com/a/18154114/4349619 – davide bubz Jan 25 '19 at 14:15
2

Regarding the WARNING

profiles.profile[development].repositories.repository.id must be unique, ...

I found this to be two settings.xml files being found:

  • One in a maven installation's ${maven-install}/conf/settings.xml as well as
  • one in ~/.m2/settings.xml.

Decide which one you want and clean up the other.

petermeissner
  • 12,234
  • 5
  • 63
  • 63
user3366644
  • 71
  • 2
  • 3
2

Because each repository definition should have unique id - and id must be connected with server section so it is not way to have one server for many repositories.

Trying put username and password in properties also have no expected result because properties in defined in setting.xml are not resolved in the same settings.xml

But we can define environment variables with user name and password and use it in settings.xml

So we can have in settings.xml:

<server>
   <id>Staging-group</id>
   <username>${env.MVN_USER}</username>
   <password>${env.MVN_PASS}</password>
</server>
<server>
   <id>RELEASES</id>
   <username>${env.MVN_USER}</username>
   <password>${env.MVN_PASS}</password>
</server>
<server>
   <id>SNAPSHOTS</id>
   <username>${env.MVN_USER}</username>
   <password>${env.MVN_PASS}</password>
</server>

Now we must define somewhere used environment variables, we can use for it ~/.mavenrc so this variables will be available only for maven.

example of ~/.mavenrc

export MVN_USER=user
export MVN_PASS=pass

This workaround give us possibility to have defined username and password for many repositories in one place.

Testing with maven 3.6.3

Slawomir Jaranowski
  • 7,381
  • 3
  • 25
  • 33