Dynamically specifying a cross-subproject dependency in a plugin

I have a project with multiple subprojects, and am simplifying their build scripts.

To keep things simple, I’ll use a much simplified example.

There are two versions of a Java library, each with their own subproject, say java:he and java:ee. There are two versions of a Graal VM-using C library, which uses the outputs of the Java library process as input, c:he and c:ee.

The build scripts are complex, but the core logic is effectively identical between the he and ee versions. The core logic is extracted to a plugin (let’s call it cbuild) shared by both c:he and c:ee versions. What differences there are, are handled by applying and configuring the plugin.

The issue I’m having is that I want to depend on artifacts of the correct java:_e version in the cbuild plugin, and that is set by configuring the plugin’s extension like so:

# c/he/build.gradle.kts

plugins {
  id("cbuild")
}

cbuild {
  edition = "he"
}

The java:he project has defined a consumable configuration containing its source files, javaSources.

In the cbuild.gradle.kts plugin file I have this:

interface CBuildExtension {
    abstract val edition: Property<String>
}

val extension = project.extensions.create<CBuildExtension>("cbuild")

val javaSources by configurations.dependencyScope("javaSources")

dependencies {
    javaSources(project(mapOf(
        "path" to extension.edition.map { ":java:${it}" },
        "configuration" to "runtimeElements"
    )))
}

Needless to say, this does not compile.

Is there a mechanism I can use to achieve this, or do I need to declare it explicitly in a dependencies block in c/he/build.gradle.kts?

Thanks!

Needless to say, this does not compile.

Not so needless.
It might not work properly or throw a runtime error.
But it should compile just fine I’d say.
Which compile error do you get?

You probably meant a runtime error as the value for path is not a String, respectively it does not find a project with path like the toString() of the provider, right?

This should work:

dependencies {
    javaSources(
        extension.edition.map {
            project(path = ":java:$it", configuration = "runtimeElements")
        }
    )
}

But actually, have a look at ./gradlew :java:ee:outgoingVariants.
You should find that there is already a proper outgoing variant that publishes the sources which you could simply select by requesting the right attributes without any need to declare an own consumable configuration.