Update bootJar and bootWar to use new main class resolution mechanism
See gh-22922
This commit is contained in:
parent
c078a48064
commit
b1c4af4081
|
@ -17,15 +17,10 @@
|
||||||
package org.springframework.boot.gradle.plugin;
|
package org.springframework.boot.gradle.plugin;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
|
||||||
import java.nio.charset.StandardCharsets;
|
|
||||||
import java.nio.file.Files;
|
|
||||||
import java.nio.file.Path;
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.concurrent.Callable;
|
|
||||||
|
|
||||||
import org.gradle.api.Action;
|
import org.gradle.api.Action;
|
||||||
import org.gradle.api.Plugin;
|
import org.gradle.api.Plugin;
|
||||||
|
@ -37,13 +32,10 @@ import org.gradle.api.attributes.Bundling;
|
||||||
import org.gradle.api.attributes.LibraryElements;
|
import org.gradle.api.attributes.LibraryElements;
|
||||||
import org.gradle.api.attributes.Usage;
|
import org.gradle.api.attributes.Usage;
|
||||||
import org.gradle.api.file.FileCollection;
|
import org.gradle.api.file.FileCollection;
|
||||||
import org.gradle.api.file.RegularFileProperty;
|
|
||||||
import org.gradle.api.internal.artifacts.dsl.LazyPublishArtifact;
|
import org.gradle.api.internal.artifacts.dsl.LazyPublishArtifact;
|
||||||
import org.gradle.api.model.ObjectFactory;
|
import org.gradle.api.model.ObjectFactory;
|
||||||
import org.gradle.api.plugins.ApplicationPlugin;
|
import org.gradle.api.plugins.ApplicationPlugin;
|
||||||
import org.gradle.api.plugins.BasePlugin;
|
import org.gradle.api.plugins.BasePlugin;
|
||||||
import org.gradle.api.plugins.Convention;
|
|
||||||
import org.gradle.api.plugins.JavaApplication;
|
|
||||||
import org.gradle.api.plugins.JavaPlugin;
|
import org.gradle.api.plugins.JavaPlugin;
|
||||||
import org.gradle.api.plugins.JavaPluginConvention;
|
import org.gradle.api.plugins.JavaPluginConvention;
|
||||||
import org.gradle.api.provider.Provider;
|
import org.gradle.api.provider.Provider;
|
||||||
|
@ -51,7 +43,6 @@ import org.gradle.api.tasks.SourceSet;
|
||||||
import org.gradle.api.tasks.TaskProvider;
|
import org.gradle.api.tasks.TaskProvider;
|
||||||
import org.gradle.api.tasks.compile.JavaCompile;
|
import org.gradle.api.tasks.compile.JavaCompile;
|
||||||
|
|
||||||
import org.springframework.boot.gradle.dsl.SpringBootExtension;
|
|
||||||
import org.springframework.boot.gradle.tasks.bundling.BootBuildImage;
|
import org.springframework.boot.gradle.tasks.bundling.BootBuildImage;
|
||||||
import org.springframework.boot.gradle.tasks.bundling.BootJar;
|
import org.springframework.boot.gradle.tasks.bundling.BootJar;
|
||||||
import org.springframework.boot.gradle.tasks.run.BootRun;
|
import org.springframework.boot.gradle.tasks.run.BootRun;
|
||||||
|
@ -86,8 +77,7 @@ final class JavaPluginAction implements PluginApplicationAction {
|
||||||
TaskProvider<BootJar> bootJar = configureBootJarTask(project);
|
TaskProvider<BootJar> bootJar = configureBootJarTask(project);
|
||||||
configureBootBuildImageTask(project, bootJar);
|
configureBootBuildImageTask(project, bootJar);
|
||||||
configureArtifactPublication(bootJar);
|
configureArtifactPublication(bootJar);
|
||||||
TaskProvider<ResolveMainClassName> resolveMainClassName = configureResolveMainClassNameTask(project);
|
configureBootRunTask(project);
|
||||||
configureBootRunTask(project, resolveMainClassName);
|
|
||||||
configureUtf8Encoding(project);
|
configureUtf8Encoding(project);
|
||||||
configureParametersCompilerArg(project);
|
configureParametersCompilerArg(project);
|
||||||
configureAdditionalMetadataLocations(project);
|
configureAdditionalMetadataLocations(project);
|
||||||
|
@ -102,59 +92,26 @@ final class JavaPluginAction implements PluginApplicationAction {
|
||||||
.configure((task) -> task.dependsOn(this.singlePublishedArtifact));
|
.configure((task) -> task.dependsOn(this.singlePublishedArtifact));
|
||||||
}
|
}
|
||||||
|
|
||||||
private TaskProvider<ResolveMainClassName> configureResolveMainClassNameTask(Project project) {
|
|
||||||
Convention convention = project.getConvention();
|
|
||||||
return project.getTasks().register("resolveMainClassName", ResolveMainClassName.class,
|
|
||||||
(resolveMainClassName) -> {
|
|
||||||
resolveMainClassName.setClasspath(
|
|
||||||
javaPluginConvention(project).getSourceSets().findByName(SourceSet.MAIN_SOURCE_SET_NAME)
|
|
||||||
.getRuntimeClasspath().filter(new JarTypeFileSpec()));
|
|
||||||
resolveMainClassName.getConfiguredMainClassName().convention(project.provider(() -> {
|
|
||||||
JavaApplication javaApplication = convention.findByType(JavaApplication.class);
|
|
||||||
String javaApplicationMainClass = null;
|
|
||||||
if (javaApplication != null) {
|
|
||||||
try {
|
|
||||||
javaApplicationMainClass = javaApplication.getMainClass().getOrNull();
|
|
||||||
}
|
|
||||||
catch (NoSuchMethodError ex) {
|
|
||||||
javaApplicationMainClass = javaApplication.getMainClassName();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (javaApplicationMainClass != null) {
|
|
||||||
return javaApplicationMainClass;
|
|
||||||
}
|
|
||||||
SpringBootExtension springBootExtension = project.getExtensions()
|
|
||||||
.findByType(SpringBootExtension.class);
|
|
||||||
if (springBootExtension != null) {
|
|
||||||
return springBootExtension.getMainClass().getOrNull();
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}));
|
|
||||||
resolveMainClassName.getOutputFile()
|
|
||||||
.set(project.getLayout().getBuildDirectory().file("spring-boot-main-class-name"));
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
private TaskProvider<BootJar> configureBootJarTask(Project project) {
|
private TaskProvider<BootJar> configureBootJarTask(Project project) {
|
||||||
|
SourceSet mainSourceSet = javaPluginConvention(project).getSourceSets()
|
||||||
|
.getByName(SourceSet.MAIN_SOURCE_SET_NAME);
|
||||||
|
Configuration developmentOnly = project.getConfigurations()
|
||||||
|
.getByName(SpringBootPlugin.DEVELOPMENT_ONLY_CONFIGURATION_NAME);
|
||||||
|
Configuration productionRuntimeClasspath = project.getConfigurations()
|
||||||
|
.getByName(SpringBootPlugin.PRODUCTION_RUNTIME_CLASSPATH_NAME);
|
||||||
|
FileCollection classpath = mainSourceSet.getRuntimeClasspath()
|
||||||
|
.minus((developmentOnly.minus(productionRuntimeClasspath))).filter(new JarTypeFileSpec());
|
||||||
|
TaskProvider<ResolveMainClassName> resolveMainClassName = ResolveMainClassName
|
||||||
|
.registerForTask(SpringBootPlugin.BOOT_JAR_TASK_NAME, project, classpath);
|
||||||
return project.getTasks().register(SpringBootPlugin.BOOT_JAR_TASK_NAME, BootJar.class, (bootJar) -> {
|
return project.getTasks().register(SpringBootPlugin.BOOT_JAR_TASK_NAME, BootJar.class, (bootJar) -> {
|
||||||
bootJar.setDescription(
|
bootJar.setDescription(
|
||||||
"Assembles an executable jar archive containing the main classes and their dependencies.");
|
"Assembles an executable jar archive containing the main classes and their dependencies.");
|
||||||
bootJar.setGroup(BasePlugin.BUILD_GROUP);
|
bootJar.setGroup(BasePlugin.BUILD_GROUP);
|
||||||
SourceSet mainSourceSet = javaPluginConvention(project).getSourceSets()
|
bootJar.classpath(classpath);
|
||||||
.getByName(SourceSet.MAIN_SOURCE_SET_NAME);
|
Provider<String> manifestStartClass = project
|
||||||
bootJar.classpath((Callable<FileCollection>) () -> {
|
.provider(() -> (String) bootJar.getManifest().getAttributes().get("Start-Class"));
|
||||||
Configuration developmentOnly = project.getConfigurations()
|
bootJar.getMainClass().convention(resolveMainClassName.flatMap((resolver) -> manifestStartClass.isPresent()
|
||||||
.getByName(SpringBootPlugin.DEVELOPMENT_ONLY_CONFIGURATION_NAME);
|
? manifestStartClass : resolveMainClassName.get().readMainClassName()));
|
||||||
Configuration productionRuntimeClasspath = project.getConfigurations()
|
|
||||||
.getByName(SpringBootPlugin.PRODUCTION_RUNTIME_CLASSPATH_NAME);
|
|
||||||
return mainSourceSet.getRuntimeClasspath().minus((developmentOnly.minus(productionRuntimeClasspath)))
|
|
||||||
.filter(new JarTypeFileSpec());
|
|
||||||
});
|
|
||||||
bootJar.getMainClass().convention(project.provider(() -> {
|
|
||||||
String manifestStartClass = (String) bootJar.getManifest().getAttributes().get("Start-Class");
|
|
||||||
return (manifestStartClass != null) ? manifestStartClass
|
|
||||||
: new MainClassConvention(project, bootJar::getClasspath).call();
|
|
||||||
}));
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -172,41 +129,28 @@ final class JavaPluginAction implements PluginApplicationAction {
|
||||||
this.singlePublishedArtifact.addCandidate(artifact);
|
this.singlePublishedArtifact.addCandidate(artifact);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void configureBootRunTask(Project project, TaskProvider<ResolveMainClassName> resolveMainClassName) {
|
private void configureBootRunTask(Project project) {
|
||||||
|
FileCollection classpath = javaPluginConvention(project).getSourceSets()
|
||||||
|
.findByName(SourceSet.MAIN_SOURCE_SET_NAME).getRuntimeClasspath().filter(new JarTypeFileSpec());
|
||||||
|
TaskProvider<ResolveMainClassName> resolveProvider = ResolveMainClassName.registerForTask("bootRun", project,
|
||||||
|
classpath);
|
||||||
project.getTasks().register("bootRun", BootRun.class, (run) -> {
|
project.getTasks().register("bootRun", BootRun.class, (run) -> {
|
||||||
run.setDescription("Runs this project as a Spring Boot application.");
|
run.setDescription("Runs this project as a Spring Boot application.");
|
||||||
run.setGroup(ApplicationPlugin.APPLICATION_GROUP);
|
run.setGroup(ApplicationPlugin.APPLICATION_GROUP);
|
||||||
run.classpath(javaPluginConvention(project).getSourceSets().findByName(SourceSet.MAIN_SOURCE_SET_NAME)
|
run.classpath(classpath);
|
||||||
.getRuntimeClasspath().filter(new JarTypeFileSpec()));
|
|
||||||
run.getConventionMapping().map("jvmArgs", () -> {
|
run.getConventionMapping().map("jvmArgs", () -> {
|
||||||
if (project.hasProperty("applicationDefaultJvmArgs")) {
|
if (project.hasProperty("applicationDefaultJvmArgs")) {
|
||||||
return project.property("applicationDefaultJvmArgs");
|
return project.property("applicationDefaultJvmArgs");
|
||||||
}
|
}
|
||||||
return Collections.emptyList();
|
return Collections.emptyList();
|
||||||
});
|
});
|
||||||
run.dependsOn(resolveMainClassName);
|
|
||||||
run.getInputs().file(resolveMainClassName.map((task) -> task.getOutputFile()));
|
|
||||||
try {
|
try {
|
||||||
run.getMainClass().set(resolveMainClassName.flatMap((task) -> readMainClassName(task.getOutputFile())));
|
run.getMainClass().convention(resolveProvider.flatMap(ResolveMainClassName::readMainClassName));
|
||||||
}
|
}
|
||||||
catch (NoSuchMethodError ex) {
|
catch (NoSuchMethodError ex) {
|
||||||
|
run.getInputs().file(resolveProvider.map((task) -> task.getOutputFile()));
|
||||||
run.conventionMapping("main",
|
run.conventionMapping("main",
|
||||||
() -> resolveMainClassName.flatMap((task) -> readMainClassName(task.getOutputFile())).get());
|
() -> resolveProvider.flatMap(ResolveMainClassName::readMainClassName).get());
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
private Provider<String> readMainClassName(RegularFileProperty outputFile) {
|
|
||||||
return outputFile.map((file) -> {
|
|
||||||
Path output = file.getAsFile().toPath();
|
|
||||||
if (!Files.exists(output)) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
return new String(Files.readAllBytes(output), StandardCharsets.UTF_8);
|
|
||||||
}
|
|
||||||
catch (IOException ex) {
|
|
||||||
throw new RuntimeException("Failed to read main class name from '" + output + "'");
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,109 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright 2012-2020 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.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* https://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.springframework.boot.gradle.plugin;
|
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.lang.reflect.Method;
|
|
||||||
import java.util.Objects;
|
|
||||||
import java.util.concurrent.Callable;
|
|
||||||
import java.util.function.Supplier;
|
|
||||||
|
|
||||||
import org.gradle.api.InvalidUserDataException;
|
|
||||||
import org.gradle.api.Project;
|
|
||||||
import org.gradle.api.file.FileCollection;
|
|
||||||
import org.gradle.api.plugins.JavaApplication;
|
|
||||||
import org.gradle.api.provider.Property;
|
|
||||||
|
|
||||||
import org.springframework.boot.gradle.dsl.SpringBootExtension;
|
|
||||||
import org.springframework.boot.loader.tools.MainClassFinder;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A {@link Callable} that provides a convention for the project's main class name.
|
|
||||||
*
|
|
||||||
* @author Andy Wilkinson
|
|
||||||
*/
|
|
||||||
final class MainClassConvention implements Callable<String> {
|
|
||||||
|
|
||||||
private static final String SPRING_BOOT_APPLICATION_CLASS_NAME = "org.springframework.boot.autoconfigure.SpringBootApplication";
|
|
||||||
|
|
||||||
private final Project project;
|
|
||||||
|
|
||||||
private final Supplier<FileCollection> classpathSupplier;
|
|
||||||
|
|
||||||
MainClassConvention(Project project, Supplier<FileCollection> classpathSupplier) {
|
|
||||||
this.project = project;
|
|
||||||
this.classpathSupplier = classpathSupplier;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String call() throws Exception {
|
|
||||||
SpringBootExtension springBootExtension = this.project.getExtensions().findByType(SpringBootExtension.class);
|
|
||||||
if (springBootExtension != null) {
|
|
||||||
String mainClass = springBootExtension.getMainClass().getOrNull();
|
|
||||||
if (mainClass != null) {
|
|
||||||
return mainClass;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
String javaApplicationMainClass = getJavaApplicationMainClass();
|
|
||||||
return (javaApplicationMainClass != null) ? javaApplicationMainClass : resolveMainClass();
|
|
||||||
}
|
|
||||||
|
|
||||||
@SuppressWarnings({ "unchecked", "deprecation" })
|
|
||||||
private String getJavaApplicationMainClass() {
|
|
||||||
JavaApplication javaApplication = this.project.getConvention().findByType(JavaApplication.class);
|
|
||||||
if (javaApplication == null) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
Method getMainClass = findMethod(JavaApplication.class, "getMainClass");
|
|
||||||
if (getMainClass != null) {
|
|
||||||
try {
|
|
||||||
Property<String> mainClass = (Property<String>) getMainClass.invoke(javaApplication);
|
|
||||||
return mainClass.getOrElse(null);
|
|
||||||
}
|
|
||||||
catch (Exception ex) {
|
|
||||||
// Continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return javaApplication.getMainClassName();
|
|
||||||
}
|
|
||||||
|
|
||||||
private static Method findMethod(Class<?> type, String name) {
|
|
||||||
for (Method candidate : type.getMethods()) {
|
|
||||||
if (candidate.getName().equals(name)) {
|
|
||||||
return candidate;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
private String resolveMainClass() {
|
|
||||||
return this.classpathSupplier.get().filter(File::isDirectory).getFiles().stream().map(this::findMainClass)
|
|
||||||
.filter(Objects::nonNull).findFirst().orElseThrow(() -> new InvalidUserDataException(
|
|
||||||
"Main class name has not been configured and it could not be resolved"));
|
|
||||||
}
|
|
||||||
|
|
||||||
private String findMainClass(File file) {
|
|
||||||
try {
|
|
||||||
return MainClassFinder.findSingleMainClass(file, SPRING_BOOT_APPLICATION_CLASS_NAME);
|
|
||||||
}
|
|
||||||
catch (IOException ex) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -20,21 +20,29 @@ import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
import java.nio.file.StandardOpenOption;
|
import java.nio.file.StandardOpenOption;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
import org.gradle.api.DefaultTask;
|
import org.gradle.api.DefaultTask;
|
||||||
import org.gradle.api.InvalidUserDataException;
|
import org.gradle.api.InvalidUserDataException;
|
||||||
|
import org.gradle.api.Project;
|
||||||
import org.gradle.api.Task;
|
import org.gradle.api.Task;
|
||||||
import org.gradle.api.file.FileCollection;
|
import org.gradle.api.file.FileCollection;
|
||||||
import org.gradle.api.file.RegularFileProperty;
|
import org.gradle.api.file.RegularFileProperty;
|
||||||
|
import org.gradle.api.plugins.BasePlugin;
|
||||||
|
import org.gradle.api.plugins.Convention;
|
||||||
|
import org.gradle.api.plugins.JavaApplication;
|
||||||
import org.gradle.api.provider.Property;
|
import org.gradle.api.provider.Property;
|
||||||
|
import org.gradle.api.provider.Provider;
|
||||||
import org.gradle.api.tasks.Classpath;
|
import org.gradle.api.tasks.Classpath;
|
||||||
import org.gradle.api.tasks.Input;
|
import org.gradle.api.tasks.Input;
|
||||||
import org.gradle.api.tasks.Optional;
|
import org.gradle.api.tasks.Optional;
|
||||||
import org.gradle.api.tasks.OutputFile;
|
import org.gradle.api.tasks.OutputFile;
|
||||||
import org.gradle.api.tasks.TaskAction;
|
import org.gradle.api.tasks.TaskAction;
|
||||||
|
import org.gradle.api.tasks.TaskProvider;
|
||||||
|
|
||||||
|
import org.springframework.boot.gradle.dsl.SpringBootExtension;
|
||||||
import org.springframework.boot.loader.tools.MainClassFinder;
|
import org.springframework.boot.loader.tools.MainClassFinder;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -102,11 +110,11 @@ public class ResolveMainClassName extends DefaultTask {
|
||||||
|
|
||||||
@TaskAction
|
@TaskAction
|
||||||
void resolveAndStoreMainClassName() throws IOException {
|
void resolveAndStoreMainClassName() throws IOException {
|
||||||
String mainClassName = resolveMainClassName();
|
|
||||||
File outputFile = this.outputFile.getAsFile().get();
|
File outputFile = this.outputFile.getAsFile().get();
|
||||||
outputFile.getParentFile().mkdirs();
|
outputFile.getParentFile().mkdirs();
|
||||||
Files.write(this.outputFile.get().getAsFile().toPath(), mainClassName.getBytes(StandardCharsets.UTF_8),
|
String mainClassName = resolveMainClassName();
|
||||||
StandardOpenOption.WRITE, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
|
Files.write(outputFile.toPath(), mainClassName.getBytes(StandardCharsets.UTF_8), StandardOpenOption.WRITE,
|
||||||
|
StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
|
||||||
}
|
}
|
||||||
|
|
||||||
private String resolveMainClassName() {
|
private String resolveMainClassName() {
|
||||||
|
@ -115,8 +123,7 @@ public class ResolveMainClassName extends DefaultTask {
|
||||||
return configuredMainClass;
|
return configuredMainClass;
|
||||||
}
|
}
|
||||||
return getClasspath().filter(File::isDirectory).getFiles().stream().map(this::findMainClass)
|
return getClasspath().filter(File::isDirectory).getFiles().stream().map(this::findMainClass)
|
||||||
.filter(Objects::nonNull).findFirst().orElseThrow(() -> new InvalidUserDataException(
|
.filter(Objects::nonNull).findFirst().orElse("");
|
||||||
"Main class name has not been configured and it could not be resolved"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private String findMainClass(File file) {
|
private String findMainClass(File file) {
|
||||||
|
@ -128,4 +135,58 @@ public class ResolveMainClassName extends DefaultTask {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Provider<String> readMainClassName() {
|
||||||
|
return this.outputFile.map((file) -> {
|
||||||
|
if (file.getAsFile().length() == 0) {
|
||||||
|
throw new InvalidUserDataException(
|
||||||
|
"Main class name has not been configured and it could not be resolved");
|
||||||
|
}
|
||||||
|
Path output = file.getAsFile().toPath();
|
||||||
|
try {
|
||||||
|
return new String(Files.readAllBytes(output), StandardCharsets.UTF_8);
|
||||||
|
}
|
||||||
|
catch (IOException ex) {
|
||||||
|
throw new RuntimeException("Failed to read main class name from '" + output + "'");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
static TaskProvider<ResolveMainClassName> registerForTask(String taskName, Project project,
|
||||||
|
FileCollection classpath) {
|
||||||
|
TaskProvider<ResolveMainClassName> resolveMainClassNameProvider = project.getTasks()
|
||||||
|
.register(taskName + "MainClassName", ResolveMainClassName.class, (resolveMainClassName) -> {
|
||||||
|
Convention convention = project.getConvention();
|
||||||
|
resolveMainClassName.setDescription(
|
||||||
|
"Resolves the name of the application's main class for the " + taskName + " task.");
|
||||||
|
resolveMainClassName.setGroup(BasePlugin.BUILD_GROUP);
|
||||||
|
resolveMainClassName.setClasspath(classpath);
|
||||||
|
resolveMainClassName.getConfiguredMainClassName().convention(project.provider(() -> {
|
||||||
|
String javaApplicationMainClass = getJavaApplicationMainClass(convention);
|
||||||
|
if (javaApplicationMainClass != null) {
|
||||||
|
return javaApplicationMainClass;
|
||||||
|
}
|
||||||
|
SpringBootExtension springBootExtension = project.getExtensions()
|
||||||
|
.findByType(SpringBootExtension.class);
|
||||||
|
return springBootExtension.getMainClass().getOrNull();
|
||||||
|
}));
|
||||||
|
resolveMainClassName.getOutputFile()
|
||||||
|
.set(project.getLayout().getBuildDirectory().file(taskName + "MainClassName"));
|
||||||
|
});
|
||||||
|
return resolveMainClassNameProvider;
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("deprecation")
|
||||||
|
private static String getJavaApplicationMainClass(Convention convention) {
|
||||||
|
JavaApplication javaApplication = convention.findByType(JavaApplication.class);
|
||||||
|
if (javaApplication == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
return javaApplication.getMainClass().getOrNull();
|
||||||
|
}
|
||||||
|
catch (NoSuchMethodError ex) {
|
||||||
|
return javaApplication.getMainClassName();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,6 +25,9 @@ import org.gradle.api.file.FileCollection;
|
||||||
import org.gradle.api.internal.artifacts.dsl.LazyPublishArtifact;
|
import org.gradle.api.internal.artifacts.dsl.LazyPublishArtifact;
|
||||||
import org.gradle.api.plugins.BasePlugin;
|
import org.gradle.api.plugins.BasePlugin;
|
||||||
import org.gradle.api.plugins.WarPlugin;
|
import org.gradle.api.plugins.WarPlugin;
|
||||||
|
import org.gradle.api.provider.Provider;
|
||||||
|
import org.gradle.api.tasks.SourceSet;
|
||||||
|
import org.gradle.api.tasks.SourceSetContainer;
|
||||||
import org.gradle.api.tasks.TaskProvider;
|
import org.gradle.api.tasks.TaskProvider;
|
||||||
|
|
||||||
import org.springframework.boot.gradle.tasks.bundling.BootWar;
|
import org.springframework.boot.gradle.tasks.bundling.BootWar;
|
||||||
|
@ -60,23 +63,31 @@ class WarPluginAction implements PluginApplicationAction {
|
||||||
}
|
}
|
||||||
|
|
||||||
private TaskProvider<BootWar> configureBootWarTask(Project project) {
|
private TaskProvider<BootWar> configureBootWarTask(Project project) {
|
||||||
return project.getTasks().register(SpringBootPlugin.BOOT_WAR_TASK_NAME, BootWar.class, (bootWar) -> {
|
Configuration developmentOnly = project.getConfigurations()
|
||||||
bootWar.setGroup(BasePlugin.BUILD_GROUP);
|
.getByName(SpringBootPlugin.DEVELOPMENT_ONLY_CONFIGURATION_NAME);
|
||||||
bootWar.setDescription("Assembles an executable war archive containing webapp"
|
Configuration productionRuntimeClasspath = project.getConfigurations()
|
||||||
+ " content, and the main classes and their dependencies.");
|
.getByName(SpringBootPlugin.PRODUCTION_RUNTIME_CLASSPATH_NAME);
|
||||||
bootWar.providedClasspath(providedRuntimeConfiguration(project));
|
FileCollection classpath = project.getConvention().getByType(SourceSetContainer.class)
|
||||||
Configuration developmentOnly = project.getConfigurations()
|
.getByName(SourceSet.MAIN_SOURCE_SET_NAME).getRuntimeClasspath()
|
||||||
.getByName(SpringBootPlugin.DEVELOPMENT_ONLY_CONFIGURATION_NAME);
|
.minus(providedRuntimeConfiguration(project)).minus((developmentOnly.minus(productionRuntimeClasspath)))
|
||||||
Configuration productionRuntimeClasspath = project.getConfigurations()
|
.filter(new JarTypeFileSpec());
|
||||||
.getByName(SpringBootPlugin.PRODUCTION_RUNTIME_CLASSPATH_NAME);
|
TaskProvider<ResolveMainClassName> resolveMainClassName = ResolveMainClassName
|
||||||
bootWar.setClasspath(bootWar.getClasspath().minus((developmentOnly.minus(productionRuntimeClasspath)))
|
.registerForTask(SpringBootPlugin.BOOT_WAR_TASK_NAME, project, classpath);
|
||||||
.filter(new JarTypeFileSpec()));
|
TaskProvider<BootWar> bootWarProvider = project.getTasks().register(SpringBootPlugin.BOOT_WAR_TASK_NAME,
|
||||||
bootWar.getMainClass().convention(project.provider(() -> {
|
BootWar.class, (bootWar) -> {
|
||||||
String manifestStartClass = (String) bootWar.getManifest().getAttributes().get("Start-Class");
|
bootWar.setGroup(BasePlugin.BUILD_GROUP);
|
||||||
return (manifestStartClass != null) ? manifestStartClass
|
bootWar.setDescription("Assembles an executable war archive containing webapp"
|
||||||
: new MainClassConvention(project, bootWar::getClasspath).call();
|
+ " content, and the main classes and their dependencies.");
|
||||||
}));
|
bootWar.providedClasspath(providedRuntimeConfiguration(project));
|
||||||
});
|
bootWar.setClasspath(classpath);
|
||||||
|
Provider<String> manifestStartClass = project
|
||||||
|
.provider(() -> (String) bootWar.getManifest().getAttributes().get("Start-Class"));
|
||||||
|
bootWar.getMainClass()
|
||||||
|
.convention(resolveMainClassName.flatMap((resolver) -> manifestStartClass.isPresent()
|
||||||
|
? manifestStartClass : resolveMainClassName.get().readMainClassName()));
|
||||||
|
});
|
||||||
|
bootWarProvider.map((bootWar) -> bootWar.getClasspath());
|
||||||
|
return bootWarProvider;
|
||||||
}
|
}
|
||||||
|
|
||||||
private FileCollection providedRuntimeConfiguration(Project project) {
|
private FileCollection providedRuntimeConfiguration(Project project) {
|
||||||
|
|
|
@ -66,8 +66,7 @@ final class GradleCompatibilityExtension implements TestTemplateInvocationContex
|
||||||
boolean configurationCache = AnnotationUtils
|
boolean configurationCache = AnnotationUtils
|
||||||
.findAnnotation(context.getRequiredTestClass(), GradleCompatibility.class).get()
|
.findAnnotation(context.getRequiredTestClass(), GradleCompatibility.class).get()
|
||||||
.configurationCache();
|
.configurationCache();
|
||||||
if (configurationCache
|
if (configurationCache && GradleVersion.version(version).compareTo(GradleVersion.version("6.7")) >= 0) {
|
||||||
&& GradleVersion.version(version).compareTo(GradleVersion.version("6.7-rc-1")) >= 0) {
|
|
||||||
invocationContexts.add(new GradleVersionTestTemplateInvocationContext(version, true));
|
invocationContexts.add(new GradleVersionTestTemplateInvocationContext(version, true));
|
||||||
}
|
}
|
||||||
return invocationContexts.stream();
|
return invocationContexts.stream();
|
||||||
|
|
|
@ -1,81 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright 2012-2020 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.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* https://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.springframework.boot.gradle.plugin;
|
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
import org.gradle.api.Project;
|
|
||||||
import org.gradle.api.plugins.ApplicationPlugin;
|
|
||||||
import org.gradle.api.plugins.JavaApplication;
|
|
||||||
import org.gradle.testfixtures.ProjectBuilder;
|
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
import org.junit.jupiter.api.io.TempDir;
|
|
||||||
|
|
||||||
import org.springframework.boot.gradle.dsl.SpringBootExtension;
|
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Integration tests for {@link MainClassConvention}.
|
|
||||||
*
|
|
||||||
* @author Andy Wilkinson
|
|
||||||
*/
|
|
||||||
class MainClassConventionTests {
|
|
||||||
|
|
||||||
@TempDir
|
|
||||||
File temp;
|
|
||||||
|
|
||||||
private Project project;
|
|
||||||
|
|
||||||
private MainClassConvention convention;
|
|
||||||
|
|
||||||
@BeforeEach
|
|
||||||
void createConvention() throws IOException {
|
|
||||||
this.project = ProjectBuilder.builder().withProjectDir(this.temp).build();
|
|
||||||
this.convention = new MainClassConvention(this.project, () -> null);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void javaApplicationExtensionMainClassNameIsUsed() throws Exception {
|
|
||||||
this.project.getPlugins().apply(ApplicationPlugin.class);
|
|
||||||
JavaApplication extension = this.project.getExtensions().findByType(JavaApplication.class);
|
|
||||||
extension.getMainClass().set("com.example.MainClass");
|
|
||||||
assertThat(this.convention.call()).isEqualTo("com.example.MainClass");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void springBootExtensionMainClassNameIsUsed() throws Exception {
|
|
||||||
SpringBootExtension extension = this.project.getExtensions().create("springBoot", SpringBootExtension.class,
|
|
||||||
this.project);
|
|
||||||
extension.getMainClass().set("com.example.MainClass");
|
|
||||||
assertThat(this.convention.call()).isEqualTo("com.example.MainClass");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void springBootExtensionMainClassNameIsUsedInPreferenceToJavaApplicationExtensionMainClassName() throws Exception {
|
|
||||||
this.project.getPlugins().apply(ApplicationPlugin.class);
|
|
||||||
JavaApplication javaApplication = this.project.getExtensions().findByType(JavaApplication.class);
|
|
||||||
javaApplication.getMainClass().set("com.example.JavaApplicationMainClass");
|
|
||||||
SpringBootExtension extension = this.project.getExtensions().create("springBoot", SpringBootExtension.class,
|
|
||||||
this.project);
|
|
||||||
extension.getMainClass().set("com.example.SpringBootExtensionMainClass");
|
|
||||||
assertThat(this.convention.call()).isEqualTo("com.example.SpringBootExtensionMainClass");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -34,6 +34,7 @@ import org.junit.jupiter.api.TestTemplate;
|
||||||
|
|
||||||
import org.springframework.boot.gradle.testkit.GradleBuild;
|
import org.springframework.boot.gradle.testkit.GradleBuild;
|
||||||
import org.springframework.boot.loader.tools.FileUtils;
|
import org.springframework.boot.loader.tools.FileUtils;
|
||||||
|
import org.springframework.util.FileSystemUtils;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
@ -188,6 +189,27 @@ abstract class AbstractBootArchiveIntegrationTests {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestTemplate
|
||||||
|
void startClassIsSetByResolvingTheMainClass() throws IOException {
|
||||||
|
copyMainClassApplication();
|
||||||
|
assertThat(this.gradleBuild.build(this.taskName).task(":" + this.taskName).getOutcome())
|
||||||
|
.isEqualTo(TaskOutcome.SUCCESS);
|
||||||
|
try (JarFile jarFile = new JarFile(new File(this.gradleBuild.getProjectDir(), "build/libs").listFiles()[0])) {
|
||||||
|
Attributes mainAttributes = jarFile.getManifest().getMainAttributes();
|
||||||
|
assertThat(mainAttributes.getValue("Start-Class")).isEqualTo("com.example.main.CustomMainClass");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void copyMainClassApplication() throws IOException {
|
||||||
|
copyApplication("main");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void copyApplication(String name) throws IOException {
|
||||||
|
File output = new File(this.gradleBuild.getProjectDir(), "src/main/java/com/example/" + name);
|
||||||
|
output.mkdirs();
|
||||||
|
FileSystemUtils.copyRecursively(new File("src/test/java/com/example/" + name), output);
|
||||||
|
}
|
||||||
|
|
||||||
private void createStandardJar(File location) throws IOException {
|
private void createStandardJar(File location) throws IOException {
|
||||||
createJar(location, (attributes) -> {
|
createJar(location, (attributes) -> {
|
||||||
});
|
});
|
||||||
|
|
|
@ -0,0 +1,5 @@
|
||||||
|
plugins {
|
||||||
|
id 'java'
|
||||||
|
id 'application'
|
||||||
|
id 'org.springframework.boot' version '{version}'
|
||||||
|
}
|
|
@ -0,0 +1,5 @@
|
||||||
|
plugins {
|
||||||
|
id 'war'
|
||||||
|
id 'application'
|
||||||
|
id 'org.springframework.boot' version '{version}'
|
||||||
|
}
|
Loading…
Reference in New Issue