https://kotlinlang.org logo
#gradle
Title
# gradle
a

arekolek

02/27/2024, 11:09 AM
any way to re-use an extension function like
RepositoryHandler.myProxy()
in
settings.gradle.kts
?
Copy code
fun RepositoryHandler.myProxy(repository: String, fallbackUrl: String? = null, vararg includeGroups: String) {
    maven("<https://example.com/$repository/>") {
        content { includeGroups.forEach(::includeGroup) }
        credentials {
            username = providers.gradleProperty("MY_PROXY_USERNAME").get()
            password = providers.gradleProperty("MY_PROXY_PASSWORD").get()
        }
    }
    if (fallbackUrl == null) return
    maven(fallbackUrl) {
        content { includeGroups.forEach(::includeGroup) }
    }
}
I would like to use it like:
Copy code
pluginManagement {
    repositories {
        gradlePluginPortal()
        google()
        myProxy("maven-central")
        mavenCentral()
    }
}

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        myProxy("jitpack-io", "<https://jitpack.io>", "com.example")
        google()
        myProxy("maven-central")
        mavenCentral()
    }
}
but I would have to repeat the definition in both
pluginManagement
and
dependencyResolutionManagement
is there any way to re-use the same definition in both? I don't see why that should be impossible (since it works if I just duplicate it)
s

Sam

02/27/2024, 11:15 AM
Did you try it? The code you shared works fine for me.
Hm... it looked like it worked in the IDE, but Gradle doesn't seem so happy.
v

Vampire

02/27/2024, 11:20 AM
pluginManagement { ... }
is like
buildscript { ... }
and
plugins { ... }
. It is extracted and evaluated separately first, as you could for example declare repositories where to get settings plugins from or define an included build that contributes settings plugins. So you cannot use anything from outside the
pluginManagement { ... }
block inside the block even though the IDE is happy.
👍 1
What you can do, is to configure some other parts of the model from within the
pluginManagement { ... }
block though. So this works:
Copy code
// the plugin management block is evaluated first separately, do not change this to
// listOf(pluginManagement.repositories, dependencyResolutionManagement.repositories)
// instead that would change the semantics
pluginManagement {
    listOf(repositories, dependencyResolutionManagement.repositories).forEach { repositories ->
        repositories.apply {
            maven("...") {
                ...
            }
        }
    }
}
a

arekolek

02/27/2024, 11:22 AM
interesting, thanks for the tip 👍
👌 1
3 Views