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

Code before:
import com.google.cloud.tools.jib.plugins.common.ZipUtil;
import com.google.common.base.Predicates;
import com.google.common.base.Verify;
import com.google.common.collect.ImmutableList;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;
import java.util.jar.JarFile;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
import javax.annotation.Nullable;

public class SpringBootExplodedProcessor implements JarProcessor {

  @Nullable private final Path jarPath;
  @Nullable private final Path targetDirectoryPath;

  public SpringBootExplodedProcessor(Path jarPath, Path targetDirectoryPath) {
    this.jarPath = jarPath;
    this.targetDirectoryPath = targetDirectoryPath;
  }

  @Override
  public List<FileEntriesLayer> createLayers() throws IOException {
    if (targetDirectoryPath == null || jarPath == null) {
      return new ArrayList<>();
    }
    try (JarFile jarFile = new JarFile(jarPath.toFile())) {
      Path localExplodedJarRoot = targetDirectoryPath;
      ZipUtil.unzip(jarPath, localExplodedJarRoot, true);
      ZipEntry layerIndex = jarFile.getEntry("BOOT-INF/layers.idx");
      if (layerIndex != null) {
        return createLayersForLayeredSpringBootJar(localExplodedJarRoot);
      }

      Predicate<Path> isFile = Files::isRegularFile;

      // Non-snapshot layer
      Predicate<Path> isInBootInfLib =
          path -> path.startsWith(localExplodedJarRoot.resolve("BOOT-INF").resolve("lib"));
      Predicate<Path> isSnapshot = path -> path.getFileName().toString().contains("SNAPSHOT");
      Predicate<Path> isInBootInfLibAndIsNotSnapshot = isInBootInfLib.and(isSnapshot.negate());
      Predicate<Path> nonSnapshotPredicate = isFile.and(isInBootInfLibAndIsNotSnapshot);

Review comment (suggestion):
It's difficult to understand what `targetDirectoryPath` means. `targetExplodedJarRoot`? And maybe we can remove `localExplodedJarRoot` below.

Code after:
import com.google.cloud.tools.jib.plugins.common.ZipUtil;
import com.google.common.base.Predicates;
import com.google.common.base.Verify;
import com.google.common.collect.ImmutableList;
import com.google.common.io.MoreFiles;
import com.google.common.io.RecursiveDeleteOption;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;
import java.util.jar.JarFile;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.ZipEntry;

public class SpringBootExplodedProcessor implements JarProcessor {

  private final Path jarPath;
  private final Path targetExplodedJarRoot;

  public SpringBootExplodedProcessor(Path jarPath, Path targetExplodedJarRoot) {
    this.jarPath = jarPath;
    this.targetExplodedJarRoot = targetExplodedJarRoot;
  }

  @Override
  public List<FileEntriesLayer> createLayers() throws IOException {
    // Clear the exploded-jar root first
    if (Files.exists(targetExplodedJarRoot)) {
      MoreFiles.deleteRecursively(targetExplodedJarRoot, RecursiveDeleteOption.ALLOW_INSECURE);
    }

    try (JarFile jarFile = new JarFile(jarPath.toFile())) {
      ZipUtil.unzip(jarPath, targetExplodedJarRoot, true);
      ZipEntry layerIndex = jarFile.getEntry("BOOT-INF/layers.idx");
      if (layerIndex != null) {
        return createLayersForLayeredSpringBootJar(targetExplodedJarRoot);
      }

      Predicate<Path> isFile = Files::isRegularFile;

      // Non-snapshot layer
      Predicate<Path> isInBootInfLib =
          path -> path.startsWith(targetExplodedJarRoot.resolve("BOOT-INF").resolve("lib"));
      Predicate<Path> isSnapshot = path -> path.getFileName().toString().contains("SNAPSHOT");