diff --git a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/plugin/MainClassConvention.java b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/plugin/MainClassConvention.java index 3861fbf80e..543f2a8f04 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/plugin/MainClassConvention.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/plugin/MainClassConvention.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2020 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. @@ -18,6 +18,7 @@ package org.springframework.boot.gradle.plugin; import java.io.File; import java.io.IOException; +import java.lang.reflect.Method; import java.util.Objects; import java.util.concurrent.Callable; import java.util.function.Supplier; @@ -26,6 +27,7 @@ import org.gradle.api.InvalidUserDataException; import org.gradle.api.Project; import org.gradle.api.file.FileCollection; import org.gradle.api.plugins.JavaApplication; +import org.gradle.api.provider.Property; import org.springframework.boot.gradle.dsl.SpringBootExtension; import org.springframework.boot.loader.tools.MainClassFinder; @@ -54,11 +56,36 @@ final class MainClassConvention implements Callable { if (springBootExtension != null && springBootExtension.getMainClassName() != null) { return springBootExtension.getMainClassName(); } + String javaApplicationMainClass = getJavaApplicationMainClass(); + return (javaApplicationMainClass != null) ? javaApplicationMainClass : resolveMainClass(); + } + + @SuppressWarnings("unchecked") + private String getJavaApplicationMainClass() { JavaApplication javaApplication = this.project.getConvention().findByType(JavaApplication.class); - if (javaApplication != null && javaApplication.getMainClassName() != null) { - return javaApplication.getMainClassName(); + if (javaApplication == null) { + return null; + } + Method getMainClass = findMethod(JavaApplication.class, "getMainClass"); + if (getMainClass != null) { + try { + Property mainClass = (Property) getMainClass.invoke(javaApplication); + return mainClass.getOrElse(null); + } + catch (Exception ex) { + // Continue + } + } + return javaApplication.getMainClassName(); + } + + private static Method findMethod(Class type, String name) { + for (Method candidate : type.getMethods()) { + if (candidate.getName().equals(name)) { + return candidate; + } } - return resolveMainClass(); + return null; } private String resolveMainClass() { diff --git a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/junit/GradleCompatibilityExtension.java b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/junit/GradleCompatibilityExtension.java index 3b00d23503..ea27459db4 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/junit/GradleCompatibilityExtension.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/junit/GradleCompatibilityExtension.java @@ -39,7 +39,7 @@ import org.springframework.boot.gradle.testkit.GradleBuildExtension; public final class GradleCompatibilityExtension implements TestTemplateInvocationContextProvider { private static final List GRADLE_VERSIONS = Arrays.asList("default", "5.0", "5.1.1", "5.2.1", "5.3.1", - "5.4.1", "5.5.1", "5.6.4", "6.0.1", "6.1.1", "6.2.2", "6.3", "6.4", "6.5.1", "6.6"); + "5.4.1", "5.5.1", "5.6.4", "6.0.1", "6.1.1", "6.2.2", "6.3", "6.4", "6.5.1", "6.6", "6.7-rc-1"); @Override public Stream provideTestTemplateInvocationContexts(ExtensionContext context) { diff --git a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/testkit/GradleBuild.java b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/testkit/GradleBuild.java index fdfa43be2a..2ed1dd9117 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/testkit/GradleBuild.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/testkit/GradleBuild.java @@ -86,8 +86,9 @@ public class GradleBuild { } private List pluginClasspath() { - return Arrays.asList(new File("bin"), new File("build/classes/java/main"), new File("build/resources/main"), - new File(pathOfJarContaining(LaunchScript.class)), new File(pathOfJarContaining(ClassVisitor.class)), + return Arrays.asList(new File("bin/main"), new File("build/classes/java/main"), + new File("build/resources/main"), new File(pathOfJarContaining(LaunchScript.class)), + new File(pathOfJarContaining(ClassVisitor.class)), new File(pathOfJarContaining(DependencyManagementPlugin.class)), new File(pathOfJarContaining(PropertiesKt.class)), new File(pathOfJarContaining(KotlinLogger.class)), new File(pathOfJarContaining(KotlinPlugin.class)), new File(pathOfJarContaining(KotlinProject.class)),