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
pull/1116/head
Dave Syer 11 years ago
parent 3a8a127285
commit 4a40e8f833

@ -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();
}
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;
}
}

@ -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);
}
}

Loading…
Cancel
Save