From c083e0b0499fbd480fce1ed96af089b88de413cd Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Fri, 19 Nov 2021 13:35:30 +0000 Subject: [PATCH] Avoid using temp before @TempDir has initialized it Closes gh-28748 --- .../boot/build/JavaConventions.java | 29 ++++++++++++++++++- .../tasks/bundling/BootBuildImageTests.java | 11 ++++--- 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/buildSrc/src/main/java/org/springframework/boot/build/JavaConventions.java b/buildSrc/src/main/java/org/springframework/boot/build/JavaConventions.java index 6af96fa9e7..916f1a409d 100644 --- a/buildSrc/src/main/java/org/springframework/boot/build/JavaConventions.java +++ b/buildSrc/src/main/java/org/springframework/boot/build/JavaConventions.java @@ -16,6 +16,7 @@ package org.springframework.boot.build; +import java.io.File; import java.util.Arrays; import java.util.Collections; import java.util.List; @@ -23,16 +24,21 @@ import java.util.Map; import java.util.Set; import java.util.TreeMap; import java.util.stream.Collectors; +import java.util.stream.Stream; import io.spring.javaformat.gradle.SpringJavaFormatPlugin; import io.spring.javaformat.gradle.tasks.CheckFormat; import io.spring.javaformat.gradle.tasks.Format; +import org.gradle.api.Action; +import org.gradle.api.GradleException; import org.gradle.api.JavaVersion; import org.gradle.api.Project; +import org.gradle.api.Task; import org.gradle.api.artifacts.Configuration; import org.gradle.api.artifacts.ConfigurationContainer; import org.gradle.api.artifacts.Dependency; import org.gradle.api.artifacts.DependencySet; +import org.gradle.api.file.FileTree; import org.gradle.api.plugins.JavaBasePlugin; import org.gradle.api.plugins.JavaPlugin; import org.gradle.api.plugins.JavaPluginExtension; @@ -178,7 +184,10 @@ class JavaConventions { } private void configureJavadocConventions(Project project) { - project.getTasks().withType(Javadoc.class, (javadoc) -> javadoc.getOptions().source("1.8").encoding("UTF-8")); + project.getTasks().withType(Javadoc.class, (javadoc) -> { + javadoc.getOptions().source("1.8").encoding("UTF-8"); + javadoc.doFirst(new VerifyPackageInfo()); + }); } private void configureJavaConventions(Project project) { @@ -264,4 +273,22 @@ class JavaConventions { project.getTasks().getByName(JavaBasePlugin.CHECK_TASK_NAME).dependsOn(checkClasspathForProhibitedDependencies); } + private static final class VerifyPackageInfo implements Action { + + @Override + public void execute(Task javadoc) { + FileTree source = ((Javadoc) javadoc).getSource(); + Stream dirs = source.getFiles().stream().map((file) -> file.getParentFile()).distinct(); + Set missingPackageInfo = dirs.map((file) -> new File(file, "package-info.java")) + .filter((packageInfo) -> !packageInfo.isFile()).collect(Collectors.toSet()); + if (!missingPackageInfo.isEmpty()) { + StringBuilder message = new StringBuilder( + "A package-info.java file was missing from the following directories:\n"); + missingPackageInfo.forEach((packageInfo) -> message.append("\t" + packageInfo.getParentFile() + "\n")); + throw new GradleException(message.toString()); + } + } + + } + } diff --git a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/tasks/bundling/BootBuildImageTests.java b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/tasks/bundling/BootBuildImageTests.java index 31d2d10dd8..cf6ca50433 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/tasks/bundling/BootBuildImageTests.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/tasks/bundling/BootBuildImageTests.java @@ -24,6 +24,7 @@ import java.util.Map; import org.gradle.api.GradleException; import org.gradle.api.JavaVersion; import org.gradle.api.Project; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; @@ -45,15 +46,13 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType; */ class BootBuildImageTests { - @TempDir - File temp; - Project project; - private final BootBuildImage buildImage; + private BootBuildImage buildImage; - BootBuildImageTests() { - File projectDir = new File(this.temp, "project"); + @BeforeEach + void setUp(@TempDir File temp) { + File projectDir = new File(temp, "project"); projectDir.mkdirs(); this.project = GradleProjectBuilder.builder().withProjectDir(projectDir).withName("build-image-test").build(); this.project.setDescription("Test project for BootBuildImage");