|
1 | 1 | /*
|
2 |
| - * Copyright 2012-2019 the original author or authors. |
| 2 | + * Copyright 2012-2020 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
18 | 18 |
|
19 | 19 | import java.io.File;
|
20 | 20 | import java.io.IOException;
|
| 21 | +import java.lang.reflect.Method; |
21 | 22 | import java.util.Objects;
|
22 | 23 | import java.util.concurrent.Callable;
|
23 | 24 | import java.util.function.Supplier;
|
|
26 | 27 | import org.gradle.api.Project;
|
27 | 28 | import org.gradle.api.file.FileCollection;
|
28 | 29 | import org.gradle.api.plugins.JavaApplication;
|
| 30 | +import org.gradle.api.provider.Property; |
29 | 31 |
|
30 | 32 | import org.springframework.boot.gradle.dsl.SpringBootExtension;
|
31 | 33 | import org.springframework.boot.loader.tools.MainClassFinder;
|
@@ -54,11 +56,36 @@ public Object call() throws Exception {
|
54 | 56 | if (springBootExtension != null && springBootExtension.getMainClassName() != null) {
|
55 | 57 | return springBootExtension.getMainClassName();
|
56 | 58 | }
|
| 59 | + String javaApplicationMainClass = getJavaApplicationMainClass(); |
| 60 | + return (javaApplicationMainClass != null) ? javaApplicationMainClass : resolveMainClass(); |
| 61 | + } |
| 62 | + |
| 63 | + @SuppressWarnings("unchecked") |
| 64 | + private String getJavaApplicationMainClass() { |
57 | 65 | JavaApplication javaApplication = this.project.getConvention().findByType(JavaApplication.class);
|
58 |
| - if (javaApplication != null && javaApplication.getMainClassName() != null) { |
59 |
| - return javaApplication.getMainClassName(); |
| 66 | + if (javaApplication == null) { |
| 67 | + return null; |
| 68 | + } |
| 69 | + Method getMainClass = findMethod(JavaApplication.class, "getMainClass"); |
| 70 | + if (getMainClass != null) { |
| 71 | + try { |
| 72 | + Property<String> mainClass = (Property<String>) getMainClass.invoke(javaApplication); |
| 73 | + return mainClass.getOrElse(null); |
| 74 | + } |
| 75 | + catch (Exception ex) { |
| 76 | + // Continue |
| 77 | + } |
| 78 | + } |
| 79 | + return javaApplication.getMainClassName(); |
| 80 | + } |
| 81 | + |
| 82 | + private static Method findMethod(Class<?> type, String name) { |
| 83 | + for (Method candidate : type.getMethods()) { |
| 84 | + if (candidate.getName().equals(name)) { |
| 85 | + return candidate; |
| 86 | + } |
60 | 87 | }
|
61 |
| - return resolveMainClass(); |
| 88 | + return null; |
62 | 89 | }
|
63 | 90 |
|
64 | 91 | private String resolveMainClass() {
|
|
0 commit comments