Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle multi project transitive dependency

I have three gradle projects. Say ProjectA, ProjectB and ProjectC.

ProjectC is dependent on both ProjectA and ProjectB. While ProjectB is dependent on ProjectA.

So ProjectC's build.gradle has the following lines:

dependencies {
  implementation project(':ProjectA')
  implementation project(':ProjectB')
}

And ProjectB's build.gradle has the following:

dependencies {
  implementation project(':ProjectA')
}

My question is why do I need explicit implementation declaration for ProjectA in ProjectC's build file?

Since, I am adding ProjectB, shouldn't ProjectA be included automatically since ProjectB is dependent on ProjectA ?

In other words, why the following does not work for ProjectC ?

dependencies {
  implementation project(':ProjectB')
}

I am new to gradle and thus trying to understand how dependency management between Project's work.


Edit:

So I want to change ProjectB's build.gradle to below:

dependencies {
  api project(':ProjectA')
}

So that I can simplify ProjectC's build.gradle to:

dependencies {
  implementation project(':ProjectB')
}

However, I get the following error:

A problem occurred evaluating project ':ProjectB'.
> Could not find method api() for arguments [:ProjectA] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.

Am I missing something?

like image 318
Vicky Avatar asked Mar 28 '19 03:03

Vicky


People also ask

How do you handle transitive dependencies in Gradle?

Transitive dependency Releases of a module hosted on a repository can provide metadata to declare those transitive dependencies. By default, Gradle resolves transitive dependencies automatically. The version selection for transitive dependencies can be influenced by declaring dependency constraints.

How do you avoid transitive dependencies in Gradle?

You can tell Gradle to disable transitive dependency management for a dependency by setting ModuleDependency.

What does -> mean in Gradle dependencies?

It means that dependency graph contains multiple dependencies with the same group and module but different versions for e.g. org.


2 Answers

Because implementation is precisely for that: it tells that ProjectA is needed for the code of ProjectB to work (internally), but is not part of its API (i.e. you don't want clients of ProjectB to rely on the fact that it uses ProjectA internally).

If you want ProjectA to be part of the API of ProjectB, then use the api configuration rather than implementation.

See the guide for more details.

like image 92
JB Nizet Avatar answered Sep 25 '22 04:09

JB Nizet


Add

plugins {
    id 'java-library'
}

to the build.gradle file, this will enable api modifier for the parent project

See: https://docs.gradle.org/current/userguide/java_library_plugin.html

like image 28
Benas Avatar answered Sep 26 '22 04:09

Benas