diff --git a/spring-boot-integration-tests/src/test/java/org/springframework/boot/gradle/InstallTests.java b/spring-boot-integration-tests/src/test/java/org/springframework/boot/gradle/InstallTests.java index c665ca78a6..16ed982b0b 100644 --- a/spring-boot-integration-tests/src/test/java/org/springframework/boot/gradle/InstallTests.java +++ b/spring-boot-integration-tests/src/test/java/org/springframework/boot/gradle/InstallTests.java @@ -16,10 +16,7 @@ package org.springframework.boot.gradle; -import java.io.IOException; - import org.gradle.tooling.ProjectConnection; -import org.junit.BeforeClass; import org.junit.Test; import org.springframework.boot.dependency.tools.ManagedDependencies; @@ -30,19 +27,25 @@ import org.springframework.boot.dependency.tools.ManagedDependencies; */ public class InstallTests { - private static ProjectConnection project; + private ProjectConnection project; - private static final String BOOT_VERSION = ManagedDependencies.get() - .find("spring-boot").getVersion(); + private static final String BOOT_VERSION = ManagedDependencies.get().find( + "spring-boot").getVersion(); - @BeforeClass - public static void createProject() throws IOException { + @Test + public void cleanInstall() throws Exception { project = new ProjectCreator().createProject("installer"); + project.newBuild().forTasks("install").withArguments( + "-PbootVersion=" + BOOT_VERSION, "--stacktrace").run(); } @Test - public void cleanInstall() { - project.newBuild().forTasks("install").withArguments("-PbootVersion=" + BOOT_VERSION, "--stacktrace").run(); + public void cleanInstallApp() throws Exception { + project = new ProjectCreator().createProject("install-app"); + // "install" from the application plugin was renamed "installApp" in Gradle + // 1.0 + project.newBuild().forTasks("installApp").withArguments( + "-PbootVersion=" + BOOT_VERSION, "--stacktrace", "--info").run(); } } diff --git a/spring-boot-integration-tests/src/test/resources/install-app.gradle b/spring-boot-integration-tests/src/test/resources/install-app.gradle new file mode 100644 index 0000000000..fce05a8137 --- /dev/null +++ b/spring-boot-integration-tests/src/test/resources/install-app.gradle @@ -0,0 +1,32 @@ +buildscript { + repositories { + mavenLocal() + } + dependencies { + classpath("org.springframework.boot:spring-boot-gradle-plugin:${project.bootVersion}") + } +} + +apply plugin: 'java' +apply plugin: 'maven' +apply plugin: 'spring-boot' + +group = 'installer' +version = '0.0.0' + +run { + main = 'org.springframework.boot.SpringApplication' +} + +jar { + baseName = 'install-app' +} + +repositories { + mavenLocal() + mavenCentral() +} + +dependencies { + compile "org.springframework.boot:spring-boot-starter" +} diff --git a/spring-boot-tools/spring-boot-gradle-plugin/src/main/groovy/org/springframework/boot/gradle/repackage/RepackagePluginFeatures.java b/spring-boot-tools/spring-boot-gradle-plugin/src/main/groovy/org/springframework/boot/gradle/repackage/RepackagePluginFeatures.java index 4f0e341d10..19bc3a5b68 100644 --- a/spring-boot-tools/spring-boot-gradle-plugin/src/main/groovy/org/springframework/boot/gradle/repackage/RepackagePluginFeatures.java +++ b/spring-boot-tools/spring-boot-gradle-plugin/src/main/groovy/org/springframework/boot/gradle/repackage/RepackagePluginFeatures.java @@ -29,7 +29,7 @@ import org.springframework.boot.gradle.PluginFeatures; */ public class RepackagePluginFeatures implements PluginFeatures { - private static final String REPACKAGE_TASK_NAME = "bootRepackage"; + public static final String REPACKAGE_TASK_NAME = "bootRepackage"; @Override public void apply(Project project) { diff --git a/spring-boot-tools/spring-boot-gradle-plugin/src/main/groovy/org/springframework/boot/gradle/repackage/RepackageTask.java b/spring-boot-tools/spring-boot-gradle-plugin/src/main/groovy/org/springframework/boot/gradle/repackage/RepackageTask.java index 38d79deea7..40a3413790 100644 --- a/spring-boot-tools/spring-boot-gradle-plugin/src/main/groovy/org/springframework/boot/gradle/repackage/RepackageTask.java +++ b/spring-boot-tools/spring-boot-gradle-plugin/src/main/groovy/org/springframework/boot/gradle/repackage/RepackageTask.java @@ -55,6 +55,10 @@ public class RepackageTask extends DefaultTask { public void setMainClass(String mainClass) { this.mainClass = mainClass; } + + public String getMainClass() { + return mainClass; + } @TaskAction public void repackage() { diff --git a/spring-boot-tools/spring-boot-gradle-plugin/src/main/groovy/org/springframework/boot/gradle/run/FindMainClassTask.java b/spring-boot-tools/spring-boot-gradle-plugin/src/main/groovy/org/springframework/boot/gradle/run/FindMainClassTask.java index 674cb35964..cef3f86dea 100644 --- a/spring-boot-tools/spring-boot-gradle-plugin/src/main/groovy/org/springframework/boot/gradle/run/FindMainClassTask.java +++ b/spring-boot-tools/spring-boot-gradle-plugin/src/main/groovy/org/springframework/boot/gradle/run/FindMainClassTask.java @@ -1,9 +1,12 @@ + package org.springframework.boot.gradle.run; import java.io.IOException; import org.gradle.api.DefaultTask; import org.gradle.api.Project; +import org.gradle.api.Task; +import org.gradle.api.plugins.ApplicationPluginConvention; import org.gradle.api.tasks.SourceSet; import org.gradle.api.tasks.TaskAction; import org.springframework.boot.gradle.SpringBootPluginExtension; @@ -29,28 +32,55 @@ public class FindMainClassTask extends DefaultTask { private String findMainClass() { Project project = getProject(); + String mainClass = null; + // Try the SpringBoot extension setting SpringBootPluginExtension bootExtension = project.getExtensions().getByType( SpringBootPluginExtension.class); - if(bootExtension.getMainClass() != null) { - return bootExtension.getMainClass(); + if (bootExtension.getMainClass() != null) { + mainClass = bootExtension.getMainClass(); + } + + ApplicationPluginConvention application = (ApplicationPluginConvention) project.getConvention().getPlugins().get( + "application"); + // Try the Application extension setting + if (mainClass == null && application.getMainClassName() != null) { + mainClass = application.getMainClassName(); + } + + Task runTask = getProject().getTasks().getByName("run"); + if (mainClass == null && runTask.hasProperty("main")) { + mainClass = (String) runTask.property("main"); } - // Search - SourceSet mainSourceSet = SourceSets.findMainSourceSet(project); - if (mainSourceSet == null) { - return null; + if (mainClass == null) { + // Search + SourceSet mainSourceSet = SourceSets.findMainSourceSet(project); + if (mainSourceSet != null) { + project.getLogger().debug( + "Looking for main in: " + + mainSourceSet.getOutput().getClassesDir()); + try { + mainClass = MainClassFinder.findSingleMainClass(mainSourceSet.getOutput().getClassesDir()); + project.getLogger().info("Computed main class: " + mainClass); + } catch (IOException ex) { + throw new IllegalStateException("Cannot find main class", ex); + } + } } - project.getLogger().debug( - "Looking for main in: " + mainSourceSet.getOutput().getClassesDir()); - try { - String mainClass = MainClassFinder.findSingleMainClass(mainSourceSet - .getOutput().getClassesDir()); - project.getLogger().info("Computed main class: " + mainClass); - return mainClass; + + project.getLogger().info("Found main: " + mainClass); + + if (bootExtension.getMainClass() == null) { + bootExtension.setMainClass(mainClass); } - catch (IOException ex) { - throw new IllegalStateException("Cannot find main class", ex); + if (application.getMainClassName() == null) { + application.setMainClassName(mainClass); } + if (!runTask.hasProperty("main")) { + runTask.setProperty("main", mainClass); + } + + return mainClass; } } diff --git a/spring-boot-tools/spring-boot-gradle-plugin/src/main/groovy/org/springframework/boot/gradle/run/RunPluginFeatures.java b/spring-boot-tools/spring-boot-gradle-plugin/src/main/groovy/org/springframework/boot/gradle/run/RunPluginFeatures.java index e9b0f87ab5..4aff97f413 100644 --- a/spring-boot-tools/spring-boot-gradle-plugin/src/main/groovy/org/springframework/boot/gradle/run/RunPluginFeatures.java +++ b/spring-boot-tools/spring-boot-gradle-plugin/src/main/groovy/org/springframework/boot/gradle/run/RunPluginFeatures.java @@ -23,6 +23,7 @@ import org.gradle.api.Project; import org.gradle.api.Task; import org.gradle.api.plugins.JavaPluginConvention; import org.gradle.api.tasks.JavaExec; +import org.gradle.api.tasks.application.CreateStartScripts; import org.springframework.boot.gradle.PluginFeatures; /** @@ -47,7 +48,7 @@ public class RunPluginFeatures implements PluginFeatures { project.getTasks().all(new Action() { @Override public void execute(Task task) { - if(task instanceof JavaExec) { + if(task instanceof JavaExec || task instanceof CreateStartScripts) { task.dependsOn(FIND_MAIN_CLASS_TASK_NAME); } }