Part 7: Use a Consumer Project
Learn how to set up a dummy consumer project to test your plugin without publishing it.
Step 1: Create a Consumer Project
We will now create a separate consumer
project.
This is a best practice for testing plugins because it simulates how a real user would apply your plugin to their own project.
It also allows you to test your plugin without publishing it to a repository like the Gradle Plugin Portal.
First, update your project structure to include a new consumer
directory:
.
├── gradlew
├── settings.gradle.kts
├── plugin/
└── consumer/ (1)
├── settings.gradle.kts
└── build.gradle.kts
1 | Create a new consumer directory with its own build files. |
.
├── gradlew
├── settings.gradle
├── plugin/
└── consumer/ (1)
├── settings.gradle
└── build.gradle
1 | Create a new consumer directory with its own build files. |
Next, we’ll configure a Composite Build by updating the root settings.gradle(.kts)
file.
This tells Gradle to treat the plugin
project as an included build.
rootProject.name = "plugin-tutorial"
includeBuild("consumer")
rootProject.name = 'plugin-tutorial'
includeBuild('consumer')
The key line here is includeBuild("plugin")
inside the pluginManagement {}
block.
This makes all the plugins from the plugin
project available to other projects in the composite build by their ID.
Finally, we need to update the plugin’s build file to account for the composite build setup.
We’ll change the `id
from a local file to a standard String
.
plugins {
// Apply the Java Gradle plugin development plugin to add support for developing Gradle plugins
`java-gradle-plugin`
// Apply the Kotlin JVM plugin to add support for Kotlin.
/*
alias(libs.plugins.kotlin.jvm)
*/
id("org.jetbrains.kotlin.jvm") version "2.0.0"
}
dependencies {
// Use the awesome Spock testing and specification framework
/*
testImplementation libs.spock.core
*/
testImplementation 'org.spockframework:spock-core:2.3-groovy-4.0'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
// Use the Java Slack Client APIs
implementation("com.slack.api:slack-api-client:1.45.3")
}
In a Composite Build, the plugin project no longer has access to the root libs.versions.toml file. For this tutorial, we are using direct dependency declarations.
|
Step 2: Wire the Consumer Project to the Plugin
Now we’ll configure the consumer
project’s settings file to apply our plugin.
Add this code to consumer/settings.gradle(.kts)
:
pluginManagement {
includeBuild("../plugin")
repositories {
gradlePluginPortal()
}
}
rootProject.name = "consumer"
pluginManagement {
includeBuild("../plugin")
repositories {
gradlePluginPortal()
}
}
rootProject.name = "consumer"
This tells Gradle that when it encounters a plugins
block that references your org.example.slack
plugin, it should look for the plugin in the plugin
project of the composite build.
Step 3: Apply and Configure the Plugin in the Consumer
Now, let’s configure the consumer
project’s build.gradle(.kts)
file to actually apply and configure our plugin.
plugins {
id("java")
id("org.example.slack")
}
repositories {
mavenCentral()
}
slack {
token.set(System.getenv("SLACK_TOKEN"))
channel.set("#social")
message.set("Hello from consumer via composite build!")
}
plugins {
id("java")
id("org.example.slack")
}
repositories {
mavenCentral()
}
slack {
token.set(System.getenv("SLACK_TOKEN"))
channel.set("#social")
message.set("Hello from consumer via composite build!")
}
Notice that we don’t need a version
for the plugin! Gradle automatically resolves it from the included build.
Step 4: Run the Consumer Project
You can now test your plugin by running a task in the consumer
project.
From the consumer
directory, you can run your custom task:
$ ./gradlew sendTestSlackMessage
Or, from the root directory, you can run the same task using its full path:
$ ./gradlew :consumer:sendTestSlackMessage
You can also run a standard lifecycle task like build
, which will automatically trigger the FlowAction
to send a Slack notification at the end of the build.
$ ./gradlew :consumer:build
You can now iterate on your plugin in the plugin
directory and test it immediately in the consumer
project without any publishing overhead.
Next Step: Publish your Plugin Locally >>