Skip to content

Commit e6a3ca5

Browse files
committed
Address deprecation warning that's generated by Gradle 4.x
In Gradle 4.x, SourceSetOutput now has multiple classes directories and getClassesDir() has been deprecated. This commit introduces the use of reflection to use getClassesDirs() when it's available rather than getClassesDir(). Closes spring-projectsgh-9559
1 parent fc57a0e commit e6a3ca5

File tree

2 files changed

+61
-5
lines changed
  • spring-boot-tools

2 files changed

+61
-5
lines changed

spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/run/FindMainClassTask.java

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,16 @@
1616

1717
package org.springframework.boot.gradle.run;
1818

19+
import java.io.File;
1920
import java.io.IOException;
21+
import java.lang.reflect.Method;
22+
import java.util.Arrays;
23+
import java.util.Collection;
2024

2125
import org.gradle.api.DefaultTask;
2226
import org.gradle.api.Project;
2327
import org.gradle.api.Task;
28+
import org.gradle.api.file.FileCollection;
2429
import org.gradle.api.plugins.ApplicationPluginConvention;
2530
import org.gradle.api.plugins.ExtraPropertiesExtension;
2631
import org.gradle.api.tasks.Input;
@@ -30,6 +35,7 @@
3035

3136
import org.springframework.boot.gradle.SpringBootPluginExtension;
3237
import org.springframework.boot.loader.tools.MainClassFinder;
38+
import org.springframework.util.ReflectionUtils;
3339

3440
/**
3541
* Task to find and set the 'mainClassName' convention when it's missing by searching the
@@ -103,13 +109,13 @@ private String findMainClass() {
103109
if (mainClass == null) {
104110
// Search
105111
if (this.mainClassSourceSetOutput != null) {
106-
project.getLogger().debug("Looking for main in: "
107-
+ this.mainClassSourceSetOutput.getClassesDir());
112+
Collection<File> classesDirs = getClassesDirs(
113+
this.mainClassSourceSetOutput);
114+
getProject().getLogger().debug("Looking for main in: " + classesDirs);
108115
try {
109-
mainClass = MainClassFinder.findSingleMainClass(
110-
this.mainClassSourceSetOutput.getClassesDir(),
116+
mainClass = MainClassFinder.findSingleMainClass(classesDirs,
111117
SPRING_BOOT_APPLICATION_CLASS_NAME);
112-
project.getLogger().info("Computed main class: " + mainClass);
118+
getProject().getLogger().info("Computed main class: " + mainClass);
113119
}
114120
catch (IOException ex) {
115121
throw new IllegalStateException("Cannot find main class", ex);
@@ -132,6 +138,17 @@ private String findMainClass() {
132138
return mainClass;
133139
}
134140

141+
private Collection<File> getClassesDirs(SourceSetOutput sourceSetOutput) {
142+
Method getClassesDirs = ReflectionUtils.findMethod(SourceSetOutput.class,
143+
"getClassesDirs");
144+
if (getClassesDirs == null) {
145+
return Arrays.asList(sourceSetOutput.getClassesDir());
146+
}
147+
FileCollection classesDirs = (FileCollection) ReflectionUtils
148+
.invokeMethod(getClassesDirs, sourceSetOutput);
149+
return classesDirs.getFiles();
150+
}
151+
135152
private JavaExec findRunTask(Project project) {
136153
Task runTask = project.getTasks().findByName("run");
137154
if (runTask instanceof JavaExec) {

spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/MainClassFinder.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.util.ArrayDeque;
2626
import java.util.ArrayList;
2727
import java.util.Arrays;
28+
import java.util.Collection;
2829
import java.util.Collections;
2930
import java.util.Comparator;
3031
import java.util.Deque;
@@ -117,6 +118,44 @@ public static String findSingleMainClass(File rootFolder, String annotationName)
117118
return callback.getMainClassName();
118119
}
119120

121+
/**
122+
* Find a single main class from the given {@code rootFolders}. A main class annotated
123+
* with an annotation with the given {@code annotationName} will be preferred over a
124+
* main class with no such annotation.
125+
* @param rootFolders the root folders to search
126+
* @param annotationName the name of the annotation that may be present on the main
127+
* class
128+
* @return the main class or {@code null}
129+
* @throws IOException if a root folder cannot be read
130+
* @since 1.5.5
131+
*/
132+
public static String findSingleMainClass(Collection<File> rootFolders,
133+
String annotationName) throws IOException {
134+
SingleMainClassCallback callback = new SingleMainClassCallback(annotationName);
135+
doWithMainClasses(rootFolders, callback);
136+
return callback.getMainClassName();
137+
}
138+
139+
/**
140+
* Perform the given callback operation on all main classes from the given root
141+
* folder.
142+
* @param <T> the result type
143+
* @param rootFolders the root folders
144+
* @param callback the callback
145+
* @return the first callback result or {@code null}
146+
* @throws IOException in case of I/O errors
147+
*/
148+
static <T> T doWithMainClasses(Collection<File> rootFolders,
149+
MainClassCallback<T> callback) throws IOException {
150+
for (File rootFolder : rootFolders) {
151+
T result = doWithMainClasses(rootFolder, callback);
152+
if (result != null) {
153+
return result;
154+
}
155+
}
156+
return null;
157+
}
158+
120159
/**
121160
* Perform the given callback operation on all main classes from the given root
122161
* folder.

0 commit comments

Comments
 (0)