Repository: GoogleContainerTools/jib
File: jib-core/src/main/java/com/google/cloud/tools/jib/api/JavaContainerBuilder.java (Java)

Code before:
   * @param pathInContainer path in container.
   */
  public static void addFileToLayer(
      Map<LayerType, FileEntriesLayer.Builder> layerBuilders,
      LayerType layerType,
      Path sourceFile,
      AbsoluteUnixPath pathInContainer) {
    if (!layerBuilders.containsKey(layerType)) {
      layerBuilders.put(layerType, FileEntriesLayer.builder());
    }
    Instant modificationTime = modificationTimeProvider.get(sourceFile, pathInContainer);
    layerBuilders.get(layerType).addEntry(sourceFile, pathInContainer, modificationTime);
  }

  /**
   * Adds the contents of a directory to a {@link FileEntriesLayer.Builder}.
   *
   * @param layerBuilders map that keeps track of all layers, with the key being the layer type and
   *     value being a {@link FileEntriesLayer.Builder}.
   * @param layerType the layer type.
   * @param sourceRoot path to root directory.
   * @param pathFilter predicate to filter the files by.
   * @param basePathInContainer base path in container.
   * @throws IOException if I/O error occurs when opening the directory.
   */
  public static void addDirectoryContentsToLayer(
      Map<LayerType, FileEntriesLayer.Builder> layerBuilders,
      LayerType layerType,
      Path sourceRoot,
      Predicate<Path> pathFilter,
      AbsoluteUnixPath basePathInContainer)
      throws IOException {
    if (!layerBuilders.containsKey(layerType)) {
      layerBuilders.put(layerType, FileEntriesLayer.builder());
    }
    FileEntriesLayer.Builder builder = layerBuilders.get(layerType);

    new DirectoryWalker(sourceRoot)
        .filterRoot()
        .filter(path -> Files.isDirectory(path) || pathFilter.test(path))
        .walk(
            path -> {
              AbsoluteUnixPath pathOnContainer =
                  basePathInContainer.resolve(sourceRoot.relativize(path));
              Instant modificationTime = modificationTimeProvider.get(path, pathOnContainer);
              builder.addEntry(path, pathOnContainer, modificationTime);
            });
  }
}


Review comment (refactor):
Should remain an instance method.

And note `JavaContainerBuilder` is a public API. Probably we don't want to expose this internal helper method.

Just asking: did we decide to use `JavaContainerBuilder` instead of `JibContainerBuilder` for some reason? It's fine if `JavaContainerBuilder` works, but just saying I believe it's a bit curated and limiting.

Code after:
    }
    Instant modificationTime = modificationTimeProvider.get(sourceFile, pathInContainer);
    layerBuilders.get(layerType).addEntry(sourceFile, pathInContainer, modificationTime);
  }

  private void addDirectoryContentsToLayer(
      Map<LayerType, FileEntriesLayer.Builder> layerBuilders,
      LayerType layerType,
      Path sourceRoot,
      Predicate<Path> pathFilter,
      AbsoluteUnixPath basePathInContainer)
      throws IOException {
    if (!layerBuilders.containsKey(layerType)) {
      layerBuilders.put(layerType, FileEntriesLayer.builder());
    }
    FileEntriesLayer.Builder builder = layerBuilders.get(layerType);

    new DirectoryWalker(sourceRoot)
        .filterRoot()
        .filter(path -> Files.isDirectory(path) || pathFilter.test(path))
        .walk(
            path -> {
              AbsoluteUnixPath pathOnContainer =
                  basePathInContainer.resolve(sourceRoot.relativize(path));
              Instant modificationTime = modificationTimeProvider.get(path, pathOnContainer);
              builder.addEntry(path, pathOnContainer, modificationTime);
            });
  }
}