Add mainClassName to springBoot DSL in Gradle plugin
This commit introduces a new mainClassName property on the springBoot DSL provided by the Gradle plugin. It can be used to explicitly configure the mainClassName of the default bootRun, bootJar, and bootWar tasks in a single place. Previously, the only mechanism provided to do so was the mainClassName property that's only available when the application plugin has been applied. Closes gh-10623
This commit is contained in:
@@ -93,8 +93,16 @@ property:
|
||||
include::../gradle/packaging/boot-jar-main-class.gradle[tags=main-class]
|
||||
----
|
||||
|
||||
Alternatively, if the {application-plugin}[`application` plugin] has been applied
|
||||
its `mainClassName` project property can be used:
|
||||
Alternatively, the main class name can be configured project-wide using the
|
||||
`mainClassName` property of the Spring Boot DSL:
|
||||
|
||||
[source,groovy,indent=0,subs="verbatim"]
|
||||
----
|
||||
include::../gradle/packaging/spring-boot-dsl-main-class.gradle[tags=main-class]
|
||||
----
|
||||
|
||||
If the {application-plugin}[`application` plugin] has been applied its `mainClassName`
|
||||
project property can be used for the same purpose:
|
||||
|
||||
[source,groovy,indent=0,subs="verbatim"]
|
||||
----
|
||||
|
||||
@@ -19,8 +19,16 @@ classpath. The main class can also be configured explicitly using the task's
|
||||
include::../gradle/running/boot-run-main.gradle[tags=main]
|
||||
----
|
||||
|
||||
Alternatively, if the {application-plugin}[`application` plugin] has been applied
|
||||
its `mainClassName` project property can be used:
|
||||
Alternatively, the main class name can be configured project-wide using the
|
||||
`mainClassName` property of the Spring Boot DSL:
|
||||
|
||||
[source,groovy,indent=0,subs="verbatim"]
|
||||
----
|
||||
include::../gradle/running/spring-boot-dsl-main-class-name.gradle[tags=main-class]
|
||||
----
|
||||
|
||||
If the {application-plugin}[`application` plugin] has been applied its `mainClassName`
|
||||
project property can be used for the same purpose:
|
||||
|
||||
[source,groovy,indent=0,subs="verbatim"]
|
||||
----
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
buildscript {
|
||||
dependencies {
|
||||
classpath files(pluginClasspath.split(','))
|
||||
}
|
||||
}
|
||||
|
||||
apply plugin: 'org.springframework.boot'
|
||||
apply plugin: 'java'
|
||||
|
||||
// tag::main-class[]
|
||||
springBoot {
|
||||
mainClassName = 'com.example.ExampleApplication'
|
||||
}
|
||||
// end::main-class[]
|
||||
@@ -0,0 +1,21 @@
|
||||
buildscript {
|
||||
dependencies {
|
||||
classpath files(pluginClasspath.split(','))
|
||||
}
|
||||
}
|
||||
|
||||
apply plugin: 'org.springframework.boot'
|
||||
apply plugin: 'java'
|
||||
apply plugin: 'application'
|
||||
|
||||
// tag::main-class[]
|
||||
springBoot {
|
||||
mainClassName = 'com.example.ExampleApplication'
|
||||
}
|
||||
// end::main-class[]
|
||||
|
||||
task configuredMainClass {
|
||||
doLast {
|
||||
println bootRun.mainClassName
|
||||
}
|
||||
}
|
||||
@@ -39,6 +39,8 @@ public class SpringBootExtension {
|
||||
|
||||
private final Project project;
|
||||
|
||||
private String mainClassName;
|
||||
|
||||
/**
|
||||
* Creates a new {@code SpringBootPluginExtension} that is associated with the given
|
||||
* {@code project}.
|
||||
@@ -49,6 +51,24 @@ public class SpringBootExtension {
|
||||
this.project = project;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the main class name of the application.
|
||||
*
|
||||
* @return the name of the application's main class
|
||||
*/
|
||||
public String getMainClassName() {
|
||||
return this.mainClassName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the main class name of the application.
|
||||
*
|
||||
* @param mainClassName the name of the application's main class
|
||||
*/
|
||||
public void setMainClassName(String mainClassName) {
|
||||
this.mainClassName = mainClassName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@link BuildInfo} task named {@code bootBuildInfo} and configures the
|
||||
* Java plugin's {@code classes} task to depend upon it.
|
||||
|
||||
@@ -25,6 +25,7 @@ import java.util.function.Supplier;
|
||||
import org.gradle.api.Project;
|
||||
import org.gradle.api.file.FileCollection;
|
||||
|
||||
import org.springframework.boot.gradle.dsl.SpringBootExtension;
|
||||
import org.springframework.boot.loader.tools.MainClassFinder;
|
||||
|
||||
/**
|
||||
@@ -47,6 +48,12 @@ final class MainClassConvention implements Callable<String> {
|
||||
|
||||
@Override
|
||||
public String call() throws Exception {
|
||||
SpringBootExtension springBootExtension = this.project.getExtensions()
|
||||
.findByType(SpringBootExtension.class);
|
||||
if (springBootExtension != null
|
||||
&& springBootExtension.getMainClassName() != null) {
|
||||
return springBootExtension.getMainClassName();
|
||||
}
|
||||
if (this.project.hasProperty("mainClassName")) {
|
||||
Object mainClassName = this.project.property("mainClassName");
|
||||
if (mainClassName != null) {
|
||||
|
||||
Reference in New Issue
Block a user