This message was deleted.
# community-support
s
This message was deleted.
v
Maybe from
project.buildscript
?
j
I tried that but dependencies always shows up empty. I tried a bunch of of variations but not luck. Maybe I'm missing where they are contained in buildscript?
v
You usually get dependencies from configurations.
dependencies
is where you declare them.
j
I tried the following but the dependencies are always empty
Copy code
register("bbb") {
        project.buildscript.configurations.forEach {
            println("Bellini: configurations")
            println(it.name)
            println("Bellini: deps")
            val deps = it.dependencies
//            deps.iterator().forEach {
//                println(it.name)
//            }
            it.allDependencies.iterator().forEach {
                println(it.name)
            }
        }
    }
Output is
Copy code
Bellini: configurations
classpath
Bellini: deps
v
Maybe because you do it at configuration time instead of execution time? (missing
doLast
)
j
Tried with doLast{} but still empty unfortunately.
v
Works fine here, even at configuration time
Maybe you don't have dependencies in that build script class path
j
Thanks for helping here. Here is what my
buildSrc/build.gradle.kts
looks like:
Copy code
plugins {
    `kotlin-dsl`
}

repositories {
    mavenLocal()
    mavenCentral()
    gradlePluginPortal()
}

dependencies {
    val versionKotlin = "1.5.10"        // help with version upgrades to make sure all version are aligned in this file
    implementation("org.jetbrains.kotlin:kotlin-allopen:$versionKotlin")

    implementation("com.ea.stuff:stuff-common:16.6.0-SNAPSHOT")    implementation("org.kie:kie-api:7.45.0.Final")
    implementation("org.drools:drools-compiler:7.45.0.Final")
    implementation("com.fasterxml.jackson.core:jackson-databind:2.13.1")
}
What I want to programmatically obtain is the version of the dependency
com.ea.stuff:stuff-common:16.6.0-SNAPSHOT
v
That is not a buildscript dependency, but a normal project dependency
Only not-built-in plugins from the
plugins
block and things from a
buildscript { dependencies { ... } }
block are buildscript dependencies
j
Interesting... even though its the
buildSrc/build.gradle.kts
dependencies which has the plugins listed + and code used in the build script?
v
And where do you have the
bbb
task? And actually, what do you want to achieve?
j
I want to programmatically obtain is the version of the dependency
com.ea.stuff:stuff-common:16.6.0-SNAPSHOT
from the `buildSrc/build.gradle.kts`dependencies section. The
bbb
task is in a
stuff-conventions.gradle.kts
file with in the
buildSrc/src/main/kotlin/.../stuff-conventions.gradle.kts
v
Actually your problem is that you are using
buildSrc
and not an build. plugins / dependencies from an included build end up on the buildscript class path. (while you would still miss the dependency, because with your code you only get declared dependencies, no transitive ones which that one is from the viewpoint of the main build)
buildSrc
dependencies are in its own class loader that is a parent of the build script class loader. So those dependencies are not part of the build script configurations you are looking at.
What do you want to access the version for? I suspect that "I want to get the version of that dep" is your try to solve the actual use-case, but is not the use-case itself.
j
I need to pass that version to a subprocess that is the gradle build starts but doesn't control. So rather than hard coding the version in two places, I want to programmatically grab it.
v
If you want to get the version at runtime, you probably need to get a class from that dependency and then get that classes codesource from its protection domain and then parse the version out of the JAR file name. Or if that dependeny provides its version via some Version class or properties file or manifest entry, then you might get it from there. Or you get the version not in your convention plugin, but actually during the buildSrc build and write it to some properties file your convention plugin then can read. Or you define the version in some variable (or probably better in a version catalog) and then use it to declare the dependency and also to generate said properties file with a
WriteProperties
task, or with a filter on
processResources
to fill in a blank in a properties file you create manually, that your convention plugin then can read at runtime.
j
Thanks. I tried adding it to gradle.properties and then reading the properties but the `buildsrc/build.gradle.kts`doesn't have access to that. Version catalog would be good to try assuming
buildsrc/build.gradle.kts
can access it (haven't tried). Appreciate the ideas, I'll work through some of these and see what works.
v
tried adding it to gradle.properties and then reading the properties but the `buildsrc/build.gradle.kts`doesn't have access to that
buildSrc/gradle.properties
I hope. Otherwise it would make little sense. Well, you could even read
gradle.properties
, but you then have to do it explicitly, using a
Properties
instance.