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

Code before:
    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);
            });
    return builder.build();
  }

  private static List<FileEntriesLayer> getDependenciesLayers(
      Path jarPath, AbsoluteUnixPath pathOnContainer) throws IOException {
    List<FileEntriesLayer> layers = new ArrayList<>();
    String classPath = null;

    // 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() == null ? Paths.get("") : jarPath.getParent();
      if (!nonSnapshotDependencies.isEmpty()) {
        FileEntriesLayer.Builder nonSnapshotDependenciesLayerBuilder =
            FileEntriesLayer.builder().setName(DEPENDENCIES);
        nonSnapshotDependencies.forEach(
            path ->
                nonSnapshotDependenciesLayerBuilder.addEntry(
                    jarParent.resolve(path), pathOnContainer.resolve(path)));
        layers.add(nonSnapshotDependenciesLayerBuilder.build());
      }
      if (!snapshotDependencies.isEmpty()) {
        FileEntriesLayer.Builder snapshotDependenciesLayerBuilder =
            FileEntriesLayer.builder().setName(SNAPSHOT_DEPENDENCIES);
        snapshotDependencies.forEach(
            path ->
                snapshotDependenciesLayerBuilder.addEntry(

Review comment (refactor):
Now that we have refactored out this logic to this method, short-circuiting is possible, and I think it helps readability. And now I think it should be fine to expand the `try` block until the end. For example,

```suggestion
    try (JarFile jarFile = new JarFile(jarPath.toFile())) {
      String classPath = jarFile.getManifest().getMainAttributes().getValue(Attributes.Name.CLASS_PATH);
      if (classPath == null) {
        return new ArrayList<>();
      }

      List<FileEntriesLayer> layers = new ArrayList<>();
      ...
    }
```

Code after:
        List<Path> nonSnapshots =
            allDependencies
                .stream()
                .filter(isSnapshot.negate())
                .map(Paths::get)
                .collect(Collectors.toList());
        List<Path> snapshots =
            allDependencies
                .stream()
                .filter(isSnapshot)
                .map(Paths::get)
                .collect(Collectors.toList());
        if (!nonSnapshots.isEmpty()) {
          FileEntriesLayer.Builder nonSnapshotLayer =
              FileEntriesLayer.builder().setName(DEPENDENCIES);
          nonSnapshots.forEach(
              path ->
                  addDependency(
                      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));
    }