Sub project dependency inheritance

I have a gradle multi project setup. Let’s say the project contains two sub projects

|    |_build.gradle.kts
|    |_build.gradle.kts

settings.gradle.kts includes both project1 and project2

projects2 has following dependency specification

dependencies {

project1 has the following dependency specification

dependencies {
    implementation(project(":project2"))  // Dependency on project2

dependency graph of project1 show postgres driver jar only in

runtimeClasspath - Runtime classpath of source set 'main'.
\--- project :project2
     \--- org.postgresql:postgresql:42.7.0
          \--- org.checkerframework:checker-qual:3.31.0

The compile classpath of project1 appears as

compileClasspath - Compile classpath for source set 'main'.
\--- project :project2

I could not use/access postgres jdbc driver classes in project1’s classes directly.

Is this the default behaviour? Should project1 dependency be modified to include postgres jdbc?

Yes, this is the default and correct behavior.

You declared the dependency as implementation dependency of project 2, so it does not pollute the compile classpath of downstream projects, which is bad per se and also would make compilation unnecessarily slow.

If you use the classes from this jar in the public API of project 2 (parameter types, return types, superclasses, …) you would declare the dependency as api not implementation which would also make it available to consumers like project 1.

But don’t just declare it as api because you also need it in project 1’s implementation, that’s bad practice. If it just also needs the jar for its implementation, depend on it explicitly.

There is also this plugin that greatly helps in putting dependencies to the correct configurations: GitHub - autonomousapps/dependency-analysis-gradle-plugin: Gradle plugin for JVM projects written in Java, Kotlin, Groovy, or Scala; and Android projects written in Java or Kotlin. Provides advice for managing dependencies and other applied plugins