diff --git a/first-party/jib-layer-filter-extension-maven/src/main/java/com/google/cloud/tools/jib/maven/extension/layerfilter/JibLayerFilterExtension.java b/first-party/jib-layer-filter-extension-maven/src/main/java/com/google/cloud/tools/jib/maven/extension/layerfilter/JibLayerFilterExtension.java index 14fc7f0..10097ff 100644 --- a/first-party/jib-layer-filter-extension-maven/src/main/java/com/google/cloud/tools/jib/maven/extension/layerfilter/JibLayerFilterExtension.java +++ b/first-party/jib-layer-filter-extension-maven/src/main/java/com/google/cloud/tools/jib/maven/extension/layerfilter/JibLayerFilterExtension.java @@ -54,13 +54,8 @@ @Singleton public class JibLayerFilterExtension implements JibMavenPluginExtension { - private Map pathMatchers = new LinkedHashMap<>(); - @VisibleForTesting @Inject ProjectDependenciesResolver dependencyResolver; - // (layer name, layer builder) map for new layers of configured - @VisibleForTesting Map newToLayers = new LinkedHashMap<>(); - @Override public Optional> getExtraConfigType() { return Optional.of(Configuration.class); @@ -80,7 +75,12 @@ public ContainerBuildPlan extendContainerBuildPlan( return buildPlan; } - preparePathMatchersAndLayerBuilders(buildPlan, config.get()); + Map pathMatchers = new LinkedHashMap<>(); + + // (layer name, layer builder) map for new layers of configured + Map newToLayers = new LinkedHashMap<>(); + + preparePathMatchersAndLayerBuilders(buildPlan, config.get(), pathMatchers, newToLayers); ContainerBuildPlan.Builder newPlanBuilder = buildPlan.toBuilder(); newPlanBuilder.setLayers(Collections.emptyList()); @@ -92,7 +92,8 @@ public ContainerBuildPlan extendContainerBuildPlan( List filesToKeep = new ArrayList<>(); for (FileEntry entry : layer.getEntries()) { - Optional finalLayerName = determineFinalLayerName(entry, layer.getName()); + Optional finalLayerName = + determineFinalLayerName(entry, layer.getName(), pathMatchers); // Either keep, move, or delete this FileEntry. if (finalLayerName.isPresent()) { if (finalLayerName.get().equals(layer.getName())) { @@ -235,11 +236,14 @@ private void logMissingParentDependencies( } private void preparePathMatchersAndLayerBuilders( - ContainerBuildPlan buildPlan, Configuration config) throws JibPluginExtensionException { + ContainerBuildPlan buildPlan, + Configuration config, + Map pathMatchers, + Map newToLayers) + throws JibPluginExtensionException { List originalLayerNames = buildPlan.getLayers().stream().map(LayerObject::getName).collect(Collectors.toList()); - newToLayers.clear(); // ensure empty (in case previously built module already populated it) for (Configuration.Filter filter : config.getFilters()) { String toLayerName = filter.getToLayer(); if (!toLayerName.isEmpty() && originalLayerNames.contains(toLayerName)) { @@ -271,7 +275,8 @@ private void preparePathMatchersAndLayerBuilders( * @return final layer name into which {@code fileEntry} should move. May be same as {@code * originalLayerName}. {@link Optional#empty()} indicates deletion. */ - private Optional determineFinalLayerName(FileEntry fileEntry, String originalLayerName) { + private Optional determineFinalLayerName( + FileEntry fileEntry, String originalLayerName, Map pathMatchers) { Optional finalLayerName = Optional.of(originalLayerName); for (Map.Entry mapEntry : pathMatchers.entrySet()) { diff --git a/first-party/jib-layer-filter-extension-maven/src/test/java/com/google/cloud/tools/jib/maven/extension/layerfilter/JibLayerFilterExtensionTest.java b/first-party/jib-layer-filter-extension-maven/src/test/java/com/google/cloud/tools/jib/maven/extension/layerfilter/JibLayerFilterExtensionTest.java index 4c16e88..f972a53 100644 --- a/first-party/jib-layer-filter-extension-maven/src/test/java/com/google/cloud/tools/jib/maven/extension/layerfilter/JibLayerFilterExtensionTest.java +++ b/first-party/jib-layer-filter-extension-maven/src/test/java/com/google/cloud/tools/jib/maven/extension/layerfilter/JibLayerFilterExtensionTest.java @@ -33,7 +33,6 @@ import com.google.cloud.tools.jib.plugins.extension.ExtensionLogger.LogLevel; import com.google.cloud.tools.jib.plugins.extension.JibPluginExtensionException; import java.nio.file.Paths; -import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; @@ -236,9 +235,6 @@ public void testExtendContainerBuildPlan_sameLayerNameInMultipleFilters() ContainerBuildPlan newPlan = extension.extendContainerBuildPlan(buildPlan, null, Optional.of(config), null, logger); - ArrayList layerNames = new ArrayList<>(extension.newToLayers.keySet()); - assertEquals(Arrays.asList("foo", "same layer name", "bar", "baz"), layerNames); - assertEquals(4, newPlan.getLayers().size()); FileEntriesLayer newLayer1 = (FileEntriesLayer) newPlan.getLayers().get(0); FileEntriesLayer newLayer2 = (FileEntriesLayer) newPlan.getLayers().get(1);