Skip to content

Commit 24a67e9

Browse files
committed
Avoid calling Task.getProject() from a task action at execution time
Fix ``` > Task :spring-boot-project:spring-boot-tools:spring-boot-maven-plugin:formatHelpMojoSource Invocation of Task.project at execution time has been deprecated. This will fail with an error in Gradle 9.0. Consult the upgrading guide for further information: https://docs.gradle.org/8.10.2/userguide/upgrading_version_7.html#task_project ```
1 parent 619b24a commit 24a67e9

File tree

4 files changed

+28
-13
lines changed

4 files changed

+28
-13
lines changed

buildSrc/src/main/java/org/springframework/boot/build/antora/GenerateAntoraPlaybook.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,16 @@
5050
* Task to generate a local Antora playbook.
5151
*
5252
* @author Phillip Webb
53+
* @author Yanming Zhou
5354
*/
5455
public abstract class GenerateAntoraPlaybook extends DefaultTask {
5556

5657
private static final String ANTORA_SOURCE_DIR = "src/docs/antora";
5758

5859
private static final String GENERATED_DOCS = "build/generated/docs/";
5960

61+
private final Project project;
62+
6063
@OutputFile
6164
public abstract RegularFileProperty getOutputFile();
6265

@@ -76,9 +79,10 @@ public abstract class GenerateAntoraPlaybook extends DefaultTask {
7679
public abstract Property<Boolean> getExcludeJavadocExtension();
7780

7881
public GenerateAntoraPlaybook() {
82+
this.project = getProject();
7983
setGroup("Documentation");
8084
setDescription("Generates an Antora playbook.yml file for local use");
81-
getOutputFile().convention(getProject().getLayout()
85+
getOutputFile().convention(this.project.getLayout()
8286
.getBuildDirectory()
8387
.file("generated/docs/antora-playbook/antora-playbook.yml"));
8488
getContentSourceConfiguration().convention("antoraContent");
@@ -127,7 +131,7 @@ private void addExtensions(Map<String, Object> data) {
127131
extensions.xref((xref) -> xref.stub(getXrefStubs().getOrElse(Collections.emptyList())));
128132
extensions.zipContentsCollector((zipContentsCollector) -> {
129133
zipContentsCollector.versionFile("gradle.properties");
130-
String locationName = getProject().getName() + "-${version}-${name}-${classifier}.zip";
134+
String locationName = this.project.getName() + "-${version}-${name}-${classifier}.zip";
131135
Path antoraContent = getRelativeProjectPath()
132136
.resolve(GENERATED_DOCS + "antora-content/" + locationName);
133137
Path antoraDependencies = getRelativeProjectPath()
@@ -149,12 +153,12 @@ private void addSources(Map<String, Object> data) {
149153
private Map<String, Object> createContentSource() {
150154
Map<String, Object> source = new LinkedHashMap<>();
151155
Path playbookPath = getOutputFile().get().getAsFile().toPath().getParent();
152-
Path antoraSrc = getProjectPath(getProject()).resolve(ANTORA_SOURCE_DIR);
156+
Path antoraSrc = getProjectPath(this.project).resolve(ANTORA_SOURCE_DIR);
153157
StringBuilder url = new StringBuilder(".");
154158
relativizeFromRootProject(playbookPath).normalize().forEach((path) -> url.append(File.separator).append(".."));
155159
source.put("url", url.toString());
156160
source.put("branches", "HEAD");
157-
source.put("version", getProject().getVersion().toString());
161+
source.put("version", this.project.getVersion().toString());
158162
Set<String> startPaths = new LinkedHashSet<>();
159163
addAntoraContentStartPaths(startPaths);
160164
startPaths.add(relativizeFromRootProject(antoraSrc).toString());
@@ -163,7 +167,7 @@ private Map<String, Object> createContentSource() {
163167
}
164168

165169
private void addAntoraContentStartPaths(Set<String> startPaths) {
166-
Configuration configuration = getProject().getConfigurations().findByName("antoraContent");
170+
Configuration configuration = this.project.getConfigurations().findByName("antoraContent");
167171
if (configuration != null) {
168172
for (ProjectDependency dependency : configuration.getAllDependencies().withType(ProjectDependency.class)) {
169173
Path path = dependency.getDependencyProject().getProjectDir().toPath();
@@ -175,7 +179,7 @@ private void addAntoraContentStartPaths(Set<String> startPaths) {
175179
private void addDir(Map<String, Object> data) {
176180
Path playbookDir = toRealPath(getOutputFile().get().getAsFile().toPath()).getParent();
177181
Path outputDir = toRealPath(
178-
getProject().getLayout().getBuildDirectory().dir("site").get().getAsFile().toPath());
182+
this.project.getLayout().getBuildDirectory().dir("site").get().getAsFile().toPath());
179183
data.put("output", Map.of("dir", "." + File.separator + playbookDir.relativize(outputDir)));
180184
}
181185

@@ -202,11 +206,11 @@ private Yaml createYaml() {
202206
}
203207

204208
private Path getRelativeProjectPath() {
205-
return relativizeFromRootProject(getProjectPath(getProject()));
209+
return relativizeFromRootProject(getProjectPath(this.project));
206210
}
207211

208212
private Path relativizeFromRootProject(Path subPath) {
209-
Path rootProjectPath = getProjectPath(getProject().getRootProject());
213+
Path rootProjectPath = getProjectPath(this.project.getRootProject());
210214
return rootProjectPath.relativize(subPath).normalize();
211215
}
212216

buildSrc/src/main/java/org/springframework/boot/build/cli/HomebrewFormula.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,21 +47,24 @@
4747
* A {@link Task} for creating a Homebrew formula manifest.
4848
*
4949
* @author Andy Wilkinson
50+
* @author Yanming Zhou
5051
*/
5152
public abstract class HomebrewFormula extends DefaultTask {
5253

5354
private static final Logger logger = LoggerFactory.getLogger(HomebrewFormula.class);
5455

56+
private final Project project;
57+
5558
private final FileSystemOperations fileSystemOperations;
5659

5760
@Inject
5861
public HomebrewFormula(FileSystemOperations fileSystemOperations) {
62+
this.project = getProject();
5963
this.fileSystemOperations = fileSystemOperations;
60-
Project project = getProject();
6164
MapProperty<String, Object> properties = getProperties();
6265
properties.put("hash", getArchive().map((archive) -> sha256(archive.getAsFile())));
63-
getProperties().put("repo", ArtifactRelease.forProject(project).getDownloadRepo());
64-
getProperties().put("version", project.getVersion().toString());
66+
getProperties().put("repo", ArtifactRelease.forProject(this.project).getDownloadRepo());
67+
getProperties().put("version", this.project.getVersion().toString());
6568
}
6669

6770
private String sha256(File file) {
@@ -90,7 +93,7 @@ private String sha256(File file) {
9093

9194
@TaskAction
9295
void createFormula() {
93-
BuildType buildType = BuildProperties.get(getProject()).buildType();
96+
BuildType buildType = BuildProperties.get(this.project).buildType();
9497
if (buildType != BuildType.OPEN_SOURCE) {
9598
logger.debug("Skipping Homebrew formula for non open source build type");
9699
return;

buildSrc/src/main/java/org/springframework/boot/build/mavenplugin/MavenPluginPlugin.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@
106106
*
107107
* @author Andy Wilkinson
108108
* @author Phillip Webb
109+
* @author Yanming Zhou
109110
*/
110111
public class MavenPluginPlugin implements Plugin<Project> {
111112

@@ -334,8 +335,14 @@ private void addExtractVersionPropertiesTask(Project project) {
334335

335336
public abstract static class FormatHelpMojoSource extends DefaultTask {
336337

338+
private final Project project;
339+
337340
private Task generator;
338341

342+
public FormatHelpMojoSource() {
343+
this.project = getProject();
344+
}
345+
339346
void setGenerator(Task generator) {
340347
this.generator = generator;
341348
getInputs().files(this.generator)
@@ -350,7 +357,7 @@ void setGenerator(Task generator) {
350357
void syncAndFormat() {
351358
FileFormatter formatter = new FileFormatter();
352359
for (File output : this.generator.getOutputs().getFiles()) {
353-
formatter.formatFiles(getProject().fileTree(output), StandardCharsets.UTF_8)
360+
formatter.formatFiles(this.project.fileTree(output), StandardCharsets.UTF_8)
354361
.forEach((edit) -> save(output, edit));
355362
}
356363
}

spring-boot-project/spring-boot-tools/spring-boot-antlib/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ task syncIntegrationTestSources(type: Sync) {
3232
}
3333

3434
processResources {
35+
def project = owner.project
3536
eachFile {
3637
filter { it.replace('${spring-boot.version}', project.version) }
3738
}

0 commit comments

Comments
 (0)