# Include transitive dependencies in forced dependency

**URL:** <https://discuss.gradle.org/t/include-transitive-dependencies-in-forced-dependency/9788>\
**Category:** Help/Discuss\
**Created:** [May 26, 2015, 12:25pm UTC](https://discuss.gradle.org/t/include-transitive-dependencies-in-forced-dependency/9788 "2015-05-26T12:25:56Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Francois\_Guillot](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/francois_guillot/32/8566_2.png) [@Francois\_Guillot](https://discuss.gradle.org/u/Francois_Guillot)\
**Post date:** [May 26, 2015, 12:25pm UTC](https://discuss.gradle.org/t/include-transitive-dependencies-in-forced-dependency/9788/1 "2015-05-26T12:25:56Z")

</div>

Let’s say I have the following dependency tree

```
MyProj
|
|----A
| |------B
| |-----C
|----D

```

in my proj, I define

> dependencies {  
> compile ‘A’, version 1  
> compile ‘D’, version 1  
> }

I get

```
MyProj
|----A (1)
| |------B (1)
| |-----C(1)
|----D (1)

```

If I recompile B with another C version

```
B (1.1)
|------C (1.1)

```

If I set

```
dependencies {
compile 'A', version 1
compile 'D', version 1
compile ('B' version 1.1){force=true}
}

```

I get

> ```
> MyProj
> |----A (1)
> | |------B (1.1) forced
> | |-----C(1)
> |----D (1)
> 
> ```

B only is forced, not his transitive dependencies.  
How can I simply express that I want to force B _and its transitive dependencies_ as well ?  
i.e.

> ```
> MyProj
> |----A (1)
> | |------B (1.1) forced
> | |-----C(1.1) forced
> |----D (1)
> 
> ```

---

<div class="post-metadata">

**Author:** ![mark\_vieira](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/mark_vieira/32/5396_2.png) [@mark\_vieira](https://discuss.gradle.org/u/mark_vieira)\
**Post date:** [May 27, 2015, 2:12am UTC](https://discuss.gradle.org/t/include-transitive-dependencies-in-forced-dependency/9788/2 "2015-05-27T02:12:16Z")

</div>

There is currently no way express this without explicitly forcing each transitive dependency. To avoid having to add all the transitive dependencies as direct dependencies you’ll probably want to configure your resolution strategy instead.

```
configurations.all {
  resolutionStrategy.force 'b:transitive-a:1.1', 'b:transitive-b:1.1'
}
```

---

<div class="post-metadata">

**Author:** ![Francois\_Guillot](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/francois_guillot/32/8566_2.png) [@Francois\_Guillot](https://discuss.gradle.org/u/Francois_Guillot)\
**Post date:** [May 27, 2015, 6:28am UTC](https://discuss.gradle.org/t/include-transitive-dependencies-in-forced-dependency/9788/3 "2015-05-27T06:28:24Z")

</div>

ok, thanks Mark.  
Wouldn’t it be a feature to keep in mind for future enhancement of the dependency management ?  
e.g.

> compile (“goup:module:version”){ force = NO / SINGLE / TRANSITIVE}

(replacing the force boolean by an enum, to include the ‘transitive’ possibility, that would force all the transitive dependencies of the forced dependency)  
This mechanism would under the hood install `resolutionStrategy.force SINGLE` on all the found transitive dependencies  
… or something like that 😄

---

<div class="post-metadata">

**Author:** ![mark\_vieira](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/mark_vieira/32/5396_2.png) [@mark\_vieira](https://discuss.gradle.org/u/mark_vieira)\
**Post date:** [May 27, 2015, 9:22pm UTC](https://discuss.gradle.org/t/include-transitive-dependencies-in-forced-dependency/9788/4 "2015-05-27T21:22:30Z")

</div>

Can give some more context to your specific use case? I think your example is a bit overly simplistic. Forcing a newer version of a dependency is almost never necessary since conflict resolution would end up choosing that version anyway.

---

<div class="post-metadata">

**Author:** ![Francois\_Guillot](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/francois_guillot/32/8566_2.png) [@Francois\_Guillot](https://discuss.gradle.org/u/Francois_Guillot)\
**Post date:** [May 27, 2015, 10:29pm UTC](https://discuss.gradle.org/t/include-transitive-dependencies-in-forced-dependency/9788/5 "2015-05-27T22:29:10Z")

</div>

At work we have several sub components: A, B, C, D, E, F, G  
Their relation is sth like

> ```
> A
> |
> B
> / \
> C D
> \ /
> E
> |
> F
> |
> G
> 
> ```

Each of them has its own repository and release lifecyle (different release name, not always released at the same time)

Sometimes when developing a feature, the main modification is in C, but to make it work I also have to change something in A and B.  
So I end up having three private versions: myA, myB, myC.  
Before commiting each one of them, I want to perform our integration tests, which we normally launch by telling on which version of G it shall be launched.  
By transitive dependency retrieval, it’s gonna take all the sub components, until A  
(e.g. I tell to run with G version 1.1-snapshot, and it’s gonna look the ivy.xml to retrieve F 1.1-snap, E 1.0-snap (for instance) etc etc)

So in my case I want to use the ‘current’ G version and its dependencies, except myA, myB, myC.  
I could write

```
dependencies {
integTest 'group:G:1.1-snapshot'
integTest ('group:C:myC'){force=true}
integTest ('group:B:myB'){force=true}
integTest ('group:A:myA'){force=true}
}

```

or as you suggested, I could use resolutionStrategy to specify all three versions of A,B,C  
but I think it would be simpler to say

> “I want to test myC version of C, and therefore ‘trust’ its declared dependencies, no matter what they are”

As you pointed out, conflict resolution will choose the newest version anyway, but that’s not necessarilly the one I want.  
I want to trust the transitive version of a dependency (because I’m developing several librairies tightly coupled with each other)

---

<div class="post-metadata">

**Author:** ![mark\_vieira](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/mark_vieira/32/5396_2.png) [@mark\_vieira](https://discuss.gradle.org/u/mark_vieira)\
**Post date:** [May 27, 2015, 10:47pm UTC](https://discuss.gradle.org/t/include-transitive-dependencies-in-forced-dependency/9788/6 "2015-05-27T22:47:15Z")

</div>

I think in this case you really do want to use the resolution strategy method. This has the benefit of applying to transitive dependencies of _other_ projects, that might also depend on A, B, or C. I think for this use case, what you really want is the ability to explicitly enforce the usage of defined set of dependencies, which is what the resolution strategy method will accomplish. In this _particular_ case I believe it just incidentally happens to be that they are primarily all transitive dependencies of the same dependency.

Take for example you were working on project F, which required changes to C and D. You definitely wouldn’t want to use the recursive force in that case, as E might specify some dependencies that you do want conflict resolution to resolve, or for that matter, same for projects B and A.

I don’t believe we want to introduce a “recursive force” feature at this time. Doing so would have non-trivial implications. While in this case such a feature might make for a slightly more concise build script, I’m not certain it is actually the best course of action.

---

<div class="post-metadata">

**Author:** ![Francois\_Guillot](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/francois_guillot/32/8566_2.png) [@Francois\_Guillot](https://discuss.gradle.org/u/Francois_Guillot)\
**Post date:** [May 28, 2015, 9:21am UTC](https://discuss.gradle.org/t/include-transitive-dependencies-in-forced-dependency/9788/7 "2015-05-28T09:21:56Z")

</div>

ok, I see your point.  
I know which version I want to use anyway,so it’s not a problem of knowing what to write in the resolutionStrategy block. Thanks for your insight

---

<div class="post-metadata">

**Author:** ![mkornipati](https://avatars.discourse-cdn.com/v4/letter/m/f05b48/32.png) [@mkornipati](https://discuss.gradle.org/u/mkornipati)\
**Post date:** [September 30, 2015, 7:55pm UTC](https://discuss.gradle.org/t/include-transitive-dependencies-in-forced-dependency/9788/8 "2015-09-30T19:55:17Z")

</div>

Hi Mark, I am going through

[https://docs.gradle.org/current/dsl/org.gradle.api.artifacts.ResolutionStrategy.html#org.gradle.api.artifacts.ResolutionStrategy:force(java.lang.Object[])](https://docs.gradle.org/current/dsl/org.gradle.api.artifacts.ResolutionStrategy.html#org.gradle.api.artifacts.ResolutionStrategy:force(java.lang.Object%5B%5D))

and it says "Allows forcing certain versions of dependencies, including transitive dependencies. "

So, I am assuming the transitives should be forced too. Is that not or am I misreading it?

Regards,  
Mohan

---

<div class="post-metadata">

**Author:** ![mark\_vieira](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/mark_vieira/32/5396_2.png) [@mark\_vieira](https://discuss.gradle.org/u/mark_vieira)\
**Post date:** [October 2, 2015, 9:02pm UTC](https://discuss.gradle.org/t/include-transitive-dependencies-in-forced-dependency/9788/9 "2015-10-02T21:02:11Z")

</div>

> [@mkornipati](#):
>
> So, I am assuming the transitives should be forced too. Is that not or am I misreading it?

What it means is that the module will be forced _even if_ it is a transitive (non-direct) dependency. What it will NOT do is also force its transitive dependencies, which is what Francois was talking about above. Basically, only the single module is forced, we do not recursively set `force = true` on all the transitive dependencies as well.

---

<div class="post-metadata">

**Author:** ![mkornipati](https://avatars.discourse-cdn.com/v4/letter/m/f05b48/32.png) [@mkornipati](https://discuss.gradle.org/u/mkornipati)\
**Post date:** [October 2, 2015, 9:24pm UTC](https://discuss.gradle.org/t/include-transitive-dependencies-in-forced-dependency/9788/10 "2015-10-02T21:24:18Z")

</div>

Thanks Mark. Got it.

Regards,  
Mohan
