Slackbot
05/18/2023, 8:24 PMChris Lee
05/18/2023, 8:29 PMRoberto Fuentes
05/18/2023, 8:46 PMRoberto Fuentes
05/18/2023, 8:50 PMclass SettingsPlugin implements Plugin<Settings> {..}
Where you override this apply
function, doesn’t seem to be a place where you can expose functions and use it in other placesAdam
05/18/2023, 8:58 PMsettings.gradle.kts
or build.gradle.kts
) there are basically two ways
Either you add a dependency using the buildscript block
buildscript {
dependencies {
classpath("org.apache.commons:commons-lang3:jar:3.12.0")
}
}
This is good for dependencies that are published to a Maven repo you can add, but it’s not so convenient for project-local utilities that you don’t want to publish
The alternative is to use a composite build. This is probably what you want.
1. create a new directory in your project, my-settings-utils
, and because this is going to be an independent Gradle build (not a subproject) create a build.gradle.kts
and a settings.gradle.kts
2. in build.gradle.kts
, apply the kotlin-dsl plugin
3. Create your utility class - my-settings-utils/src/main/kotlin/RepoUtils.kt
4. add includeBuild("./my-settings-utils")
in the base project settings file, as detailed in the docs
5. my memory is vague here, but I think you need to create a settings plugin, else Gradle won’t add the my-settings-utils.jar
to the settings.gradle.kts
buildscript classpath. So just create a dummy precompiled script plugin - my-settings-utils/src/main/kotlin/my-repos.settings.gradle.kts
- which can be empty, it doesn’t need any content
6. Now apply the plugin in settings.gradle.kts
plugins {
id("my-repos")
}
And the JAR will be added to the buildscript classpath, and because RepoUtils.kt
will be compiled into the same JAR as the dummy plugin those functions will be availableVampire
05/18/2023, 9:14 PM// 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("<https://nexus.company.com/repository/maven>") {
name = "Company Nexus"
}
}
}
}
Roberto Fuentes
05/18/2023, 9:28 PMsettings.gradle.kts
and can use the repositories function without duplicating them!
And now I have another small question
So if this block is evaluated first separately it means that it cannot access anything outside of pluginManagement
? And neither someone can access any function found inside this block, right?
So let’s say in my build.gradle.kts
. it’s impossible to access any function I’ve created there in my settings.gradle.kts
right?
And I guess gradle.settingsEvaluated
doesn’t add anything hereVampire
05/18/2023, 9:31 PMpluginManagement
are seen outside too.
That's why you can confiugre the dependencyResolutionManagement.repositories
inside the block while you would usually do it outside.
So you could define some function and store it in an ext
property within pluginManagement
and then also use it outside, retrieving it from the extra properties.Roberto Fuentes
05/18/2023, 10:13 PMpluginManagement
block this:
extra.set("dummy", "dummy")
And on build.gradle.kts
I read it like this:
extra.get("dummy") as String
And it says, it cannot get property “dummy”
So I’m not sure if I’m doing something wrong or I can’t get it in the build.gradle.kts
at project levelVampire
05/18/2023, 10:15 PMRoberto Fuentes
05/19/2023, 6:36 AMpluginManagement
) not in different files. (It helps to re-read again after 8h of sleep 😂)
Got it, thank you!
Although my problem has been partially solved by not duplicating repos (Which is amazing 🙇) I have the feeling that there’s no way to share the function nor results or use the same function in both places: pluginManagement
& build.gradle.kts
root level.Vampire
05/19/2023, 6:46 AMGradle
instance. It just gets more dirty to do so. :-D especially as there should be no reason to do so. The only thing could be repositories and when defining them centrally using dependencyResolutionManagement
, there is not much point in declaring them in the build script again. I even usually forbid project repositories.Roberto Fuentes
05/19/2023, 9:50 AMsettings.gradle.kts
to my build.gradle.kts
buildscript block!!
In my settings.gradle.kts
pluginManagement block I set it like this:
(gradle as ExtensionAware).extensions.extraProperties.set("dummy", "dummy")
Then on my build.gradle.kts
buildscript block i do the same:
println("Buildscript: ${(gradle as ExtensionAware).extensions.extraProperties.get("dummy")}")
Damn that was a nice finding, thank you!! 🙇Roberto Fuentes
05/19/2023, 9:51 AMVampire
05/19/2023, 11:28 AMShaun Reich
05/19/2023, 10:21 PMRoberto Fuentes
05/22/2023, 6:32 PMpluginManagement
block. Inside there I define the
• repositories
block (Which is for conventions plugin)
• dependencyResolutionManagement
◦ repositories
block for dependenciesShaun Reich
05/23/2023, 1:10 PM