From 2572c6d3cd3f35939e520ddbd9258b7d936b6482 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Fri, 26 Apr 2024 15:52:36 +0100 Subject: [PATCH] Stop calling getProject() during execution of StarterMetadata Closes gh-40537 --- .../boot/build/starters/StarterMetadata.java | 39 ++++++++++--------- .../boot/build/starters/StarterPlugin.java | 4 +- 2 files changed, 23 insertions(+), 20 deletions(-) diff --git a/buildSrc/src/main/java/org/springframework/boot/build/starters/StarterMetadata.java b/buildSrc/src/main/java/org/springframework/boot/build/starters/StarterMetadata.java index 4125182d6e5..fc6fa76c5e6 100644 --- a/buildSrc/src/main/java/org/springframework/boot/build/starters/StarterMetadata.java +++ b/buildSrc/src/main/java/org/springframework/boot/build/starters/StarterMetadata.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2023 the original author or authors. + * Copyright 2012-2024 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. @@ -20,15 +20,18 @@ import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.util.Properties; -import java.util.concurrent.Callable; import java.util.stream.Collectors; import org.gradle.api.DefaultTask; +import org.gradle.api.Project; import org.gradle.api.Task; import org.gradle.api.artifacts.Configuration; import org.gradle.api.artifacts.ResolvedArtifact; import org.gradle.api.file.FileCollection; +import org.gradle.api.file.RegularFileProperty; +import org.gradle.api.provider.Property; import org.gradle.api.tasks.Classpath; +import org.gradle.api.tasks.Input; import org.gradle.api.tasks.OutputFile; import org.gradle.api.tasks.TaskAction; @@ -39,17 +42,22 @@ import org.springframework.core.CollectionFactory; * * @author Andy Wilkinson */ -public class StarterMetadata extends DefaultTask { +public abstract class StarterMetadata extends DefaultTask { private Configuration dependencies; - private File destination; - public StarterMetadata() { - getInputs().property("name", (Callable) () -> getProject().getName()); - getInputs().property("description", (Callable) () -> getProject().getDescription()); + Project project = getProject(); + getStarterName().convention(project.provider(project::getName)); + getStarterDescription().convention(project.provider(project::getDescription)); } + @Input + public abstract Property getStarterName(); + + @Input + public abstract Property getStarterDescription(); + @Classpath public FileCollection getDependencies() { return this.dependencies; @@ -60,19 +68,13 @@ public class StarterMetadata extends DefaultTask { } @OutputFile - public File getDestination() { - return this.destination; - } - - public void setDestination(File destination) { - this.destination = destination; - } + public abstract RegularFileProperty getDestination(); @TaskAction void generateMetadata() throws IOException { Properties properties = CollectionFactory.createSortedProperties(true); - properties.setProperty("name", getProject().getName()); - properties.setProperty("description", getProject().getDescription()); + properties.setProperty("name", getStarterName().get()); + properties.setProperty("description", getStarterDescription().get()); properties.setProperty("dependencies", String.join(",", this.dependencies.getResolvedConfiguration() @@ -80,8 +82,9 @@ public class StarterMetadata extends DefaultTask { .stream() .map(ResolvedArtifact::getName) .collect(Collectors.toSet()))); - this.destination.getParentFile().mkdirs(); - try (FileWriter writer = new FileWriter(this.destination)) { + File destination = getDestination().getAsFile().get(); + destination.getParentFile().mkdirs(); + try (FileWriter writer = new FileWriter(destination)) { properties.store(writer, null); } } diff --git a/buildSrc/src/main/java/org/springframework/boot/build/starters/StarterPlugin.java b/buildSrc/src/main/java/org/springframework/boot/build/starters/StarterPlugin.java index fa28c3dfa5d..84afee6efee 100644 --- a/buildSrc/src/main/java/org/springframework/boot/build/starters/StarterPlugin.java +++ b/buildSrc/src/main/java/org/springframework/boot/build/starters/StarterPlugin.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2023 the original author or authors. + * Copyright 2012-2024 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. @@ -57,7 +57,7 @@ public class StarterPlugin implements Plugin { Configuration runtimeClasspath = configurations.getByName(JavaPlugin.RUNTIME_CLASSPATH_CONFIGURATION_NAME); starterMetadata.setDependencies(runtimeClasspath); File destination = new File(project.getBuildDir(), "starter-metadata.properties"); - starterMetadata.setDestination(destination); + starterMetadata.getDestination().set(destination); configurations.create("starterMetadata"); project.getArtifacts() .add("starterMetadata", project.provider(starterMetadata::getDestination),