Quantcast
Channel: Gradle Forums - Latest posts
Viewing all articles
Browse latest Browse all 19859

Externilization of Maven repo credentials

$
0
0

You can define repos in an init.gradle script (equivalent of settings.xml for Maven)

allprojects {
  repositories {
    maven {
      url "${url}"
      credentials {
        username = 'joe'
        password = 'secret'
    }
  }
}

Similarly, you can also programmatically configure that through a plugin, which sounds like what you’re trying to do.

https://docs.gradle.org/current/userguide/init_scripts.html#sec:init_script_plugins

apply plugin: EnterpriseRepositoryPlugin

class EnterpriseRepositoryPlugin implements Plugin<Gradle> {

    private static String ENTERPRISE_REPOSITORY_URL = "https://repo.gradle.org/gradle/repo"

    void apply(Gradle gradle) {
        // ONLY USE ENTERPRISE REPO FOR DEPENDENCIES
        gradle.allprojects { project ->
            project.repositories {

                // Remove all repositories not pointing to the enterprise repository url
                all { ArtifactRepository repo ->
                    if (!(repo instanceof MavenArtifactRepository) ||
                          repo.url.toString() != ENTERPRISE_REPOSITORY_URL) {
                        project.logger.lifecycle "Repository ${repo.url} removed. Only $ENTERPRISE_REPOSITORY_URL is allowed"
                        remove repo
                    }
                }

                // add the enterprise repository
                maven {
                    name "STANDARD_ENTERPRISE_REPO"
                    url ENTERPRISE_REPOSITORY_URL
                }
            }
        }
    }
}

Viewing all articles
Browse latest Browse all 19859

Trending Articles