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

Code before:
    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());
      if (!nonSnapshotDependencies.isEmpty()) {
        FileEntriesLayer.Builder nonSnapshotDependenciesLayerBuilder =
            FileEntriesLayer.builder().setName(DEPENDENCIES);
        nonSnapshotDependencies.forEach(
            path ->
                nonSnapshotDependenciesLayerBuilder.addEntry(
                    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(
                    path, APP_ROOT.resolve(RelativeUnixPath.get("dependencies")).resolve(path)));
        layers.add(snapshotDependenciesLayerBuilder.build());
      }
    }
    return layers;
  }
}


Review comment (bug):
I missed this. `jib jar` will fail if running from a different directory (e.g., `jib ... jar ../my.jar`). Perhaps the first argument to `addEntry` should be ~~`jarPath.resolve(path)`~~?

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