How to add a dependency at build time?

Hello,

I have a muliti-project build.gradle.kts that builds a Spring Boot webflux web service. At build-time, I need to dynamically insert a dependency based on an environment variable’s value, and only one plugin can be used for a particular build. Basically, the variable is the name of a plugin that needs to be baked in with the rest of the service’s code for the executable jar. The plugins are in separate modules/projects. I cannot quite get this to work and have been trying to use Gradle substitutions. I am sure that there must be an easy way to accomplish this goal. I’d appreciate any tips or nudges in the right direction.

Here is the current substitution code… Besides not working as I hoped, it is also running for every project in my multiple project build. I only need to insert the plugin into the root project’s build.

configurations.all {
    resolutionStrategy
        .dependencySubstitution {
        println(">>>>>>>>>> FOUND PLUGIN NAME '$pluginName' IN THE ENVIRONMENT.")
        pluginNameToProjectPath
            .takeIf { it.containsKey(pluginName) }
            .let { plugin ->
                println(">>>>>>>>>> ATTEMPTING TO SUBSTITUTE THE DEFAULT PLUGIN MODULE WITH: '$pluginName'.")

                substitute(module("microservices:plugins:default")).with(
                    module(
                        plugin
                            ?.get(pluginName)
                            .toString()
                    )
                )
                println(">>>>>>>>>> SUBSTITUTION OF THE DEFAULT PLUGIN MODULE WITH: '$pluginName' WAS SUCCESSFUL.")
            }
    }
}

Thanks,
Mike