Define project dependency configuration dynamically

I’ve got a subproject (let’s call it mySubProject), from which I’d like to access the archived jars of some other subprojects via a dependency configuration.

But I don’t want to define those project dependencies explicitly, but rather add them to my configuration dynamically. I’ve written some kind of loop that iterates over those project names:

loop {
    dependencies.add("myConfigurationName", "project(path: ':$projectName', configuration: 'archives')")
}

But if I try to access them by something like this:

task printConfig << {
    configurations.myConfigurationName.files.each { File file ->
        println file.name
    }
}

I get

“Could not find project(path: ‘:someSubprojectName’, configuration.” Required by:

src: mySubProject:0.0.0.1"

You need to get rid of the quotes around the project dependency. A project dependency is not a String. Also, you need to change ‘project’ to ‘dependencies.project’.

Thank you very much.

I figured, that the quotes were wrong, but I couldn’t get Gradle to evaluate the $projectName variable.

It works with

dependencies.add("myConfigurationName", dependencies.project(path: ":$projectName", configuration: 'archives'))

Guess that failed before, because I just wrote “project” instead of “dependencies.project”

You can mark this one as answered.