Support for main class detection in installApp
I'm sure someone can do a better job of this, but here's a proposal that works. It uses our FindMainTask to set the relevant properties if theu are missing in the application plugin. Fixes gh-1105
This commit is contained in:
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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"
|
||||
}
|
||||
@@ -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) {
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
// Search
|
||||
SourceSet mainSourceSet = SourceSets.findMainSourceSet(project);
|
||||
if (mainSourceSet == null) {
|
||||
return null;
|
||||
ApplicationPluginConvention application = (ApplicationPluginConvention) project.getConvention().getPlugins().get(
|
||||
"application");
|
||||
// Try the Application extension setting
|
||||
if (mainClass == null && application.getMainClassName() != null) {
|
||||
mainClass = application.getMainClassName();
|
||||
}
|
||||
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;
|
||||
|
||||
Task runTask = getProject().getTasks().getByName("run");
|
||||
if (mainClass == null && runTask.hasProperty("main")) {
|
||||
mainClass = (String) runTask.property("main");
|
||||
}
|
||||
catch (IOException ex) {
|
||||
throw new IllegalStateException("Cannot find main class", ex);
|
||||
|
||||
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().info("Found main: " + mainClass);
|
||||
|
||||
if (bootExtension.getMainClass() == null) {
|
||||
bootExtension.setMainClass(mainClass);
|
||||
}
|
||||
if (application.getMainClassName() == null) {
|
||||
application.setMainClassName(mainClass);
|
||||
}
|
||||
if (!runTask.hasProperty("main")) {
|
||||
runTask.setProperty("main", mainClass);
|
||||
}
|
||||
|
||||
return mainClass;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<Task>() {
|
||||
@Override
|
||||
public void execute(Task task) {
|
||||
if(task instanceof JavaExec) {
|
||||
if(task instanceof JavaExec || task instanceof CreateStartScripts) {
|
||||
task.dependsOn(FIND_MAIN_CLASS_TASK_NAME);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user