Packaging Wiremock Stubs into a Standalone JAR
The Wiremock project is pretty great for stubbing out HTTP services, and I've been using it for a few years for both local testing, and distributing stubs to consumers of applications.
Although you can distribute stubs in i.e. a ZIP or a lightweight dependency packaged as a JAR, it can also be useful to package it as a standalone JAR, which could be additionally distributed in a Docker image.
Wiremock has supported a standalone JAR for some time, but until the v2.32.0 release this morning, it wasn't possible to package the stubs into the same JAR as the Wiremock JAR.
Now it's possible to package it all into a single JAR, and allow you to execute the following:
java -jar /path/to/standalone-jar.jar --load-resources-from-classpath 'wiremock'
Note that you must put your resources into a directory in the classpath as it's not possible to use the top-level classpath.
So how do I get this working?
Following similar setup to Packaging Wiremock Extensions into the Standalone Server Runner, we can set up our build.gradle
:
plugins {
id "com.github.johnrengelman.shadow" version "7.0.0"
id 'java'
}
project.ext {
wiremockVersion = '2.32.0'
}
dependencies {
runtimeOnly "com.github.tomakehurst:wiremock-jre8-standalone:$wiremockVersion"
implementation "com.github.tomakehurst:wiremock-jre8:$wiremockVersion"
}
shadowJar {
mergeServiceFiles() // https://www.shiveenp.com/posts/fix-shadow-jar-http4k-jetty/
archiveClassifier.set '' // as this defaults to a separate artifact, called `-all`
}
jar {
manifest {
attributes 'Main-Class': 'com.github.tomakehurst.wiremock.standalone.WireMockServerRunner'
}
}
assemble.dependsOn 'shadowJar'
This can be seen in this sample project.
Then, when we populate the src/main/resources/wiremock
directory:
src/main/resources/wiremock/mappings/stub.json
src/main/resources/wiremock/__file/
And that's it!