Build your HiveMQ Extensions with Gradle
Written by Lukas Brand
Published: October 1, 2020
Now, in addition to Maven, you can use the Gradle build tool to create HiveMQ extensions. This new option lets you take advantage of everything Gradle has to offer. For example,
- incremental builds,
- better dependency management, and
- scripting your build configuration.
To make it even easier to set up a complete Gradle build pipeline for your HiveMQ extension, we’ve created a Gradle plugin that registers all the basic tasks you need from start to finish.
Example HiveMQ extension build with Gradle
Let’s take a look at how to create a HiveMQ extension with Gradle. In this example I use the Kotlin DSL for the Gradle scripts.
First, to apply the
com.hivemq.extension Gradle plugin, add the following lines to the build files that are located at the root of your project.
Once you apply the HiveMQ extension plugin to your project, you can configure all the important HiveMQ extension properties directly in your build file.
You may notice strong similarities between the
hivemq-extension.xml you used to create and bundle into your extension zip.
The Gradle plugin takes care of creating the file for you and filling it with all the required properties.
Additionally, the gradle plugin generates the service descriptor file
Just specify the mainClass entry point of the extension as shown above.
That is all you need to create a HiveMQ extension with Gradle.
In order to assemble your extension, simply execute the
hivemqExtensionZip Gradle task which executes all the other tasks needed to create the finalized zip of your HiveMQ extension.
When the task is done, the zip file appears in the
build/hivemq-extension folder of your project folder.
We recommend using a Gradle wrapper so that every build uses the same Gradle version. To initialize the Gradle wrapper, execute the following command in your project folder. At least Gradle version 6 is required.
gradle wrapper --gradle-version=6.6.1
You can build your HiveMQ extension with:
Assemble additional files into your extension folder
Files inside the
src/hivemq-extension folder are automatically included in your HiveMQ extension zip.
hivemq-hello-world-extension ├─ src │ ├─ hivemq-extension // Everything here is added to the extension folder in the zip │ ├─ main // Everything here is used to generate the jar archive │ │ ├─ java // Your code │ │ └─ resources // Resources inside the jar, do not put resources for the zip here │ └─ test │ ... ├─ build.gradle.kts └─ settings.gradle.kts
If you want to add files from other locations to the zip, you can use the
hivemqExtensionResources Gradle task.
Due to Gradle’s scripting nature, you can also include files that your own custom tasks create.
Debug your extension directly from your IDE
The HiveMQ Gradle plugin lets you run your extension with HiveMQ directly from your IDE. This enables you to quickly debug and test your extension. To use this feature, you need to download HiveMQ Enterprise or Community Edition first and unzip it somewhere suitable.
For example, add the following lines to your build file.
The HiveMQ extension Gradle plugin enables you to easily develop, debug, and build your HiveMQ extensions with Gradle. Simply configure some metadata and make use of Gradle’s flexible scripting capabilities for more complicated extension builds.
Have a great day,
Lukas from the HiveMQ Team