Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Small wrapper example to use sw-precache with Gradle

Writing a custom Gradle wrapper

Using sw-precache with Gradle is rather straightforward which allows you to for example generate a service-worker for the Javadoc of your Java application.

task generateJavadoc(type: Javadoc) {
  // Specify your Javadoc options here
  // After the javadoc is generated, also generate the service-worker
  doLast {
    try {
      logger.lifecycle("Installing sw-precache globally")
      exec {
        commandLine "npm", "install", "-g", "sw-precache"
      exec {
        commandLine "sw-precache", "--config=sw-precache-config.json"
      // Even though there is an option ignoreExitValue for exec, exec keeps on throwing exceptions.
      // Therefore we just have to catch them and continue on with the build.
    } catch(Exception ignored) {
      logger.lifecycle("NPM is not installed. Aborting generating service-worker");

An example sw-precache-config.json for the javadoc is:

  "staticFileGlobs": [
  "stripPrefix": "build/javadoc/",
  "swFile": "build/javadoc/service-worker.js"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment