2

Please, help me to find the best solution for my problem:

In private LAN-network I have nexus repositories, and i need to use one user for getting access to repos.

The simplest solution is to add user and password to gradle.properties file and in every build.gradle file and every repo write the same code:

maven {
        credentials {
            username "$mavenUser"
            password "$mavenPassword"
        }
        url 'https://maven.yourcorp1.net/'
   }

maven {
        credentials {
            username "$mavenUser"
            password "$mavenPassword"
        }
        url 'https://maven.yourcorp2.net/'
   }

Where mavenUser and mavenPassword in gradle.properties are the same for all repositories:

mavenUser=admin
mavenPassword=admin123

It's all good and works fine, but there is a lot of duplicate code.

Maybe you know - how in one place set same credentials for all maven repositories? I don't want to copy the same code for all repositories.

Thanks you a lot!

aarexer
  • 523
  • 6
  • 20

2 Answers2

5

You can try finding the common part of all your repositories URL and use something like that :

['yourcorp1', 'yourcorp2'].each {
    maven {
         credentials {
             username "$mavenUser"
             password "$mavenPassword" 
        }
        url "https://maven.${it}.net/"
    } 
}

If they are many Gradle files, put it in a repositories.gradle file and use the include directive.

ToYonos
  • 16,469
  • 2
  • 54
  • 70
1

one can add Maven server settings into $HOME/.m2/settings.xml

<settings>
   <servers>
    <server>
        <id>yourcorp1</id>
        <username>username</username>
        <password>password</password>
    </server>
    <server>
        <id>yourcorp2</id>
        <username>username</username>
        <password>password</password>
    </server>
    ...
    </servers>
</settings>

while Gradle requires plugins maven-publish and maven-publish-auth:

apply plugin: 'maven-publish'
apply plugin: 'maven-publish-auth'

and one has to assign unique names to the repositories:

maven {
    name: 'yourcorp1'
    url 'https://maven.yourcorp1.net/'
}

maven {
    name: 'yourcorp2'
    url 'https://maven.yourcorp2.net/'
}

alike this, there is little chance to check in confidential information ...

Martin Zeitler
  • 1
  • 19
  • 155
  • 216