https://gradle.com/ logo
#community-support
Title
# community-support
v

Vinay Nagaraj

05/24/2023, 1:23 PM
Hey, my company publishes some custom plugins and dependencies to an internally hosted artifactory instance. I'm trying to declare this repository in my :buildSrc's settings.gradle and my top level settings.gradle, but I want to avoid duplicating this declaration. I've added a third gradle module that just declares an extension function:
Copy code
fun RepositoryHandler.customRepo(provider: ProviderFactory) {
        maven("<https://release-url>") {
            mavenContent { releasesOnly() }
            artifactoryCredentials(provider)
        }
        maven("<https://snapshot-url>") {
            mavenContent { snapshotsOnly() }
            artifactoryCredentials(provider)
        }
    }

    fun AuthenticationSupported.artifactoryCredentials(provider: ProviderFactory) {
        val userName = provider.propOrEnv("artifactory_username").get()
        val apiKey = provider.propOrEnv("artifactory_api_key").get()
        credentials {
            username = userName
            password = apiKey
        }
    }

    fun ProviderFactory.propOrEnv(key: String): Provider<String> {
        return gradleProperty(key).orElse(environmentVariable(key))
    }
What's the best way to make this function available in both (buildSrc & root) settings.gradle?
m

Martin

05/24/2023, 1:30 PM
For the repo side of this, I've found using
apply(from = repositories.gradle.kts)
is the easiest way to do this without going circular dependencies all the way down. One example here
v

Vinay Nagaraj

05/24/2023, 1:33 PM
Would that break configuration caching?
m

Martin

05/24/2023, 1:33 PM
Maybe?
I have no idea, we've not enabled that yet
v

Vampire

05/24/2023, 1:34 PM
Is that for
pluginManagement { repositories { ... } }
or
dependendencyManagement { repositories { ... } }
?
Because for the former you will have a hard time as it is super-special-early-separate-evaluation
m

Martin

05/24/2023, 1:35 PM
@Vampire we have a very long thread somewhere about this but I think it works with
pluginManagement
too in KTS (but maybe not in Groovy)
v

Vinay Nagaraj

05/24/2023, 1:37 PM
Want to configure some internal plugins and make them available as precompiled gradle scripts so will be declaring it as a dependency in my buildSrc using
dependendencyManagement
(assuming that will make these plugins available to all subprojects), and also as a repository in
dependencyManagement
for all my subprojects.
v

Vampire

05/24/2023, 1:40 PM
Then instead of a script plugin as Martin suggested if you don't want that or if it is not CC compatible, you should be able to add your new project that is just for that extension function either to the buildscript classpath of the settings plugin explicitly, or by having a dummy settings plugin that you apply to the settings script to get the classes with the extension on the buildscript classpath. The settings plugin does not need to do actions (or it could actually add those repositories already) and you will
includeBuild
that build within
pluginManagement { ... }
.
v

Vinay Nagaraj

05/24/2023, 1:52 PM
Hmm, i'm trying this but gradle complains that I can't add
project
dependencies to the buildscript classpath.
I guess i'm left with the dummy plugin method. Will try that thanks.
v

Vampire

05/24/2023, 1:55 PM
Not a project dependency. You cannot use something you build in the build that builds it. It should be in a separate included build via composite build.
v

Vinay Nagaraj

05/24/2023, 2:00 PM
Ah, if i includeBuild in
pluginManagement
, would it automatically get added to buildscript classpath?
Even after included via composite build, how would a dependency be added to the extension function?
v

Vampire

05/24/2023, 2:01 PM
No, you still need to add it by coordinates or dummy plugin
Maybe even just the dummy plugin works, did not try yet whether the other way works in that case
19 Views