Polish "Avoid using Gradle APIs that trigger eager task creation"

See gh-29762
This commit is contained in:
Andy Wilkinson 2022-02-15 08:36:29 +00:00
parent 7cd87acd73
commit c84b35c460
2 changed files with 27 additions and 23 deletions

View File

@ -51,33 +51,37 @@ final class ApplicationPluginAction implements PluginApplicationAction {
distribution.getDistributionBaseName()
.convention((project.provider(() -> applicationConvention.getApplicationName() + "-boot")));
TaskProvider<CreateStartScripts> bootStartScripts = project.getTasks().register("bootStartScripts",
CreateStartScripts.class, (bss) -> {
bss.setDescription(
"Generates OS-specific start scripts to run the project as a Spring Boot application.");
((TemplateBasedScriptGenerator) bss.getUnixStartScriptGenerator()).setTemplate(
project.getResources().getText().fromString(loadResource("/unixStartScript.txt")));
((TemplateBasedScriptGenerator) bss.getWindowsStartScriptGenerator()).setTemplate(
project.getResources().getText().fromString(loadResource("/windowsStartScript.txt")));
project.getConfigurations().all((configuration) -> {
if ("bootArchives".equals(configuration.getName())) {
CopySpec libCopySpec = project.copySpec().into("lib")
.from((Callable<FileCollection>) () -> configuration.getArtifacts().getFiles());
libCopySpec.setFileMode(0644);
distribution.getContents().with(libCopySpec);
bss.setClasspath(configuration.getArtifacts().getFiles());
}
});
bss.getConventionMapping().map("outputDir", () -> new File(project.getBuildDir(), "bootScripts"));
bss.getConventionMapping().map("applicationName", applicationConvention::getApplicationName);
bss.getConventionMapping().map("defaultJvmOpts",
applicationConvention::getApplicationDefaultJvmArgs);
});
CreateStartScripts.class,
(task) -> configureCreateStartScripts(project, applicationConvention, distribution, task));
CopySpec binCopySpec = project.copySpec().into("bin").from(bootStartScripts);
binCopySpec.setFileMode(0755);
distribution.getContents().with(binCopySpec);
}
private void configureCreateStartScripts(Project project, ApplicationPluginConvention applicationConvention,
Distribution distribution, CreateStartScripts createStartScripts) {
createStartScripts
.setDescription("Generates OS-specific start scripts to run the project as a Spring Boot application.");
((TemplateBasedScriptGenerator) createStartScripts.getUnixStartScriptGenerator())
.setTemplate(project.getResources().getText().fromString(loadResource("/unixStartScript.txt")));
((TemplateBasedScriptGenerator) createStartScripts.getWindowsStartScriptGenerator())
.setTemplate(project.getResources().getText().fromString(loadResource("/windowsStartScript.txt")));
project.getConfigurations().all((configuration) -> {
if ("bootArchives".equals(configuration.getName())) {
CopySpec libCopySpec = project.copySpec().into("lib")
.from((Callable<FileCollection>) () -> configuration.getArtifacts().getFiles());
libCopySpec.setFileMode(0644);
distribution.getContents().with(libCopySpec);
createStartScripts.setClasspath(configuration.getArtifacts().getFiles());
}
});
createStartScripts.getConventionMapping().map("outputDir",
() -> new File(project.getBuildDir(), "bootScripts"));
createStartScripts.getConventionMapping().map("applicationName", applicationConvention::getApplicationName);
createStartScripts.getConventionMapping().map("defaultJvmOpts",
applicationConvention::getApplicationDefaultJvmArgs);
}
@Override
public Class<? extends Plugin<Project>> getPluginClass() {
return ApplicationPlugin.class;

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2021 the original author or authors.
* Copyright 2012-2022 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.