Automatically create developmentOnly configuration

Previously, the developmentOnly configuration, typically used for
Devtools, had to be declared manually. The BootJar and BootWar tasks
then had a property, excludeDevtools, that could be used to control
whether or not Devtools would be excluded from the executable archive.

This commit updates the reaction to the Java plugin being applied to
automatically create the developmentOnly configuration. The classpaths
of bootJar and bootWar are then configured not to include the contents
of the developmentOnly configuration. As a result of this, the
excludeDevtools property is no longer needed and has been deprecated.
Its default has also been changed from true to false to make it easy
to opt in to Devtools, when configured as a development-only
dependency, being included in executable jars and wars by adding
developmentOnly to the classpath of the archive task.

Closes gh-16599
This commit is contained in:
Andy Wilkinson
2020-04-27 12:00:08 +01:00
parent cbdc5d9746
commit fb33610027
21 changed files with 196 additions and 30 deletions

View File

@@ -139,6 +139,13 @@ class JavaPluginActionIntegrationTests {
assertThat(result.getOutput()).contains("compileJava compiler args: [-parameters]");
}
@TestTemplate
void applyingJavaPluginCreatesDevelopmentOnlyConfiguration() {
assertThat(this.gradleBuild
.build("configurationExists", "-PconfigurationName=developmentOnly", "-PapplyJavaPlugin").getOutput())
.contains("developmentOnly exists = true");
}
private void createMinimalMainSource() throws IOException {
File examplePackage = new File(this.gradleBuild.getProjectDir(), "src/main/java/com/example");
examplePackage.mkdirs();

View File

@@ -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.
@@ -18,7 +18,9 @@ package org.springframework.boot.gradle.tasks.bundling;
import java.io.File;
import java.io.IOException;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.stream.Stream;
import org.gradle.testkit.runner.InvalidRunnerConfigurationException;
import org.gradle.testkit.runner.TaskOutcome;
@@ -40,12 +42,15 @@ import static org.assertj.core.api.Assertions.assertThat;
@ExtendWith(GradleCompatibilityExtension.class)
abstract class AbstractBootArchiveIntegrationTests {
GradleBuild gradleBuild;
private final String taskName;
protected AbstractBootArchiveIntegrationTests(String taskName) {
private final String libPath;
GradleBuild gradleBuild;
protected AbstractBootArchiveIntegrationTests(String taskName, String libPath) {
this.taskName = taskName;
this.libPath = libPath;
}
@TestTemplate
@@ -135,4 +140,27 @@ abstract class AbstractBootArchiveIntegrationTests {
.isEqualTo(TaskOutcome.SUCCESS);
}
@TestTemplate
void developmentOnlyDependenciesAreNotIncludedInTheArchiveByDefault() throws IOException {
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");
}
}
@TestTemplate
void developmentOnlyDependenciesCanBeIncludedInTheArchive() throws IOException {
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",
this.libPath + "commons-lang3-3.9.jar");
}
}
}

View File

@@ -370,6 +370,7 @@ abstract class AbstractBootArchiveTests<T extends Jar & BootArchive> {
}
@Test
@Deprecated
void devtoolsJarCanBeIncluded() throws IOException {
this.task.setMainClassName("com.example.Main");
this.task.classpath(jarFile("spring-boot-devtools-0.1.2.jar"));

View File

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

View File

@@ -24,7 +24,7 @@ package org.springframework.boot.gradle.tasks.bundling;
class BootWarIntegrationTests extends AbstractBootArchiveIntegrationTests {
BootWarIntegrationTests() {
super("bootWar");
super("bootWar", "WEB-INF/lib/");
}
}