Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add 'excludes' tag support for spring-boot-maven-plugin #173

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version = 0.1.0-SNAPSHOT
version = 0.1.1
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@
import com.google.cloud.tools.jib.plugins.extension.JibPluginExtensionException;
import com.google.common.annotations.VisibleForTesting;
import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import org.apache.maven.model.Plugin;
Expand All @@ -54,58 +52,70 @@ public ContainerBuildPlan extendContainerBuildPlan(
throws JibPluginExtensionException {
logger.log(LogLevel.LIFECYCLE, "Running Jib Spring Boot extension");

if (!shouldExcludeDevtools(mavenData.getMavenProject(), logger)) {
logger.log(LogLevel.INFO, "Keeping spring-boot-devtools (if any)");
List<String> exclusions = excludeDependencies(mavenData.getMavenProject(), logger);
if (exclusions == null || exclusions.isEmpty()) {
logger.log(LogLevel.INFO, "Keeping dependencies (if any)");
return buildPlan;
}
logger.log(LogLevel.INFO, "Removing spring-boot-devtools (if any)");
logger.log(LogLevel.INFO, "Removing dependencies (if any)");

List<LayerObject> newLayers =
buildPlan
.getLayers()
.stream()
.map(JibSpringBootExtension::filterOutDevtools)
buildPlan.getLayers().stream()
.map(layerObject -> filterOutDependencies(layerObject, exclusions))
.collect(Collectors.toList());
return buildPlan.toBuilder().setLayers(newLayers).build();
}

@VisibleForTesting
static boolean isDevtoolsJar(File file) {
return file.getName().startsWith("spring-boot-devtools-") && file.getName().endsWith(".jar");
static boolean isDependencyJar(File file, List<String> exclusions) {
return exclusions.stream().anyMatch(e -> file.getName().startsWith(e)) && file.getName().endsWith(".jar");
}

@VisibleForTesting
static boolean shouldExcludeDevtools(MavenProject project, ExtensionLogger logger) {
static List<String> excludeDependencies(MavenProject project, ExtensionLogger logger) {
List<String> exclusions = new ArrayList<>();

Plugin bootPlugin = project.getPlugin("org.springframework.boot:spring-boot-maven-plugin");
if (bootPlugin == null) {
logger.log(
LogLevel.WARN,
"Jib Spring Boot extension: project doesn't have spring-boot-maven-plugin?");
return true;
return exclusions;
}

Xpp3Dom configuration = (Xpp3Dom) bootPlugin.getConfiguration();
if (configuration != null) {
Xpp3Dom excludeDevtools = configuration.getChild("excludeDevtools");
if (excludeDevtools != null) {
return "true".equalsIgnoreCase(excludeDevtools.getValue());
if ("true".equalsIgnoreCase(excludeDevtools.getValue())) {
exclusions.add("spring-boot-devtools-");
}
}
Xpp3Dom excludes = configuration.getChild("excludes");
if (excludes != null && excludes.getChildCount() > 0) {
for (Xpp3Dom child:excludes.getChildren()) {
exclusions.add(String.format("%s-", child.getChild("artifactId").getValue()));
}
}
}
return true; // Spring Boot's <excludeDevtools> default is true.

// no dependencies or devtools need to be excluded
return exclusions;
}

@VisibleForTesting
static LayerObject filterOutDevtools(LayerObject layerObject) {
String dependencyLayerName = JavaContainerBuilder.LayerType.DEPENDENCIES.getName();
if (!dependencyLayerName.equals(layerObject.getName())) {
static LayerObject filterOutDependencies(LayerObject layerObject, List<String> exclusions) {
// skip non-dependencies and non-project_dependencies layers
if (!Objects.equals(JavaContainerBuilder.LayerType.DEPENDENCIES.getName(), layerObject.getName())
&& !Objects.equals(JavaContainerBuilder.LayerType.PROJECT_DEPENDENCIES.getName(), layerObject.getName())) {
return layerObject;
}

FileEntriesLayer layer = (FileEntriesLayer) layerObject;
Predicate<FileEntry> notDevtoolsJar =
fileEntry -> !isDevtoolsJar(fileEntry.getSourceFile().toFile());
Predicate<FileEntry> notDependencyJar =
fileEntry -> !isDependencyJar(fileEntry.getSourceFile().toFile(), exclusions);
List<FileEntry> newEntries =
layer.getEntries().stream().filter(notDevtoolsJar).collect(Collectors.toList());
layer.getEntries().stream().filter(notDependencyJar).collect(Collectors.toList());
return layer.toBuilder().setEntries(newEntries).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
Expand Down Expand Up @@ -66,9 +67,7 @@ private static FileEntriesLayer buildLayer(String layerName, Path... paths) {
}

private static List<String> layerToExtractionPaths(FileEntriesLayer layer) {
return layer
.getEntries()
.stream()
return layer.getEntries().stream()
.map(layerEntry -> layerEntry.getExtractionPath().toString())
.collect(Collectors.toList());
}
Expand All @@ -81,24 +80,30 @@ public void setUp() {
@Test
public void testIsDevtoolsJar() {
File file = Paths.get("sub", "folder", "spring-boot-devtools-1.2.3-SNAPSHOT.jar").toFile();
assertTrue(JibSpringBootExtension.isDevtoolsJar(file));
List<String> exclusions = new ArrayList<>();
exclusions.add("spring-boot-devtools-");
assertTrue(JibSpringBootExtension.isDependencyJar(file, exclusions));
}

@Test
public void testIsDevtoolsJar_noJarExtension() {
File file = Paths.get("sub", "folder", "spring-boot-devtools-1.2.3-SNAPSHOT").toFile();
assertFalse(JibSpringBootExtension.isDevtoolsJar(file));
List<String> exclusions = new ArrayList<>();
assertFalse(JibSpringBootExtension.isDependencyJar(file, exclusions));
}

@Test
public void testIsDevtoolsJar_differentJar() {
File file = Paths.get("sub", "folder", "not-spring-boot-devtools-1.2.3-SNAPSHOT.jar").toFile();
assertFalse(JibSpringBootExtension.isDevtoolsJar(file));
List<String> exclusions = new ArrayList<>();
exclusions.add("spring-boot-devtools-");
assertFalse(JibSpringBootExtension.isDependencyJar(file, exclusions));
}

@Test
public void testShouldExcludeDevtools_noSpringBootPlugin() {
assertTrue(JibSpringBootExtension.shouldExcludeDevtools(project, logger));
List<String> exclusions = new ArrayList<>();
assertEquals(exclusions, JibSpringBootExtension.excludeDependencies(project, logger));

verify(logger)
.log(
Expand All @@ -111,7 +116,8 @@ public void testShouldExcludeDevtools_trueByDefault() {
when(project.getPlugin("org.springframework.boot:spring-boot-maven-plugin"))
.thenReturn(bootPlugin);

assertTrue(JibSpringBootExtension.shouldExcludeDevtools(project, logger));
List<String> exclusions = new ArrayList<>();
assertEquals(exclusions, JibSpringBootExtension.excludeDependencies(project, logger));
}

@Test
Expand All @@ -125,7 +131,8 @@ public void testShouldExcludeDevtools_false() {
.thenReturn(bootPlugin);
when(bootPlugin.getConfiguration()).thenReturn(configuration);

assertFalse(JibSpringBootExtension.shouldExcludeDevtools(project, logger));
List<String> exclusions = new ArrayList<>();
assertEquals(exclusions, JibSpringBootExtension.excludeDependencies(project, logger));
}

@Test
Expand All @@ -139,7 +146,9 @@ public void testShouldExcludeDevtools_true() {
.thenReturn(bootPlugin);
when(bootPlugin.getConfiguration()).thenReturn(configuration);

assertTrue(JibSpringBootExtension.shouldExcludeDevtools(project, logger));
List<String> exclusions = new ArrayList<>();
exclusions.add("spring-boot-devtools-");
assertEquals(exclusions, JibSpringBootExtension.excludeDependencies(project, logger));
}

@Test
Expand All @@ -150,7 +159,9 @@ public void testFilterOutDevtools() {
Paths.get("static").resolve("foo.txt"),
Paths.get("lib").resolve("spring-boot-devtools-1.2.3.jar"),
Paths.get("archive").resolve("bar.zip"));
FileEntriesLayer filtered = (FileEntriesLayer) JibSpringBootExtension.filterOutDevtools(layer);
List<String> exclusions = new ArrayList<>();
exclusions.add("spring-boot-devtools-");
FileEntriesLayer filtered = (FileEntriesLayer) JibSpringBootExtension.filterOutDependencies(layer, exclusions);

assertEquals(Arrays.asList("/dest/foo.txt", "/dest/bar.zip"), layerToExtractionPaths(filtered));
}
Expand All @@ -162,7 +173,9 @@ public void testFilterOutDevtools_differentDependencyLayerName() {
"NOT dependencies",
Paths.get("lib").resolve("spring-boot-devtools-1.2.3.jar"),
Paths.get("archive").resolve("bar.zip"));
LayerObject newLayer = JibSpringBootExtension.filterOutDevtools(layer);
List<String> exclusions = new ArrayList<>();
exclusions.add("spring-boot-devtools-");
LayerObject newLayer = JibSpringBootExtension.filterOutDependencies(layer, exclusions);
assertSame(layer, newLayer);
assertEquals(layer.getEntries(), ((FileEntriesLayer) newLayer).getEntries());
}
Expand Down Expand Up @@ -190,11 +203,12 @@ public void testExtendContainerBuildPlan_devtoolsFiltered() throws JibPluginExte
FileEntriesLayer newLayer1 = (FileEntriesLayer) newPlan.getLayers().get(0);
FileEntriesLayer newLayer2 = (FileEntriesLayer) newPlan.getLayers().get(1);

assertEquals(Arrays.asList("/dest/bar.zip"), layerToExtractionPaths(newLayer1));
assertEquals(Arrays.asList("/dest/spring-boot-devtools-1.2.3.jar", "/dest/bar.zip"),
layerToExtractionPaths(newLayer1));
assertEquals(
Arrays.asList("/dest/spring-boot-devtools-1.2.3.jar"), layerToExtractionPaths(newLayer2));

verify(logger).log(LogLevel.INFO, "Removing spring-boot-devtools (if any)");
verify(logger).log(LogLevel.INFO, "Keeping dependencies (if any)");
}

@Test
Expand Down Expand Up @@ -224,6 +238,6 @@ public void testExtendContainerBuildPlan_noFiltering() throws JibPluginExtension
.extendContainerBuildPlan(buildPlan, null, Optional.empty(), mavenData, logger);
assertSame(buildPlan, newPlan);

verify(logger).log(LogLevel.INFO, "Keeping spring-boot-devtools (if any)");
verify(logger).log(LogLevel.INFO, "Keeping dependencies (if any)");
}
}