Repository: GoogleContainerTools/jib
File: jib-cli/src/main/java/com/google/cloud/tools/jib/cli/jar/JarModeProcessor.java (Java)

Code before:

    // Get dependencies from Class-Path in the jar's manifest and add a layer each for non-snapshot
    // and snapshot dependencies. If Class-Path is not present in the jar's manifest then skip
    // adding the dependencies layers.
    try (JarFile jarFile = new JarFile(jarPath.toFile())) {
      classPath = jarFile.getManifest().getMainAttributes().getValue(Attributes.Name.CLASS_PATH);
    }
    if (classPath != null) {
      Predicate<String> isSnapshot = name -> name.contains("SNAPSHOT");
      List<String> allDependencies = Splitter.onPattern("\\s+").splitToList(classPath.trim());
      List<Path> nonSnapshotDependencies =
          allDependencies
              .stream()
              .filter(isSnapshot.negate())
              .map(Paths::get)
              .collect(Collectors.toList());
      List<Path> snapshotDependencies =
          allDependencies.stream().filter(isSnapshot).map(Paths::get).collect(Collectors.toList());
      Path jarParent = jarPath.getParent();
      if (!nonSnapshotDependencies.isEmpty()) {
        FileEntriesLayer.Builder nonSnapshotDependenciesLayerBuilder =
            FileEntriesLayer.builder().setName(DEPENDENCIES);
        nonSnapshotDependencies.forEach(
            path ->
                nonSnapshotDependenciesLayerBuilder.addEntry(
                    jarParent.resolve(path),
                    APP_ROOT.resolve(RelativeUnixPath.get("dependencies")).resolve(path)));
        layers.add(nonSnapshotDependenciesLayerBuilder.build());
      }
      if (!snapshotDependencies.isEmpty()) {
        FileEntriesLayer.Builder snapshotDependenciesLayerBuilder =
            FileEntriesLayer.builder().setName(SNAPSHOT_DEPENDENCIES);
        snapshotDependencies.forEach(
            path ->
                snapshotDependenciesLayerBuilder.addEntry(
                    jarParent.resolve(path),
                    APP_ROOT.resolve(RelativeUnixPath.get("dependencies")).resolve(path)));
        layers.add(snapshotDependenciesLayerBuilder.build());
      }
    }
    return layers;
  }
}


Review comment (bug):
I just tried my `C.jar` example, but it throws an NPE. In this case, `jarPath` is simply `C.jar` (not an absolute path) and `jarPath.getParent()` returns null.

```
~/A$ jib --target=docker://foo --stacktrace jar C.jar
java.lang.NullPointerException
        at com.google.cloud.tools.jib.cli.jar.JarModeProcessor.lambda$getDependenciesLayers$6(JarModeProcessor.java:236)
        at java.util.ArrayList.forEach(ArrayList.java:1257)
        at com.google.cloud.tools.jib.cli.jar.JarModeProcessor.getDependenciesLayers(JarModeProcessor.java:233)
```

Maybe it can be
```java
      Path jarParent = jarPath.getParent() == null ? Paths.get("") : jarPath.getParent();
```

Code after:
                      nonSnapshotLayer,
                      jarParent.resolve(path),
                      mode.equals(ProcessingMode.packaged)
                          ? APP_ROOT.resolve(path)
                          : APP_ROOT.resolve("dependencies").resolve(path.getFileName())));
          layers.add(nonSnapshotLayer.build());
        }
        if (!snapshots.isEmpty()) {
          FileEntriesLayer.Builder snapshotLayer =
              FileEntriesLayer.builder().setName(SNAPSHOT_DEPENDENCIES);
          snapshots.forEach(
              path ->
                  addDependency(
                      snapshotLayer,
                      jarParent.resolve(path),
                      mode.equals(ProcessingMode.packaged)
                          ? APP_ROOT.resolve(path)
                          : APP_ROOT.resolve("dependencies").resolve(path.getFileName())));
          layers.add(snapshotLayer.build());
        }
        return layers;
      }
    }
  }

  private static void addDependency(
      FileEntriesLayer.Builder layerbuilder, Path fullDepPath, AbsoluteUnixPath pathOnContainer) {
    if (!Files.exists(fullDepPath)) {
      throw new IllegalArgumentException(
          String.format(
              "Dependency required by the JAR (as specified in `Class-Path` in the JAR manifest) doesn't exist: %s",
              fullDepPath));
    }
    layerbuilder.addEntry(fullDepPath, pathOnContainer);
  }

  private static FileEntriesLayer addDirectoryContentsToLayer(
      String layerName,
      Path sourceRoot,
      Predicate<Path> pathFilter,
      AbsoluteUnixPath basePathInContainer)
      throws IOException {
    FileEntriesLayer.Builder builder = FileEntriesLayer.builder().setName(layerName);
    new DirectoryWalker(sourceRoot)
        .filterRoot()
        .filter(path -> Files.isDirectory(path) || pathFilter.test(path))
        .walk(
            path -> {
              AbsoluteUnixPath pathOnContainer =
                  basePathInContainer.resolve(sourceRoot.relativize(path));
              builder.addEntry(path, pathOnContainer);