Stop developmentOnly from removing too much from executable jars and wars

Fixes gh-21288
pull/21361/head
Andy Wilkinson 5 years ago
parent 66e8968b98
commit 1bc41ec336

@ -94,8 +94,13 @@ final class JavaPluginAction implements PluginApplicationAction {
bootJar.setGroup(BasePlugin.BUILD_GROUP);
SourceSet mainSourceSet = javaPluginConvention(project).getSourceSets()
.getByName(SourceSet.MAIN_SOURCE_SET_NAME);
bootJar.classpath((Callable<FileCollection>) () -> mainSourceSet.getRuntimeClasspath().minus(
project.getConfigurations().getByName(SpringBootPlugin.DEVELOPMENT_ONLY_CONFIGURATION_NAME)));
bootJar.classpath((Callable<FileCollection>) () -> {
Configuration developmentOnly = project.getConfigurations()
.getByName(SpringBootPlugin.DEVELOPMENT_ONLY_CONFIGURATION_NAME);
Configuration productionRuntimeClasspath = project.getConfigurations()
.getByName(SpringBootPlugin.PRODUCTION_RUNTIME_CLASSPATH_NAME);
return mainSourceSet.getRuntimeClasspath().minus((developmentOnly.minus(productionRuntimeClasspath)));
});
bootJar.conventionMapping("mainClassName", new MainClassConvention(project, bootJar::getClasspath));
});
}
@ -165,8 +170,13 @@ final class JavaPluginAction implements PluginApplicationAction {
.create(SpringBootPlugin.DEVELOPMENT_ONLY_CONFIGURATION_NAME);
developmentOnly
.setDescription("Configuration for development-only dependencies such as Spring Boot's DevTools.");
project.getConfigurations().getByName(JavaPlugin.RUNTIME_CLASSPATH_CONFIGURATION_NAME)
.extendsFrom(developmentOnly);
Configuration runtimeClasspath = project.getConfigurations()
.getByName(JavaPlugin.RUNTIME_CLASSPATH_CONFIGURATION_NAME);
Configuration productionRuntimeClasspath = project.getConfigurations()
.create(SpringBootPlugin.PRODUCTION_RUNTIME_CLASSPATH_NAME);
productionRuntimeClasspath.setVisible(false);
productionRuntimeClasspath.setExtendsFrom(runtimeClasspath.getExtendsFrom());
runtimeClasspath.extendsFrom(developmentOnly);
}
/**

@ -76,6 +76,8 @@ public class SpringBootPlugin implements Plugin<Project> {
*/
public static final String DEVELOPMENT_ONLY_CONFIGURATION_NAME = "developmentOnly";
static final String PRODUCTION_RUNTIME_CLASSPATH_NAME = "productionRuntimeClasspath";
/**
* The coordinates {@code (group:name:version)} of the
* {@code spring-boot-dependencies} bom.

@ -19,6 +19,7 @@ package org.springframework.boot.gradle.plugin;
import org.gradle.api.Action;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.ConfigurationContainer;
import org.gradle.api.file.FileCollection;
import org.gradle.api.internal.artifacts.dsl.LazyPublishArtifact;
@ -64,8 +65,11 @@ class WarPluginAction implements PluginApplicationAction {
bootWar.setDescription("Assembles an executable war archive containing webapp"
+ " content, and the main classes and their dependencies.");
bootWar.providedClasspath(providedRuntimeConfiguration(project));
bootWar.setClasspath(bootWar.getClasspath().minus(
project.getConfigurations().getByName(SpringBootPlugin.DEVELOPMENT_ONLY_CONFIGURATION_NAME)));
Configuration developmentOnly = project.getConfigurations()
.getByName(SpringBootPlugin.DEVELOPMENT_ONLY_CONFIGURATION_NAME);
Configuration productionRuntimeClasspath = project.getConfigurations()
.getByName(SpringBootPlugin.PRODUCTION_RUNTIME_CLASSPATH_NAME);
bootWar.setClasspath(bootWar.getClasspath().minus((developmentOnly.minus(productionRuntimeClasspath))));
bootWar.conventionMapping("mainClassName", new MainClassConvention(project, bootWar::getClasspath));
});
}

@ -46,11 +46,14 @@ abstract class AbstractBootArchiveIntegrationTests {
private final String libPath;
private final String classesPath;
GradleBuild gradleBuild;
protected AbstractBootArchiveIntegrationTests(String taskName, String libPath) {
protected AbstractBootArchiveIntegrationTests(String taskName, String libPath, String classesPath) {
this.taskName = taskName;
this.libPath = libPath;
this.classesPath = classesPath;
}
@TestTemplate
@ -142,12 +145,18 @@ abstract class AbstractBootArchiveIntegrationTests {
@TestTemplate
void developmentOnlyDependenciesAreNotIncludedInTheArchiveByDefault() throws IOException {
File srcMainResources = new File(this.gradleBuild.getProjectDir(), "src/main/resources");
srcMainResources.mkdirs();
new File(srcMainResources, "resource").createNewFile();
assertThat(this.gradleBuild.build(this.taskName).task(":" + this.taskName).getOutcome())
.isEqualTo(TaskOutcome.SUCCESS);
try (JarFile jarFile = new JarFile(new File(this.gradleBuild.getProjectDir(), "build/libs").listFiles()[0])) {
Stream<String> libEntryNames = jarFile.stream().filter((entry) -> !entry.isDirectory())
.map(JarEntry::getName).filter((name) -> name.startsWith(this.libPath));
assertThat(libEntryNames).containsExactly(this.libPath + "commons-io-2.6.jar");
Stream<String> classesEntryNames = jarFile.stream().filter((entry) -> !entry.isDirectory())
.map(JarEntry::getName).filter((name) -> name.startsWith(this.classesPath));
assertThat(classesEntryNames).containsExactly(this.classesPath + "resource");
}
}

@ -58,7 +58,7 @@ import static org.assertj.core.api.Assertions.assertThat;
class BootJarIntegrationTests extends AbstractBootArchiveIntegrationTests {
BootJarIntegrationTests() {
super("bootJar", "BOOT-INF/lib/");
super("bootJar", "BOOT-INF/lib/", "BOOT-INF/classes/");
}
@TestTemplate

@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -24,7 +24,7 @@ package org.springframework.boot.gradle.tasks.bundling;
class BootWarIntegrationTests extends AbstractBootArchiveIntegrationTests {
BootWarIntegrationTests() {
super("bootWar", "WEB-INF/lib/");
super("bootWar", "WEB-INF/lib/", "WEB-INF/classes/");
}
}

@ -13,5 +13,6 @@ repositories {
dependencies {
developmentOnly("org.apache.commons:commons-lang3:3.9")
developmentOnly("commons-io:commons-io:2.6")
implementation("commons-io:commons-io:2.6")
}

@ -13,5 +13,6 @@ repositories {
dependencies {
developmentOnly("org.apache.commons:commons-lang3:3.9")
developmentOnly("commons-io:commons-io:2.6")
implementation("commons-io:commons-io:2.6")
}

Loading…
Cancel
Save