Skip to content

Commit 5fdb2ae

Browse files
committed
Protect PluginApplicationActions against absent plugin classes
Closes gh-24526
1 parent 38e4c2a commit 5fdb2ae

File tree

3 files changed

+21
-17
lines changed

3 files changed

+21
-17
lines changed

spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/plugin/KotlinPluginAction.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -47,15 +47,8 @@ private void enableJavaParametersOption(Project project) {
4747
}
4848

4949
@Override
50-
@SuppressWarnings("unchecked")
5150
public Class<? extends Plugin<? extends Project>> getPluginClass() {
52-
try {
53-
return (Class<? extends Plugin<? extends Project>>) Class
54-
.forName("org.jetbrains.kotlin.gradle.plugin.KotlinPluginWrapper");
55-
}
56-
catch (Throwable ex) {
57-
return null;
58-
}
51+
return KotlinPluginWrapper.class;
5952
}
6053

6154
}

spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/plugin/PluginApplicationAction.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -30,9 +30,11 @@ interface PluginApplicationAction extends Action<Project> {
3030

3131
/**
3232
* The class of the {@code Plugin} that, when applied, will trigger the execution of
33-
* this action. May return {@code null} if the plugin class is not on the classpath.
34-
* @return the plugin class or {@code null}
33+
* this action.
34+
* @return the plugin class
35+
* @throws ClassNotFoundException if the plugin class cannot be found
36+
* @throws NoClassDefFoundError if an error occurs when defining the plugin class
3537
*/
36-
Class<? extends Plugin<? extends Project>> getPluginClass();
38+
Class<? extends Plugin<? extends Project>> getPluginClass() throws ClassNotFoundException, NoClassDefFoundError;
3739

3840
}

spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/plugin/SpringBootPlugin.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.util.Arrays;
2020
import java.util.List;
21+
import java.util.function.Consumer;
2122

2223
import org.gradle.api.GradleException;
2324
import org.gradle.api.Plugin;
@@ -119,10 +120,18 @@ private void registerPluginActions(Project project, Configuration bootArchives)
119120
new WarPluginAction(singlePublishedArtifact), new MavenPluginAction(bootArchives.getUploadTaskName()),
120121
new DependencyManagementPluginAction(), new ApplicationPluginAction(), new KotlinPluginAction());
121122
for (PluginApplicationAction action : actions) {
122-
Class<? extends Plugin<? extends Project>> pluginClass = action.getPluginClass();
123-
if (pluginClass != null) {
124-
project.getPlugins().withType(pluginClass, (plugin) -> action.execute(project));
125-
}
123+
withPluginClassOfAction(action,
124+
(pluginClass) -> project.getPlugins().withType(pluginClass, (plugin) -> action.execute(project)));
125+
}
126+
}
127+
128+
private void withPluginClassOfAction(PluginApplicationAction action,
129+
Consumer<Class<? extends Plugin<? extends Project>>> consumer) {
130+
try {
131+
consumer.accept(action.getPluginClass());
132+
}
133+
catch (Throwable ex) {
134+
// Plugin class unavailable. Continue.
126135
}
127136
}
128137

0 commit comments

Comments
 (0)