Add testAndDevelopmentOnly configuration

Closes gh-35436
This commit is contained in:
Andy Wilkinson
2023-09-28 13:23:48 +01:00
parent b52a781f7b
commit bebca55a8f
33 changed files with 587 additions and 24 deletions

View File

@@ -142,7 +142,52 @@ class JavaPluginActionIntegrationTests {
@TestTemplate
void applyingJavaPluginCreatesDevelopmentOnlyConfiguration() {
assertThat(this.gradleBuild.build("build").getOutput()).contains("developmentOnly exists = true");
assertThat(this.gradleBuild.build("help").getOutput()).contains("developmentOnly exists = true");
}
@TestTemplate
void applyingJavaPluginCreatesTestAndDevelopmentOnlyConfiguration() {
assertThat(this.gradleBuild.build("help").getOutput()).contains("testAndDevelopmentOnly exists = true");
}
@TestTemplate
void testCompileClasspathIncludesTestAndDevelopmentOnlyDependencies() {
assertThat(this.gradleBuild.build("help").getOutput()).contains("commons-lang3-3.12.0.jar");
}
@TestTemplate
void testRuntimeClasspathIncludesTestAndDevelopmentOnlyDependencies() {
assertThat(this.gradleBuild.build("help").getOutput()).contains("commons-lang3-3.12.0.jar");
}
@TestTemplate
void testCompileClasspathDoesNotIncludeDevelopmentOnlyDependencies() {
assertThat(this.gradleBuild.build("help").getOutput()).doesNotContain("commons-lang3-3.12.0.jar");
}
@TestTemplate
void testRuntimeClasspathDoesNotIncludeDevelopmentOnlyDependencies() {
assertThat(this.gradleBuild.build("help").getOutput()).doesNotContain("commons-lang3-3.12.0.jar");
}
@TestTemplate
void compileClasspathDoesNotIncludeTestAndDevelopmentOnlyDependencies() {
assertThat(this.gradleBuild.build("help").getOutput()).doesNotContain("commons-lang3-3.12.0.jar");
}
@TestTemplate
void runtimeClasspathIncludesTestAndDevelopmentOnlyDependencies() {
assertThat(this.gradleBuild.build("help").getOutput()).contains("commons-lang3-3.12.0.jar");
}
@TestTemplate
void compileClasspathDoesNotIncludeDevelopmentOnlyDependencies() {
assertThat(this.gradleBuild.build("help").getOutput()).doesNotContain("commons-lang3-3.12.0.jar");
}
@TestTemplate
void runtimeClasspathIncludesDevelopmentOnlyDependencies() {
assertThat(this.gradleBuild.build("help").getOutput()).contains("commons-lang3-3.12.0.jar");
}
@TestTemplate

View File

@@ -105,6 +105,14 @@ class NativeImagePluginActionIntegrationTests {
assertThat(result.getOutput()).doesNotContain("commons-lang");
}
@TestTemplate
void testAndDevelopmentOnlyDependenciesDoNotAppearInNativeImageClasspath() {
writeDummySpringApplicationAotProcessorMainClass();
BuildResult result = this.gradleBuild.expectDeprecationWarningsWithAtLeastVersion("8.2-rc-1")
.build("checkNativeImageClasspath");
assertThat(result.getOutput()).doesNotContain("commons-lang");
}
@TestTemplate
void classesGeneratedDuringAotProcessingAreOnTheNativeImageClasspath() {
BuildResult result = this.gradleBuild.expectDeprecationWarningsWithAtLeastVersion("8.2-rc-1")

View File

@@ -106,10 +106,22 @@ class SpringBootAotPluginIntegrationTests {
@TestTemplate
void processTestAotDoesNotHaveDevelopmentOnlyDependenciesOnItsClasspath() {
String output = this.gradleBuild.build("processTestAotClasspath", "--stacktrace").getOutput();
String output = this.gradleBuild.build("processTestAotClasspath").getOutput();
assertThat(output).doesNotContain("commons-lang");
}
@TestTemplate
void processAotDoesNotHaveTestAndDevelopmentOnlyDependenciesOnItsClasspath() {
String output = this.gradleBuild.build("processAotClasspath").getOutput();
assertThat(output).doesNotContain("commons-lang");
}
@TestTemplate
void processTestAotHasTestAndDevelopmentOnlyDependenciesOnItsClasspath() {
String output = this.gradleBuild.build("processTestAotClasspath").getOutput();
assertThat(output).contains("commons-lang");
}
@TestTemplate
void processAotRunsWhenProjectHasMainSource() throws IOException {
writeMainClass("org.springframework.boot", "SpringApplicationAotProcessor");

View File

@@ -221,6 +221,41 @@ abstract class AbstractBootArchiveIntegrationTests {
}
}
@TestTemplate
void testAndDevelopmentOnlyDependenciesAreNotIncludedInTheArchiveByDefault() 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");
}
}
@TestTemplate
void testAndDevelopmentOnlyDependenciesCanBeIncludedInTheArchive() 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");
}
}
@TestTemplate
void jarTypeFilteringIsApplied() throws IOException {
File flatDirRepository = new File(this.gradleBuild.getProjectDir(), "repository");

View File

@@ -146,6 +146,22 @@ class BootRunIntegrationTests {
assertThat(result.getOutput()).contains("com.example.bootrun.main.CustomMainClass");
}
@TestTemplate
void developmentOnlyDependenciesAreOnTheClasspath() throws IOException {
copyClasspathApplication();
BuildResult result = this.gradleBuild.build("bootRun");
assertThat(result.task(":bootRun").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
assertThat(result.getOutput()).contains("commons-lang3-3.12.0.jar");
}
@TestTemplate
void testAndDevelopmentOnlyDependenciesAreOnTheClasspath() throws IOException {
copyClasspathApplication();
BuildResult result = this.gradleBuild.build("bootRun");
assertThat(result.task(":bootRun").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
assertThat(result.getOutput()).contains("commons-lang3-3.12.0.jar");
}
private void copyMainClassApplication() throws IOException {
copyApplication("main");
}

View File

@@ -117,6 +117,22 @@ class BootTestRunIntegrationTests {
}
}
@TestTemplate
void developmentOnlyDependenciesAreNotOnTheClasspath() throws IOException {
copyClasspathApplication();
BuildResult result = this.gradleBuild.build("bootTestRun");
assertThat(result.task(":bootTestRun").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
assertThat(result.getOutput()).doesNotContain("commons-lang3-3.12.0.jar");
}
@TestTemplate
void testAndDevelopmentOnlyDependenciesAreOnTheClasspath() throws IOException {
copyClasspathApplication();
BuildResult result = this.gradleBuild.build("bootTestRun");
assertThat(result.task(":bootTestRun").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
assertThat(result.getOutput()).contains("commons-lang3-3.12.0.jar");
}
private void copyClasspathApplication() throws IOException {
copyApplication("classpath");
}

View File

@@ -0,0 +1,12 @@
plugins {
id 'org.springframework.boot' version '{version}'
id 'java'
}
springBoot {
mainClass = "com.example.Main"
}
gradle.taskGraph.whenReady {
println "testAndDevelopmentOnly exists = ${configurations.findByName('testAndDevelopmentOnly') != null}"
}

View File

@@ -0,0 +1,20 @@
plugins {
id 'org.springframework.boot' version '{version}'
id 'java'
}
springBoot {
mainClass = "com.example.Main"
}
repositories {
mavenCentral()
}
dependencies {
developmentOnly("org.apache.commons:commons-lang3:3.12.0")
}
gradle.taskGraph.whenReady {
configurations.compileClasspath.resolve().each { println it }
}

View File

@@ -0,0 +1,20 @@
plugins {
id 'org.springframework.boot' version '{version}'
id 'java'
}
springBoot {
mainClass = "com.example.Main"
}
repositories {
mavenCentral()
}
dependencies {
testAndDevelopmentOnly("org.apache.commons:commons-lang3:3.12.0")
}
gradle.taskGraph.whenReady {
configurations.compileClasspath.resolve().each { println it }
}

View File

@@ -0,0 +1,20 @@
plugins {
id 'org.springframework.boot' version '{version}'
id 'java'
}
springBoot {
mainClass = "com.example.Main"
}
repositories {
mavenCentral()
}
dependencies {
developmentOnly("org.apache.commons:commons-lang3:3.12.0")
}
gradle.taskGraph.whenReady {
configurations.runtimeClasspath.resolve().each { println it }
}

View File

@@ -0,0 +1,20 @@
plugins {
id 'org.springframework.boot' version '{version}'
id 'java'
}
springBoot {
mainClass = "com.example.Main"
}
repositories {
mavenCentral()
}
dependencies {
testAndDevelopmentOnly("org.apache.commons:commons-lang3:3.12.0")
}
gradle.taskGraph.whenReady {
configurations.runtimeClasspath.resolve().each { println it }
}

View File

@@ -0,0 +1,20 @@
plugins {
id 'org.springframework.boot' version '{version}'
id 'java'
}
springBoot {
mainClass = "com.example.Main"
}
repositories {
mavenCentral()
}
dependencies {
developmentOnly("org.apache.commons:commons-lang3:3.12.0")
}
gradle.taskGraph.whenReady {
configurations.testCompileClasspath.resolve().each { println it }
}

View File

@@ -0,0 +1,20 @@
plugins {
id 'org.springframework.boot' version '{version}'
id 'java'
}
springBoot {
mainClass = "com.example.Main"
}
repositories {
mavenCentral()
}
dependencies {
testAndDevelopmentOnly("org.apache.commons:commons-lang3:3.12.0")
}
gradle.taskGraph.whenReady {
configurations.testCompileClasspath.resolve().each { println it }
}

View File

@@ -0,0 +1,20 @@
plugins {
id 'org.springframework.boot' version '{version}'
id 'java'
}
springBoot {
mainClass = "com.example.Main"
}
repositories {
mavenCentral()
}
dependencies {
developmentOnly("org.apache.commons:commons-lang3:3.12.0")
}
gradle.taskGraph.whenReady {
configurations.testRuntimeClasspath.resolve().each { println it }
}

View File

@@ -0,0 +1,20 @@
plugins {
id 'org.springframework.boot' version '{version}'
id 'java'
}
springBoot {
mainClass = "com.example.Main"
}
repositories {
mavenCentral()
}
dependencies {
testAndDevelopmentOnly("org.apache.commons:commons-lang3:3.12.0")
}
gradle.taskGraph.whenReady {
configurations.testRuntimeClasspath.resolve().each { println it }
}

View File

@@ -0,0 +1,20 @@
plugins {
id 'org.springframework.boot' version '{version}'
id 'java'
}
apply plugin: 'org.graalvm.buildtools.native'
repositories {
mavenCentral()
}
dependencies {
testAndDevelopmentOnly("org.apache.commons:commons-lang3:3.12.0")
}
task('checkNativeImageClasspath') {
doFirst {
tasks.nativeCompile.options.get().classpath.each { println it }
}
}

View File

@@ -0,0 +1,20 @@
plugins {
id 'org.springframework.boot' version '{version}'
id 'java'
}
apply plugin: 'org.springframework.boot.aot'
repositories {
mavenCentral()
}
dependencies {
testAndDevelopmentOnly("org.apache.commons:commons-lang3:3.12.0")
}
task('processAotClasspath') {
doFirst {
tasks.processAot.classpath.each { println it }
}
}

View File

@@ -0,0 +1,31 @@
plugins {
id 'org.springframework.boot' version '{version}'
id 'java'
}
apply plugin: 'org.springframework.boot.aot'
repositories {
mavenCentral()
maven { url 'file:repository' }
}
configurations.all {
resolutionStrategy {
eachDependency {
if (it.requested.group == 'org.springframework.boot') {
it.useVersion project.bootVersion
}
}
}
}
dependencies {
testAndDevelopmentOnly("org.apache.commons:commons-lang3:3.12.0")
}
task('processTestAotClasspath') {
doFirst {
tasks.processTestAot.classpath.each { println it }
}
}

View File

@@ -0,0 +1,24 @@
plugins {
id 'java'
id 'org.springframework.boot' version '{version}'
}
bootJar {
mainClass = 'com.example.Application'
}
repositories {
mavenCentral()
}
dependencies {
testAndDevelopmentOnly("org.apache.commons:commons-lang3:3.9")
testAndDevelopmentOnly("commons-io:commons-io:2.6")
implementation("commons-io:commons-io:2.6")
}
bootJar {
layered {
enabled = false
}
}

View File

@@ -0,0 +1,27 @@
plugins {
id 'java'
id 'org.springframework.boot' version '{version}'
}
bootJar {
mainClass = 'com.example.Application'
}
repositories {
mavenCentral()
}
dependencies {
testAndDevelopmentOnly("org.apache.commons:commons-lang3:3.9")
implementation("commons-io:commons-io:2.6")
}
bootJar {
classpath configurations.testAndDevelopmentOnly
}
bootJar {
layered {
enabled = false
}
}

View File

@@ -0,0 +1,24 @@
plugins {
id 'war'
id 'org.springframework.boot' version '{version}'
}
bootWar {
mainClass = 'com.example.Application'
}
repositories {
mavenCentral()
}
dependencies {
testAndDevelopmentOnly("org.apache.commons:commons-lang3:3.9")
testAndDevelopmentOnly("commons-io:commons-io:2.6")
implementation("commons-io:commons-io:2.6")
}
bootWar {
layered {
enabled = false
}
}

View File

@@ -0,0 +1,27 @@
plugins {
id 'war'
id 'org.springframework.boot' version '{version}'
}
bootWar {
mainClass = 'com.example.Application'
}
repositories {
mavenCentral()
}
dependencies {
testAndDevelopmentOnly("org.apache.commons:commons-lang3:3.9")
implementation("commons-io:commons-io:2.6")
}
bootWar {
classpath configurations.testAndDevelopmentOnly
}
bootWar {
layered {
enabled = false
}
}

View File

@@ -0,0 +1,12 @@
plugins {
id 'java'
id 'org.springframework.boot' version '{version}'
}
repositories {
mavenCentral()
}
dependencies {
developmentOnly("org.apache.commons:commons-lang3:3.12.0")
}

View File

@@ -0,0 +1,12 @@
plugins {
id 'java'
id 'org.springframework.boot' version '{version}'
}
repositories {
mavenCentral()
}
dependencies {
testAndDevelopmentOnly("org.apache.commons:commons-lang3:3.12.0")
}

View File

@@ -0,0 +1,12 @@
plugins {
id 'java'
id 'org.springframework.boot' version '{version}'
}
repositories {
mavenCentral()
}
dependencies {
developmentOnly("org.apache.commons:commons-lang3:3.12.0")
}

View File

@@ -0,0 +1,12 @@
plugins {
id 'java'
id 'org.springframework.boot' version '{version}'
}
repositories {
mavenCentral()
}
dependencies {
testAndDevelopmentOnly("org.apache.commons:commons-lang3:3.12.0")
}