Add dependencies only for root project

Hello. I need to add subprojects as runtime dependencies to root project.
I tried to do this:

dependencies {
    runtimeOnly project(':case1')
    runtimeOnly project(':case2')
}

but this leads to the fact that the subproject depends on itself in runtime.

Also i tried this:

rootProject {
    dependencies {
        runtimeOnly project(':case1')
        runtimeOnly project(':case2')
    }
}

this:

rootProject.dependencies {
        runtimeOnly project(':case1')
        runtimeOnly project(':case2')
}

and this:

dependencies {
    if (getProject().equals(rootProject)) {
        runtimeOnly project(':case1')
        runtimeOnly project(':case2')
    }
}

but subprojects still depend on themself in runtime.

So how can I specify dependencies only for rootProject?

Either the forum has record low attendance, or people don’t know the answer. But the question is very simple, by the way.

Although I could not answer the question I asked, I still found a way to solve the problem. If you carefully analyze the project structure, you can see that the subprojects pulls dependencies from the root project, which in turn pulls them from all subprojects. The solution is to turn off transitive dependencies for subprojects.

subprojects {
    dependencies {
        compile(rootProject) { transitive = false }
        compile rootProject // TADAAA!!!
    }
}

It’s unorthodox what you’re doing here. Usually, the root project in a multi-module build does not do any compilation

I suggest that you remove your java sources etc from the root project and create a new submodule for them. In my opinion, the root project should not have src/main/java. The root project is just a logical grouping of its subprojects and should only really contain common build logic.

1 Like

Gradle positions himself as a very flexible build tool that supports any project structure. All documentation is replete with a mention of this. Maybe my project structure is unusual, but the grad must support it.

Yes, Gradle’s very flexible. Yes, it can handle your use case. And yes, you’d be better off putting your sources in their own separate module rather than the root to avoid the “chicken or egg” scenario you’ve already encountered