Ignore non-JavaExec run task when finding application's main class

Previously, FindMainClassTask would look for a property named main
on any class named run. This was based on the assumption that the
run task would be a JavaExec task (typically provided by the
application plugin). If the run task was not a JavaExec task (more
accurately, if it did not have a main property) this would result in
a build failure due to trying to read a non-existent property.

This commit updates FindMainClassTask to only use the main property
of the run task if the task is a JavaExec task. This guarantees that
the property will exist on the task, and unlike using any property
named main on a task named run, also guarantee that its value will
refer to a Java class with a main method.

Closes gh-5501
pull/5663/merge
Andy Wilkinson 9 years ago
parent ae095b2c1b
commit 1043239de0

@ -1,5 +1,5 @@
/*
* Copyright 2012-2015 the original author or authors.
* Copyright 2012-2016 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,30 +18,57 @@ package org.springframework.boot.gradle;
import java.io.IOException;
import org.gradle.tooling.BuildException;
import org.gradle.tooling.ProjectConnection;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
/**
* Tests for configuring a project's main class
*
* @author Dave Syer
* @author Andy Wilkinson
*/
public class MainClassTests {
private static ProjectConnection project;
private static final String BOOT_VERSION = Versions.getBootVersion();
private static ProjectConnection project;
@BeforeClass
public static void createProject() throws IOException {
project = new ProjectCreator().createProject("main-in-boot-run");
project = new ProjectCreator().createProject("main-class");
}
@Test
public void mainFromBootRun() {
project.newBuild().forTasks("build")
.withArguments("-PbootVersion=" + BOOT_VERSION, "--info").run();
.withArguments("-PbootVersion=" + BOOT_VERSION, "-PbootRunMain=true")
.run();
}
@Test
public void nonJavaExecRunTaskIsIgnored() {
try {
project.newBuild().forTasks("build").withArguments(
"-PbootVersion=" + BOOT_VERSION, "-PnonJavaExecRun=true").run();
}
catch (BuildException ex) {
Throwable rootCause = getRootCause(ex);
assertThat(rootCause.getMessage(), is(equalTo("Unable to find main class")));
}
}
private Throwable getRootCause(Throwable ex) {
Throwable candidate = ex;
while (candidate.getCause() != null) {
candidate = candidate.getCause();
}
return candidate;
}
}

@ -14,10 +14,17 @@ apply plugin: 'spring-boot'
group = 'installer'
version = '0.0.0'
bootRun {
if (project.hasProperty('bootRunMain')) {
bootRun {
main = 'org.springframework.boot.SpringApplication'
}
}
if (project.hasProperty('nonJavaExecRun')) {
task run { }
}
jar {
baseName = 'installer'
}

@ -1,5 +1,5 @@
/*
* Copyright 2012-2015 the original author or authors.
* Copyright 2012-2016 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.
@ -24,6 +24,7 @@ import org.gradle.api.Task;
import org.gradle.api.plugins.ApplicationPluginConvention;
import org.gradle.api.plugins.ExtraPropertiesExtension;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.JavaExec;
import org.gradle.api.tasks.SourceSetOutput;
import org.gradle.api.tasks.TaskAction;
@ -85,9 +86,9 @@ public class FindMainClassTask extends DefaultTask {
mainClass = application.getMainClassName();
}
Task runTask = project.getTasks().findByName("run");
JavaExec runTask = findRunTask(project);
if (mainClass == null && runTask != null) {
mainClass = (String) runTask.property("main");
mainClass = runTask.getMain();
}
if (mainClass == null) {
@ -122,9 +123,17 @@ public class FindMainClassTask extends DefaultTask {
application.setMainClassName(mainClass);
}
if (runTask != null && !runTask.hasProperty("main")) {
runTask.setProperty("main", mainClass);
runTask.setMain(mainClass);
}
return mainClass;
}
private JavaExec findRunTask(Project project) {
Task runTask = project.getTasks().findByName("run");
if (runTask instanceof JavaExec) {
return (JavaExec) runTask;
}
return null;
}
}

Loading…
Cancel
Save