When we use Spring Boot, bootrun
is automatically registered as a Gradle task. I followed its steps, how it is registered
The task is added when the plugin is added
bootrun
can be used when the Spring Boot is added into build.gradle
.
When loading Spring Boot plugin with apply plugin
description, SpringBootPlugin
‘s execute
is executed. And JavaPluginAction
plugin is added in it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
private void registerPluginActions(Project project, Configuration bootArchives) { SinglePublishedArtifact singlePublishedArtifact = new SinglePublishedArtifact( bootArchives.getArtifacts()); List<PluginApplicationAction> actions = Arrays.asList( new JavaPluginAction(singlePublishedArtifact), new WarPluginAction(singlePublishedArtifact), new MavenPluginAction(bootArchives.getUploadTaskName()), new DependencyManagementPluginAction(), new ApplicationPluginAction(), new KotlinPluginAction()); for (PluginApplicationAction action : actions) { Class<? extends Plugin<? extends Project>> pluginClass = action .getPluginClass(); if (pluginClass != null) { project.getPlugins().withType(pluginClass, (plugin) -> action.execute(project)); } } } |
There, KotlinPluginAction
will add Kotlin Gradle plugin.
JavaPluginAction
is loaded as plugin, so the execute
function in it will be executed. JavaPluginAction
implements PluginApplicationAction
interface.
PluginApplicationAction
is the derived interface from Action<Project>
, which has a object with one additional method to Action<Project>
.
1 2 3 4 5 6 7 8 9 10 11 |
@Override public void execute(Project project) { disableJarTask(project); configureBuildTask(project); BootJar bootJar = configureBootJarTask(project); configureArtifactPublication(bootJar); configureBootRunTask(project); configureUtf8Encoding(project); configureParametersCompilerArg(project); configureAdditionalMetadataLocations(project); } |
この中の configureBootRunTask
で タスク bootrun
が追加されます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
private void configureBootRunTask(Project project) { JavaPluginConvention javaConvention = project.getConvention() .getPlugin(JavaPluginConvention.class); BootRun run = project.getTasks().create("bootRun", BootRun.class); run.setDescription("Runs this project as a Spring Boot application."); run.setGroup(ApplicationPlugin.APPLICATION_GROUP); run.classpath(javaConvention.getSourceSets() .findByName(SourceSet.MAIN_SOURCE_SET_NAME).getRuntimeClasspath()); run.getConventionMapping().map("jvmArgs", () -> { if (project.hasProperty("applicationDefaultJvmArgs")) { return project.property("applicationDefaultJvmArgs"); } return Collections.emptyList(); }); run.conventionMapping("main", new MainClassConvention(project, run::getClasspath)); } |
What is BootRun?
bootrun
is customized gradle run
for Spring Boot, which becomes available by apply plugin: 'application'
. BootRun
is derived from JavaExec
, and not so different from JavaExec
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
/** * Custom {@link JavaExec} task for running a Spring Boot application. * * @author Andy Wilkinson * @since 2.0.0 */ public class BootRun extends JavaExec { /** * Adds the {@link SourceDirectorySet#getSrcDirs() source directories} of the given * {@code sourceSet's} {@link SourceSet#getResources() resources} to the start of the * classpath in place of the {@link SourceSet#getOutput output's} * {@link SourceSetOutput#getResourcesDir() resources directory}. * @param sourceSet the source set */ public void sourceResources(SourceSet sourceSet) { setClasspath(getProject() .files(sourceSet.getResources().getSrcDirs(), getClasspath()) .filter((file) -> !file.equals(sourceSet.getOutput().getResourcesDir()))); } @Override public void exec() { if (System.console() != null) { // Record that the console is available here for AnsiOutput to detect later this.getEnvironment().put("spring.output.ansi.console-available", true); } super.exec(); } } |