# Add dependencies only for root project

**URL:** <https://discuss.gradle.org/t/add-dependencies-only-for-root-project/32825>\
**Category:** Help/Discuss\
**Created:** [August 18, 2019, 4:11am UTC](https://discuss.gradle.org/t/add-dependencies-only-for-root-project/32825 "2019-08-18T04:11:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![RareScrap](https://avatars.discourse-cdn.com/v4/letter/r/34f0e0/32.png) [@RareScrap](https://discuss.gradle.org/u/RareScrap)\
**Post date:** [August 18, 2019, 4:11am UTC](https://discuss.gradle.org/t/add-dependencies-only-for-root-project/32825/1 "2019-08-18T04:11:12Z")

</div>

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

```gradle
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:

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

```

this:

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

```

and this:

```gradle
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?**

---

<div class="post-metadata">

**Author:** ![RareScrap](https://avatars.discourse-cdn.com/v4/letter/r/34f0e0/32.png) [@RareScrap](https://discuss.gradle.org/u/RareScrap)\
**Post date:** [August 20, 2019, 9:22am UTC](https://discuss.gradle.org/t/add-dependencies-only-for-root-project/32825/2 "2019-08-20T09:22:48Z")

</div>

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.**

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

```

---

<div class="post-metadata">

**Author:** ![Lance](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/lance/32/787_2.png) [@Lance](https://discuss.gradle.org/u/Lance)\
**Post date:** [August 20, 2019, 1:26pm UTC](https://discuss.gradle.org/t/add-dependencies-only-for-root-project/32825/3 "2019-08-20T13:26:47Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![RareScrap](https://avatars.discourse-cdn.com/v4/letter/r/34f0e0/32.png) [@RareScrap](https://discuss.gradle.org/u/RareScrap)\
**Post date:** [August 22, 2019, 6:13am UTC](https://discuss.gradle.org/t/add-dependencies-only-for-root-project/32825/4 "2019-08-22T06:13:56Z")

</div>

> [@Lance](#):
>
> It’s unorthodox what you’re doing here. Usually, the root project in a multi-module build does not do any compilation

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.

---

<div class="post-metadata">

**Author:** ![Lance](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/lance/32/787_2.png) [@Lance](https://discuss.gradle.org/u/Lance)\
**Post date:** [August 22, 2019, 6:28am UTC](https://discuss.gradle.org/t/add-dependencies-only-for-root-project/32825/5 "2019-08-22T06:28:01Z")

</div>

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
