gradle-maven-settings-plugin icon indicating copy to clipboard operation
gradle-maven-settings-plugin copied to clipboard

Apply maven settings also to buildscript repositories

Open zline opened this issue 8 years ago • 4 comments

It would be great to have ability to apply repository credentials (stored in settings.xml in encrypted form) also to repositories specified in buildscript block.

As far as I understand this could be done by means of gradle init scripts, for which Plugin<Gradle> could be written.

zline avatar Dec 14 '16 10:12 zline

I have been trying to write an initscript to do this and am not having any luck.

Here is the init.gradle:

initscript {
    repositories {
        maven {
            url 'https://plugins.gradle.org/m2/'
        }
    }
    dependencies {
        classpath 'net.linguica.gradle:maven-settings-plugin:0.5'
    }
}

allprojects {
    apply plugin: net.linguica.gradle.maven.settings.MavenSettingsPlugin
}

And here is the buildscript block of my build.gradle:

buildscript {
    repositories {
        maven {
            name = "central"
            url "<url_of_repo_maven_repo>"
        }
    }
    dependencies {
        classpath "<classpath_of_dependency>"
    }
}

The credentials are not being applied to the repository that I've specified in the buildscript block. Any help would be appreciated.

tsanborn19-zz avatar Sep 20 '19 20:09 tsanborn19-zz

If it's only for credentials you can do like this,

init.gradle:

import net.linguica.gradle.maven.settings.LocalMavenSettingsLoader
import net.linguica.gradle.maven.settings.MavenSettingsPlugin
import net.linguica.gradle.maven.settings.MavenSettingsPluginExtension

initscript {
    repositories {
        maven {
            url 'https://plugins.gradle.org/m2/'
        }
    }
    dependencies {
        classpath 'net.linguica.gradle:maven-settings-plugin:0.5'
    }
}

allprojects {
    apply plugin: MavenSettingsPlugin

    def settings = new LocalMavenSettingsLoader(new MavenSettingsPluginExtension(project)).loadSettings()

    project.apply {
        buildscript.repositories.all { repo ->
            if (repo instanceof MavenArtifactRepository) {
                def server = settings.servers.find { it.id == repo.name }
                repo.credentials.username = server?.username
                repo.credentials.password = server?.password
            }
        }
    }
}

renjfk avatar Mar 26 '21 18:03 renjfk

Is it also possible to apply credentials for pluginManagement in init.gradle in a similar fashion?

i.e. replace hard-wiring of credentials in: -

settingsEvaluated { settings ->
    settings.pluginManagement {
        repositories {
            gradlePluginPortal()
            maven {
                name 'nexus'
                url "..."
                credentials {
                    username '*****'
                    password '*****'
                }
            }
        }
    }
}

albp-rmt avatar Sep 09 '21 05:09 albp-rmt

Never tried it myself but it should be possible. Just try debugging different variations by using command line option on your IDE.

renjfk avatar Sep 09 '21 06:09 renjfk