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

Code before:
            .setName("Classes")
            .build();
    FileEntriesLayer resourcesLayer =
        addDirectoryContentsToLayer(
                FileEntriesLayer.builder(),
                localExplodedJarRoot,
                isResourceFile,
                APP_ROOT.resolve(RelativeUnixPath.get("explodedJar")))
            .setName("Resources")
            .build();

    // Get dependencies from Class-Path in the jar's manifest and create a FileEntriesLayer.Builder
    // with these dependencies as entries.
    List<Path> dependencies;
    JarFile jarFile = new JarFile(jarPath.toFile());
    String classPath =
        jarFile.getManifest().getMainAttributes().getValue(Attributes.Name.CLASS_PATH);
    if (classPath != null) {
      dependencies =
          Splitter.onPattern("\\s+")
              .splitToList(classPath.trim())
              .stream()
              .map(Paths::get)
              .collect(Collectors.toList());
    } else {
      dependencies = new ArrayList<>();
    }
    FileEntriesLayer.Builder dependenciesLayerBuilder = FileEntriesLayer.builder();
    dependencies.forEach(
        path ->
            dependenciesLayerBuilder.addEntry(
                path, APP_ROOT.resolve(RelativeUnixPath.get("dependencies")).resolve(path)));
    dependenciesLayerBuilder.setName("Dependencies");

    layers.add(dependenciesLayerBuilder.build());
    layers.add(resourcesLayer);
    layers.add(classesLayer);

    return layers;
  }

  private static FileEntriesLayer.Builder addDirectoryContentsToLayer(
      FileEntriesLayer.Builder builder,
      Path sourceRoot,
      Predicate<Path> pathFilter,
      AbsoluteUnixPath basePathInContainer)
      throws IOException {
    new DirectoryWalker(sourceRoot)
        .filterRoot()
        .filter(path -> Files.isDirectory(path) || pathFilter.test(path))
        .walk(

Review comment (refactor):
You can remove this `else` branch if you initialize the variable on line 118 like `List<Path> dependencies = new ArrayList<>();`.

How about skipping adding the dependencies layer if it's empty? To be fair, it shouldn't matter, because I believe jib-core will drop any empty layers. But we can be conservative at this level too.

Code after:

    Predicate<Path> isClassFile = path -> path.getFileName().toString().endsWith(".class");
    Predicate<Path> isResourceFile = isClassFile.negate();

    // Determine class and resource files in the directory containing jar contents and create
    // FileEntriesLayer for each type of layer (classes or resources), while maintaining the
    // file's original project structure.
    FileEntriesLayer classesLayer =
        addDirectoryContentsToLayer(
            "classes",
            localExplodedJarRoot,
            isClassFile,
            APP_ROOT.resolve(RelativeUnixPath.get("explodedJar")));
    FileEntriesLayer resourcesLayer =
        addDirectoryContentsToLayer(
            "resources",
            localExplodedJarRoot,
            isResourceFile,
            APP_ROOT.resolve(RelativeUnixPath.get("explodedJar")));

    layers.add(resourcesLayer);
    layers.add(classesLayer);
    return layers;
  }

  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();
  }
}