Packaging Wiremock Stubs into a Standalone JAR

Featured image for sharing metadata for article

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!

Written by Jamie Tanna's profile image Jamie Tanna on , and last updated on .

Content for this article is shared under the terms of the Creative Commons Attribution Non Commercial Share Alike 4.0 International, and code is shared under the Apache License 2.0.

#blogumentation #java #gradle #wiremock.

This post was filed under articles.

Interactions with this post

Interactions with this post

Below you can find the interactions that this page has had using WebMention.

Have you written a response to this post? Let me know the URL:

Do you not have a website set up with WebMention capabilities? You can use Comment Parade.