private static void writeJsonTemplate(JsonTemplate jsonTemplate, Path destination)
      throws IOException {
    Path temporaryFile = Files.createTempFile(destination.getParent(), null, null);
    temporaryFile.toFile().deleteOnExit();
    try (OutputStream outputStream = Files.newOutputStream(temporaryFile)) {
      JsonTemplateMapper.writeTo(jsonTemplate, outputStream);
    }

    // Attempts an atomic move first, and falls back to non-atomic if the file system does not
    // support atomic moves.
    try {
      Files.move(
          temporaryFile,
          destination,
          StandardCopyOption.ATOMIC_MOVE,
          StandardCopyOption.REPLACE_EXISTING);

    } catch (AtomicMoveNotSupportedException ignored) {
      Files.move(temporaryFile, destination, StandardCopyOption.REPLACE_EXISTING);
    }
  }
Writes a json template to the destination path by writing to a temporary file then moving the
file.

@param jsonTemplate the json template
@param destination the destination path
@throws IOException if an I/O exception occurs
jib-core/src/main/java/com/google/cloud/tools/jib/cache/CacheStorageWriter.java