Automatically getting dependencies of other modules

Hi,

I have a multi-project build (https://docs.gradle.org/current/userguide/multi_project_builds.html)

The one module depends on some abstracted testing code that I have inside another module called “:shared:testutils”. This module has JUnit as one of its dependencies as its some abstracted test functionality using JUnit.

Now - I want to use it another module. And it works.
In the other module:

apply plugin: "java"

version = "1.0.0"

dependencies {
    compile project(":shared:testutils")

    testImplementation "org.junit.jupiter:junit-jupiter-api:5.5.2"
    testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:5.5.2"
}

My question -
Can I automatically import the dependencies from the “:shared:testutils” module. In other words, can I somehow remove the explicit referencing of JUnit in the code segment above?

This is just an example. I have a few :shared:* modules and I constantly have to repeat their dependencies when I use them?

Thank you kindly for any help.
I scanned through the documentation on composite builds and cross project publications (https://docs.gradle.org/6.7.1/userguide/cross_project_publications.html) but thought I’d just ask for some direction on a conceptual level. I tried a few things but with no success :frowning:

Thank you :slight_smile:

Conceptually, :shared:testutils is a library. You should apply the java-library plugin instead of just java and declare the JUnit dependencies as api dependencies. However, it’s odd that your the project shown has a compile dependency on it. It seems more likely this should be a testImplementation dependency unless this project’s actual purpose is also to be a module used in other module tests.

Good idea, but didn’t work. I converted those modules to libraries. But I still had to define the dependencies as indicated.

I’m not sure if its possible to programmatically access the dependencies of the module, iterate over them, and add them to the application module…?

Sorry!!! Some operator error on my side.

Thanks - it does actually work. So I could reduce my dependencies in a few places :slight_smile: :slight_smile: :slight_smile:

If I want to use something (like explicitly reference it in the source code of the current module) from another module’s dependencies in my current module - I still need to declare the dependency explicitly. But, if its just a dependency that the other module uses and aren’t directly referenced by the current module - then I no longer need to declare it.

In the one place I still have a dependency declared in the current module where the other module returned a subclass of one of the things in one of its dependencies - maybe pointing to something that I should resolve in a better way (eg use composition rather than extend so that the module’s interface doesn’t depend on one of its dependencies)

Thanks!!

I’m also going to look at the Platform plugin to improve my handing of dependencies a bit more, to ensure that all subprojects use the same version of specific dependencies as documented here: