From b2ce1152120424b9840f6159264c354d00a80f57 Mon Sep 17 00:00:00 2001 From: Yanming Zhou Date: Wed, 21 Feb 2024 16:14:25 +0800 Subject: [PATCH] Avoid calling Task.getProject() at execution time A task must not use any Project objects at execution time. This includes calling Task.getProject() while the task is running. See https://docs.gradle.org/7.0/userguide/configuration_cache.html#config_cache:requirements:use_project_during_execution Fix GH-39635 --- .../boot/gradle/plugin/ResolveMainClassName.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/plugin/ResolveMainClassName.java b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/plugin/ResolveMainClassName.java index 30c09f60337c..9478f4d394c0 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/plugin/ResolveMainClassName.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/plugin/ResolveMainClassName.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. @@ -47,6 +47,7 @@ * {@link Task} for resolving the name of the application's main class. * * @author Andy Wilkinson + * @author Yanming Zhou * @since 2.4 */ @DisableCachingByDefault(because = "Not worth caching") @@ -58,6 +59,8 @@ public class ResolveMainClassName extends DefaultTask { private final Property configuredMainClass; + private final Path projectDir; + private FileCollection classpath; /** @@ -66,6 +69,7 @@ public class ResolveMainClassName extends DefaultTask { public ResolveMainClassName() { this.outputFile = getProject().getObjects().fileProperty(); this.configuredMainClass = getProject().getObjects().property(String.class); + this.projectDir = getProject().getProjectDir().toPath(); } /** @@ -153,7 +157,7 @@ Provider readMainClassName() { String classpath = getClasspath().filter(File::isDirectory) .getFiles() .stream() - .map((directory) -> getProject().getProjectDir().toPath().relativize(directory.toPath())) + .map((directory) -> this.projectDir.relativize(directory.toPath())) .map(Path::toString) .collect(Collectors.joining(",")); return this.outputFile.map(new ClassNameReader(classpath));