I have a library that provides implementation for processing some data and an abstract class that must be extended by the program that is using it to use the data processing stuff.
There is also another library, that depends on the first one and also implements the abstract class. I would like to make both of the libraries available to the program depending on the second one.
Example:
libA/ (no deps)
DataProcessor.java
AbstractDataSource.java
libB/ (depends on libA)
FilesystemDataSource.java
app/ (depends on libB)
[here I want to access both DataProcessor and FilesystemDataSource]
I know that I can add both of the libraries as dependencies to the app, but it would be probably easier to maintain when there is not so many items in build.gradle
files.
CodePudding user response:
Declare the dependencies with api
, not implementation
:
From https://stackoverflow.com/a/44493379/68086:
From the Gradle documentation:
dependencies { api 'commons-httpclient:commons-httpclient:3.1' implementation 'org.apache.commons:commons-lang3:3.5' }
Dependencies appearing in the
api
configurations will be transitively exposed to consumers of the library, and as such will appear on the compile classpath of consumers.Dependencies found in the
implementation
configuration will, on the other hand, not be exposed to consumers, and therefore not leak into the consumers' compile classpath. This comes with several benefits:
- dependencies do not leak into the compile classpath of consumers anymore, so you will never accidentally depend on a transitive dependency
- faster compilation thanks to reduced classpath size
- less recompilations when implementation dependencies change: consumers would not need to be recompiled
- cleaner publishing: when used in conjunction with the new maven-publish plugin, Java libraries produce POM files that distinguish exactly between what is required to compile against the library and what is required to use the library at runtime (in other words, don't mix what is needed to compile the library itself and what is needed to compile against the library).
The compile configuration still exists, but should not be used as it will not offer the guarantees that the
api
andimplementation
configurations provide.
CodePudding user response:
Add libA as a dependency for libB in the gradle file. Also add libB as a dependency in libC. Also set transitive to true in the gradle configuration of libC so it will fetch transitive dependent libraries.
Checkout gradle official documention on declaring dependencies and transitive dependencies .
A variant of a component can have dependencies on other modules to work properly, so-called transitive dependencies. Releases of a module hosted on a repository can provide metadata to declare those transitive dependencies. By default, Gradle resolves transitive dependencies automatically.