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?