|
| 1 | +/* |
| 2 | + * Copyright 2012-2022 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot.gradle.plugin; |
| 18 | + |
| 19 | +import java.util.List; |
| 20 | + |
| 21 | +import org.gradle.api.Plugin; |
| 22 | +import org.gradle.api.Project; |
| 23 | +import org.gradle.api.artifacts.Configuration; |
| 24 | +import org.gradle.api.artifacts.ConfigurationContainer; |
| 25 | +import org.gradle.api.plugins.JavaPlugin; |
| 26 | +import org.gradle.api.plugins.JavaPluginExtension; |
| 27 | +import org.gradle.api.plugins.PluginContainer; |
| 28 | +import org.gradle.api.tasks.SourceSet; |
| 29 | +import org.gradle.api.tasks.SourceSetContainer; |
| 30 | +import org.gradle.api.tasks.TaskProvider; |
| 31 | + |
| 32 | +import org.springframework.boot.gradle.tasks.aot.GenerateAotSources; |
| 33 | + |
| 34 | +/** |
| 35 | + * Gradle plugin for Spring Boot AOT. |
| 36 | + * |
| 37 | + * @author Andy Wilkinson |
| 38 | + * @since 3.0.0 |
| 39 | + */ |
| 40 | +public class SpringBootAotPlugin implements Plugin<Project> { |
| 41 | + |
| 42 | + /** |
| 43 | + * Name of the {@code aot} {@link SourceSet source set}. |
| 44 | + */ |
| 45 | + public static final String AOT_SOURCE_SET_NAME = "aot"; |
| 46 | + |
| 47 | + /** |
| 48 | + * Name of the default {@link GenerateAotSources} task. |
| 49 | + */ |
| 50 | + public static final String GENERATE_AOT_SOURCES_TASK_NAME = "generateAotSources"; |
| 51 | + |
| 52 | + @Override |
| 53 | + public void apply(Project project) { |
| 54 | + PluginContainer plugins = project.getPlugins(); |
| 55 | + plugins.withType(JavaPlugin.class).all((javaPlugin) -> { |
| 56 | + plugins.withType(SpringBootPlugin.class).all((bootPlugin) -> { |
| 57 | + SourceSet aotSourceSet = configureAotSourceSet(project); |
| 58 | + registerGenerateAotSourcesTask(project, aotSourceSet); |
| 59 | + }); |
| 60 | + }); |
| 61 | + } |
| 62 | + |
| 63 | + private SourceSet configureAotSourceSet(Project project) { |
| 64 | + JavaPluginExtension javaPluginExtension = project.getExtensions().getByType(JavaPluginExtension.class); |
| 65 | + SourceSetContainer sourceSets = javaPluginExtension.getSourceSets(); |
| 66 | + SourceSet main = sourceSets.getByName(SourceSet.MAIN_SOURCE_SET_NAME); |
| 67 | + SourceSet aotSourceSet = sourceSets.create(AOT_SOURCE_SET_NAME, (aot) -> { |
| 68 | + aot.getJava().setSrcDirs(List.of("build/generated/aotSources")); |
| 69 | + aot.getResources().setSrcDirs(List.of("build/generated/aotResources")); |
| 70 | + aot.setCompileClasspath(aot.getCompileClasspath().plus(main.getOutput())); |
| 71 | + main.setRuntimeClasspath(main.getRuntimeClasspath().plus(aot.getOutput())); |
| 72 | + ConfigurationContainer configurations = project.getConfigurations(); |
| 73 | + Configuration aotImplementation = configurations.getByName(aot.getImplementationConfigurationName()); |
| 74 | + aotImplementation.extendsFrom(configurations.getByName(main.getImplementationConfigurationName())); |
| 75 | + aotImplementation.extendsFrom(configurations.getByName(main.getRuntimeOnlyConfigurationName())); |
| 76 | + }); |
| 77 | + return aotSourceSet; |
| 78 | + } |
| 79 | + |
| 80 | + private void registerGenerateAotSourcesTask(Project project, SourceSet aotSourceSet) { |
| 81 | + TaskProvider<ResolveMainClassName> resolveMainClassName = project.getTasks() |
| 82 | + .named(SpringBootPlugin.RESOLVE_MAIN_CLASS_NAME_TASK_NAME, ResolveMainClassName.class); |
| 83 | + TaskProvider<GenerateAotSources> generateAotSources = project.getTasks() |
| 84 | + .register(GENERATE_AOT_SOURCES_TASK_NAME, GenerateAotSources.class, (task) -> { |
| 85 | + task.getApplicationClass().set(resolveMainClassName.flatMap((thing) -> thing.readMainClassName())); |
| 86 | + task.setClasspath(aotSourceSet.getCompileClasspath()); |
| 87 | + task.getSourcesDir().set(aotSourceSet.getJava().getSrcDirs().iterator().next()); |
| 88 | + task.getResourcesDir().set(aotSourceSet.getResources().getSrcDirs().iterator().next()); |
| 89 | + task.getGroupId().set(project.provider(() -> String.valueOf(project.getGroup()))); |
| 90 | + task.getArtifactId().set(project.provider(() -> project.getName())); |
| 91 | + }); |
| 92 | + project.getTasks().getByName(aotSourceSet.getCompileJavaTaskName(), |
| 93 | + (compile) -> compile.dependsOn(generateAotSources)); |
| 94 | + project.getTasks().getByName(aotSourceSet.getProcessResourcesTaskName(), |
| 95 | + (processResources) -> processResources.dependsOn(generateAotSources)); |
| 96 | + } |
| 97 | + |
| 98 | +} |
0 commit comments