Skip to content

Commit 6f2171c

Browse files
committed
Improve error message when BootJar or BootWar has no known main class
Closes gh-12712
1 parent 7b3be82 commit 6f2171c

File tree

6 files changed

+38
-7
lines changed

6 files changed

+38
-7
lines changed

spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/plugin/MainClassConvention.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,6 +22,7 @@
2222
import java.util.concurrent.Callable;
2323
import java.util.function.Supplier;
2424

25+
import org.gradle.api.InvalidUserDataException;
2526
import org.gradle.api.Project;
2627
import org.gradle.api.file.FileCollection;
2728

@@ -66,7 +67,8 @@ public Object call() throws Exception {
6667
private String resolveMainClass() {
6768
return this.classpathSupplier.get().filter(File::isDirectory).getFiles().stream()
6869
.map(this::findMainClass).filter(Objects::nonNull).findFirst()
69-
.orElse(null);
70+
.orElseThrow(() -> new InvalidUserDataException(
71+
"Main class name has not been configured and it could not be resolved"));
7072
}
7173

7274
private String findMainClass(File file) {

spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/bundling/BootArchive.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ public interface BootArchive extends Task {
3939
* @return the main class name
4040
*/
4141
@Input
42-
@Optional
4342
String getMainClassName();
4443

4544
/**

spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/bundling/BootJar.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ public class BootJar extends Jar implements BootArchive {
4242

4343
private final CopySpec bootInf;
4444

45-
private FileCollection classpath;
46-
4745
private String mainClassName;
4846

47+
private FileCollection classpath;
48+
4949
/**
5050
* Creates a new {@code BootJar} task.
5151
*/
@@ -76,6 +76,13 @@ protected CopyAction createCopyAction() {
7676

7777
@Override
7878
public String getMainClassName() {
79+
if (this.mainClassName == null) {
80+
String manifestStartClass = (String) getManifest().getAttributes()
81+
.get("Start-Class");
82+
if (manifestStartClass != null) {
83+
setMainClassName(manifestStartClass);
84+
}
85+
}
7986
return this.mainClassName;
8087
}
8188

spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/bundling/BootWar.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,13 @@ protected CopyAction createCopyAction() {
6868

6969
@Override
7070
public String getMainClassName() {
71+
if (this.mainClassName == null) {
72+
String manifestStartClass = (String) getManifest().getAttributes()
73+
.get("Start-Class");
74+
if (manifestStartClass != null) {
75+
setMainClassName(manifestStartClass);
76+
}
77+
}
7178
return this.mainClassName;
7279
}
7380

spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/plugin/JavaPluginActionIntegrationTests.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -104,6 +104,14 @@ public void assembleRunsBootJarAndJarIsSkipped() {
104104
assertThat(result.task(":jar").getOutcome()).isEqualTo(TaskOutcome.SKIPPED);
105105
}
106106

107+
@Test
108+
public void errorMessageIsHelpfulWhenMainClassCannotBeResolved() {
109+
BuildResult result = this.gradleBuild.buildAndFail("build", "-PapplyJavaPlugin");
110+
assertThat(result.task(":bootJar").getOutcome()).isEqualTo(TaskOutcome.FAILED);
111+
assertThat(result.getOutput()).contains(
112+
"Main class name has not been configured and it could not be resolved");
113+
}
114+
107115
@Test
108116
public void jarAndBootJarCanBothBeBuilt() {
109117
BuildResult result = this.gradleBuild.build("assemble");

spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/plugin/WarPluginActionIntegrationTests.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -72,4 +72,12 @@ public void warAndBootWarCanBothBeBuilt() {
7272
this.gradleBuild.getProjectDir().getName() + "-boot.war"));
7373
}
7474

75+
@Test
76+
public void errorMessageIsHelpfulWhenMainClassCannotBeResolved() {
77+
BuildResult result = this.gradleBuild.buildAndFail("build", "-PapplyWarPlugin");
78+
assertThat(result.task(":bootWar").getOutcome()).isEqualTo(TaskOutcome.FAILED);
79+
assertThat(result.getOutput()).contains(
80+
"Main class name has not been configured and it could not be resolved");
81+
}
82+
7583
}

0 commit comments

Comments
 (0)