Testing library changes in consuming project with Gradle
When working on a library, we always test the library in isolation, that’s obvious. But sometimes, we still want to try the new changes out in some consuming project before publishing the new version.
This is easily achievable with Gradle using included builds.
Let’s say we have a simple consumer project that consumes some library.
settings.gradle
:
dependencyResolutionManagement {
repositories {
mavenCentral()
}
}
rootProject.name="consumer"
include("app")
app/build.gradle
:
plugins {
id("application")
}
dependencies {
implementation("io.github.rieske.dbtest:postgresql:0.0.4") // the library that we want to test with a new, not yet released changes
}
And we have the library sources with some local changes.
settings.gradle
:
rootProject.name="libs"
include("postgresql")
postgresql/build.gradle
:
plugins {
id("java-library")
}
group="io.github.rieske.dbtest"
Given the following file system layout:
├── consumer
│ ├── app
│ ├── gradle
│ ├── gradlew
│ └── settings.gradle
└── lib
├── gradle
├── gradlew
├── postgresql
└── settings.gradle
We can substiture the published version of library io.github.rieske.dbtest:postgresql:0.0.4
with what is produced by the lib/postgresql
build using the following command from the consumer
directory:
./gradlew --include-build ../lib build
Verify that the dependecy is substituted using:
$ ./gradlew --include-build ../lib :app:dependencyInsight --dependency io.github.rieske.dbtest:postgresql
> Task :app:dependencyInsight
project :lib:postgresql (by composite build)
Variant apiElements:
| Attribute Name | Provided | Requested |
|--------------------------------|----------|--------------|
| org.gradle.category | library | library |
| org.gradle.dependency.bundling | external | external |
| org.gradle.jvm.version | 17 | 17 |
| org.gradle.libraryelements | jar | classes |
| org.gradle.usage | java-api | java-api |
| org.gradle.jvm.environment | | standard-jvm |
io.github.rieske.dbtest:postgresql:0.0.4 -> project :lib:postgresql
\--- compileClasspath
A web-based, searchable dependency report is available by adding the --scan option.
BUILD SUCCESSFUL in 513ms
1 actionable task: 1 executed
As opposed to running the same without the included build:
$ ./gradlew :app:dependencyInsight --dependency io.github.rieske.dbtest:postgresql
> Task :app:dependencyInsight
io.github.rieske.dbtest:postgresql:0.0.4
Variant apiElements:
| Attribute Name | Provided | Requested |
|--------------------------------|----------|--------------|
| org.gradle.status | release | |
| org.gradle.category | library | library |
| org.gradle.dependency.bundling | external | external |
| org.gradle.jvm.version | 11 | 17 |
| org.gradle.libraryelements | jar | classes |
| org.gradle.usage | java-api | java-api |
| org.gradle.jvm.environment | | standard-jvm |
io.github.rieske.dbtest:postgresql:0.0.4
\--- compileClasspath
A web-based, searchable dependency report is available by adding the --scan option.
BUILD SUCCESSFUL in 516ms
1 actionable task: 1 executed