Adding pluginManagement repositories programmatically? (Kotlin)

We are behind a firewall and our company provides an Artifactory that acts as a mirror for mavenCentral() and - as I have found out recently - it also mirrors gradlePluginPortal().
The firewall blocks access to mavenCentral and the gradlePluginPortal, our builds are supposed to work exclusively with our corporate Artifactory.

I wrote a plugin that adds the corporate Artifactory, essentially:

    private fun Project.declareMavenRepository(configuration: Configuration) {
        project.repositories.maven { repo ->
            repo.name = configuration.value(CiPluginExtension::mavenRepositoryName)
            repo.url = uri(configuration.value(CiPluginExtension::mavenRepositoryUrl))
            repo.credentials { credentials ->
                credentials.username = configuration.value(CiPluginExtension::mavenRepositoryUsername)
                credentials.password = configuration.value(CiPluginExtension::mavenRepositoryPassword)
            }
        }
    }

This works for dependencies that would normally be downloaded from mavenCentral.
But Gradle cannot download the community plugins from the portal.
Adding this to settings.gradle.kts solves the problem:

pluginManagement {
    repositories {
        maven {
            name = "corporateRepository"
            url = uri("https://nwb-maven-local.bin.acme.com/artifactory/nwb-maven-virtual/")
            credentials {
                username = extra["mavenUser"].toString()
                password = extra["mavenPassword"].toString()
            }
        }
        gradlePluginPortal()
    }
}

But we don’t want to add this in every project. Instead I would like to do this programmatically in Kotlin in the plugin I already wrote.
I found a similar topic, but I don’t see how the last suggestion applies: “You might as well just mutate project.gradle.settings at that point.”
I am working with Gradle 7.5.1 and I cannot get at “project.gradle.settings” in my Kotlin code.

It seems that I would have to write a second plugin that implements Plugin<Settings>. But that is somewhat unpractical. I would prefer to add our corporate Artifactory as a source for Gradle plugins in the existing Plugin<Project> code.
Is there a way to do this?

This doesn’t really make any sense. If you want to get dependencies from your corporate repository only, you can write a plugin, as you have. You have to get that plugin though. You can’t have a plugin in your corporate repository to consolidate the logic necessary to get a plugin from your corporate repository. Each project needs the logic to be able to get the initial dependency. There are ways you could have less code or simplify it if you can have a snippet on your corporate repository that is accessible without the credentials, but otherwise, chicken and egg problem.

1 Like

Thank you, good point. . :+1:
Seen in the light of a new day it is quite obvious. :grin:

I have the same requirement in a project and we actually do is providing a locally hosted gradle distribution (also in the corporate artifactory) that takes care of the initial setup with a init.d script. That includes delegation of the pluginPortal. That way you’ll have to prebuild packages for every gradle release you want to support with the small init script and ensure that this is used by the projects gradle wrapper.
But other than that no copy & pasting of those configuration jobs are required.

2 Likes