|
| 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.graalvm.buildtools.gradle.NativeImagePlugin; |
| 22 | +import org.graalvm.buildtools.gradle.dsl.GraalVMExtension; |
| 23 | +import org.graalvm.buildtools.gradle.tasks.BuildNativeImageTask; |
| 24 | +import org.gradle.api.Action; |
| 25 | +import org.gradle.api.Plugin; |
| 26 | +import org.gradle.api.Project; |
| 27 | +import org.gradle.api.artifacts.Configuration; |
| 28 | +import org.gradle.api.artifacts.ConfigurationContainer; |
| 29 | +import org.gradle.api.plugins.JavaPlugin; |
| 30 | +import org.gradle.api.plugins.JavaPluginExtension; |
| 31 | +import org.gradle.api.tasks.SourceSet; |
| 32 | +import org.gradle.api.tasks.SourceSetContainer; |
| 33 | +import org.gradle.api.tasks.TaskProvider; |
| 34 | + |
| 35 | +import org.springframework.boot.gradle.tasks.aot.GenerateAotSources; |
| 36 | + |
| 37 | +/** |
| 38 | + * {@link Action} that is executed in response to the {@link NativeImagePlugin} being |
| 39 | + * applied. |
| 40 | + * |
| 41 | + * @author Andy Wilkinson |
| 42 | + */ |
| 43 | +class NativeImagePluginAction implements PluginApplicationAction { |
| 44 | + |
| 45 | + @Override |
| 46 | + public Class<? extends Plugin<? extends Project>> getPluginClass() |
| 47 | + throws ClassNotFoundException, NoClassDefFoundError { |
| 48 | + return NativeImagePlugin.class; |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public void execute(Project project) { |
| 53 | + project.getPlugins().withType(JavaPlugin.class).all((plugin) -> { |
| 54 | + SourceSet aotSourceSet = configureAotSourceSet(project); |
| 55 | + registerGenerateAotSourcesTask(project, aotSourceSet); |
| 56 | + project.getTasks().named(NativeImagePlugin.NATIVE_COMPILE_TASK_NAME, BuildNativeImageTask.class, |
| 57 | + (nativeCompile) -> nativeCompile.getOptions().get().classpath(aotSourceSet.getOutput())); |
| 58 | + }); |
| 59 | + GraalVMExtension graalVmExtension = project.getExtensions().getByType(GraalVMExtension.class); |
| 60 | + graalVmExtension.getToolchainDetection().set(false); |
| 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", (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().register("generateAotSources", |
| 84 | + 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 | + }); |
| 90 | + project.getTasks().getByName(aotSourceSet.getCompileJavaTaskName(), |
| 91 | + (compile) -> compile.dependsOn(generateAotSources)); |
| 92 | + project.getTasks().getByName(aotSourceSet.getProcessResourcesTaskName(), |
| 93 | + (processResources) -> processResources.dependsOn(generateAotSources)); |
| 94 | + } |
| 95 | + |
| 96 | +} |
0 commit comments