Skip to content

Instantly share code, notes, and snippets.

@TimvdLippe
Created September 10, 2016 11:18
Show Gist options
  • Save TimvdLippe/4c39b99e3b0ffbcdd8814a31e2969ed1 to your computer and use it in GitHub Desktop.
Save TimvdLippe/4c39b99e3b0ffbcdd8814a31e2969ed1 to your computer and use it in GitHub Desktop.
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": [
    "build/javadoc/index-files/*.html",
    "build/javadoc/js/*.js",
    "build/javadoc/**/*.html",
    "build/javadoc/*.html",
    "build/javadoc/favicon.ico"
  ],
  "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