parent
fd7bb53180
commit
fcf45d5c22
@ -0,0 +1,58 @@
|
|||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
* 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 org.graalvm.buildtools.gradle.NativeImagePlugin;
|
||||||
|
import org.graalvm.buildtools.gradle.dsl.GraalVMExtension;
|
||||||
|
import org.graalvm.buildtools.gradle.tasks.BuildNativeImageTask;
|
||||||
|
import org.gradle.api.Action;
|
||||||
|
import org.gradle.api.Plugin;
|
||||||
|
import org.gradle.api.Project;
|
||||||
|
import org.gradle.api.plugins.JavaPlugin;
|
||||||
|
import org.gradle.api.plugins.JavaPluginExtension;
|
||||||
|
import org.gradle.api.tasks.SourceSet;
|
||||||
|
import org.gradle.api.tasks.SourceSetContainer;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@link Action} that is executed in response to the {@link NativeImagePlugin} being
|
||||||
|
* applied.
|
||||||
|
*
|
||||||
|
* @author Andy Wilkinson
|
||||||
|
*/
|
||||||
|
class NativeImagePluginAction implements PluginApplicationAction {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Class<? extends Plugin<? extends Project>> getPluginClass()
|
||||||
|
throws ClassNotFoundException, NoClassDefFoundError {
|
||||||
|
return NativeImagePlugin.class;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void execute(Project project) {
|
||||||
|
project.getPlugins().apply(SpringBootAotPlugin.class);
|
||||||
|
project.getPlugins().withType(JavaPlugin.class).all((plugin) -> {
|
||||||
|
JavaPluginExtension javaPluginExtension = project.getExtensions().getByType(JavaPluginExtension.class);
|
||||||
|
SourceSetContainer sourceSets = javaPluginExtension.getSourceSets();
|
||||||
|
SourceSet aotSourceSet = sourceSets.getByName(SpringBootAotPlugin.AOT_SOURCE_SET_NAME);
|
||||||
|
project.getTasks().named(NativeImagePlugin.NATIVE_COMPILE_TASK_NAME, BuildNativeImageTask.class,
|
||||||
|
(nativeCompile) -> nativeCompile.getOptions().get().classpath(aotSourceSet.getOutput()));
|
||||||
|
});
|
||||||
|
GraalVMExtension graalVmExtension = project.getExtensions().getByType(GraalVMExtension.class);
|
||||||
|
graalVmExtension.getToolchainDetection().set(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,98 @@
|
|||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
* 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.util.List;
|
||||||
|
|
||||||
|
import org.gradle.api.Plugin;
|
||||||
|
import org.gradle.api.Project;
|
||||||
|
import org.gradle.api.artifacts.Configuration;
|
||||||
|
import org.gradle.api.artifacts.ConfigurationContainer;
|
||||||
|
import org.gradle.api.plugins.JavaPlugin;
|
||||||
|
import org.gradle.api.plugins.JavaPluginExtension;
|
||||||
|
import org.gradle.api.plugins.PluginContainer;
|
||||||
|
import org.gradle.api.tasks.SourceSet;
|
||||||
|
import org.gradle.api.tasks.SourceSetContainer;
|
||||||
|
import org.gradle.api.tasks.TaskProvider;
|
||||||
|
|
||||||
|
import org.springframework.boot.gradle.tasks.aot.GenerateAotSources;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gradle plugin for Spring Boot AOT.
|
||||||
|
*
|
||||||
|
* @author Andy Wilkinson
|
||||||
|
* @since 3.0.0
|
||||||
|
*/
|
||||||
|
public class SpringBootAotPlugin implements Plugin<Project> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Name of the {@code aot} {@link SourceSet source set}.
|
||||||
|
*/
|
||||||
|
public static final String AOT_SOURCE_SET_NAME = "aot";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Name of the default {@link GenerateAotSources} task.
|
||||||
|
*/
|
||||||
|
public static final String GENERATE_AOT_SOURCES_TASK_NAME = "generateAotSources";
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void apply(Project project) {
|
||||||
|
PluginContainer plugins = project.getPlugins();
|
||||||
|
plugins.withType(JavaPlugin.class).all((javaPlugin) -> {
|
||||||
|
plugins.withType(SpringBootPlugin.class).all((bootPlugin) -> {
|
||||||
|
SourceSet aotSourceSet = configureAotSourceSet(project);
|
||||||
|
registerGenerateAotSourcesTask(project, aotSourceSet);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private SourceSet configureAotSourceSet(Project project) {
|
||||||
|
JavaPluginExtension javaPluginExtension = project.getExtensions().getByType(JavaPluginExtension.class);
|
||||||
|
SourceSetContainer sourceSets = javaPluginExtension.getSourceSets();
|
||||||
|
SourceSet main = sourceSets.getByName(SourceSet.MAIN_SOURCE_SET_NAME);
|
||||||
|
SourceSet aotSourceSet = sourceSets.create(AOT_SOURCE_SET_NAME, (aot) -> {
|
||||||
|
aot.getJava().setSrcDirs(List.of("build/generated/aotSources"));
|
||||||
|
aot.getResources().setSrcDirs(List.of("build/generated/aotResources"));
|
||||||
|
aot.setCompileClasspath(aot.getCompileClasspath().plus(main.getOutput()));
|
||||||
|
main.setRuntimeClasspath(main.getRuntimeClasspath().plus(aot.getOutput()));
|
||||||
|
ConfigurationContainer configurations = project.getConfigurations();
|
||||||
|
Configuration aotImplementation = configurations.getByName(aot.getImplementationConfigurationName());
|
||||||
|
aotImplementation.extendsFrom(configurations.getByName(main.getImplementationConfigurationName()));
|
||||||
|
aotImplementation.extendsFrom(configurations.getByName(main.getRuntimeOnlyConfigurationName()));
|
||||||
|
});
|
||||||
|
return aotSourceSet;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void registerGenerateAotSourcesTask(Project project, SourceSet aotSourceSet) {
|
||||||
|
TaskProvider<ResolveMainClassName> resolveMainClassName = project.getTasks()
|
||||||
|
.named(SpringBootPlugin.RESOLVE_MAIN_CLASS_NAME_TASK_NAME, ResolveMainClassName.class);
|
||||||
|
TaskProvider<GenerateAotSources> generateAotSources = project.getTasks()
|
||||||
|
.register(GENERATE_AOT_SOURCES_TASK_NAME, GenerateAotSources.class, (task) -> {
|
||||||
|
task.getApplicationClass().set(resolveMainClassName.flatMap((thing) -> thing.readMainClassName()));
|
||||||
|
task.setClasspath(aotSourceSet.getCompileClasspath());
|
||||||
|
task.getSourcesDir().set(aotSourceSet.getJava().getSrcDirs().iterator().next());
|
||||||
|
task.getResourcesDir().set(aotSourceSet.getResources().getSrcDirs().iterator().next());
|
||||||
|
task.getGroupId().set(project.provider(() -> String.valueOf(project.getGroup())));
|
||||||
|
task.getArtifactId().set(project.provider(() -> project.getName()));
|
||||||
|
});
|
||||||
|
project.getTasks().getByName(aotSourceSet.getCompileJavaTaskName(),
|
||||||
|
(compile) -> compile.dependsOn(generateAotSources));
|
||||||
|
project.getTasks().getByName(aotSourceSet.getProcessResourcesTaskName(),
|
||||||
|
(processResources) -> processResources.dependsOn(generateAotSources));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,95 @@
|
|||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
* 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.tasks.aot;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.gradle.api.file.DirectoryProperty;
|
||||||
|
import org.gradle.api.provider.Property;
|
||||||
|
import org.gradle.api.tasks.CacheableTask;
|
||||||
|
import org.gradle.api.tasks.Input;
|
||||||
|
import org.gradle.api.tasks.JavaExec;
|
||||||
|
import org.gradle.api.tasks.OutputDirectory;
|
||||||
|
import org.gradle.api.tasks.TaskAction;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Custom {@link JavaExec} task for generating sources ahead of time.
|
||||||
|
*
|
||||||
|
* @author Andy Wilkinson
|
||||||
|
* @since 3.0
|
||||||
|
*/
|
||||||
|
@CacheableTask
|
||||||
|
public class GenerateAotSources extends JavaExec {
|
||||||
|
|
||||||
|
private final Property<String> applicationClass;
|
||||||
|
|
||||||
|
private final DirectoryProperty sourcesDir;
|
||||||
|
|
||||||
|
private final DirectoryProperty resourcesDir;
|
||||||
|
|
||||||
|
private final Property<String> groupId;
|
||||||
|
|
||||||
|
private final Property<String> artifactId;
|
||||||
|
|
||||||
|
public GenerateAotSources() {
|
||||||
|
this.applicationClass = getProject().getObjects().property(String.class);
|
||||||
|
this.sourcesDir = getProject().getObjects().directoryProperty();
|
||||||
|
this.resourcesDir = getProject().getObjects().directoryProperty();
|
||||||
|
this.groupId = getProject().getObjects().property(String.class);
|
||||||
|
this.artifactId = getProject().getObjects().property(String.class);
|
||||||
|
getMainClass().set("org.springframework.boot.AotProcessor");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Input
|
||||||
|
public Property<String> getApplicationClass() {
|
||||||
|
return this.applicationClass;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Input
|
||||||
|
public Property<String> getGroupId() {
|
||||||
|
return this.groupId;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Input
|
||||||
|
public Property<String> getArtifactId() {
|
||||||
|
return this.artifactId;
|
||||||
|
}
|
||||||
|
|
||||||
|
@OutputDirectory
|
||||||
|
public DirectoryProperty getSourcesDir() {
|
||||||
|
return this.sourcesDir;
|
||||||
|
}
|
||||||
|
|
||||||
|
@OutputDirectory
|
||||||
|
public DirectoryProperty getResourcesDir() {
|
||||||
|
return this.resourcesDir;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@TaskAction
|
||||||
|
public void exec() {
|
||||||
|
List<String> args = new ArrayList<>();
|
||||||
|
args.add(this.applicationClass.get());
|
||||||
|
args.add(this.sourcesDir.getAsFile().get().getAbsolutePath());
|
||||||
|
args.add(this.resourcesDir.getAsFile().get().getAbsolutePath());
|
||||||
|
args.addAll(super.getArgs());
|
||||||
|
this.setArgs(args);
|
||||||
|
super.exec();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,42 @@
|
|||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
* 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 org.junit.jupiter.api.TestTemplate;
|
||||||
|
|
||||||
|
import org.springframework.boot.gradle.junit.GradleCompatibility;
|
||||||
|
import org.springframework.boot.testsupport.gradle.testkit.GradleBuild;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Integration tests for {@link NativeImagePluginAction}.
|
||||||
|
*
|
||||||
|
* @author Andy Wilkinson
|
||||||
|
*/
|
||||||
|
@GradleCompatibility(configurationCache = true)
|
||||||
|
class NativeImagePluginActionIntegrationTests {
|
||||||
|
|
||||||
|
GradleBuild gradleBuild;
|
||||||
|
|
||||||
|
@TestTemplate
|
||||||
|
void applyingNativeImagePluginAppliesAotPlugin() {
|
||||||
|
assertThat(this.gradleBuild.build("aotPluginApplied").getOutput())
|
||||||
|
.contains("org.springframework.boot.aot applied = true");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,48 @@
|
|||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
* 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 org.junit.jupiter.api.TestTemplate;
|
||||||
|
|
||||||
|
import org.springframework.boot.gradle.junit.GradleCompatibility;
|
||||||
|
import org.springframework.boot.testsupport.gradle.testkit.GradleBuild;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Integration tests for {@link SpringBootAotPlugin}.
|
||||||
|
*
|
||||||
|
* @author Andy Wilkinson
|
||||||
|
*/
|
||||||
|
@GradleCompatibility
|
||||||
|
class SpringBootAotPluginIntegrationTests {
|
||||||
|
|
||||||
|
GradleBuild gradleBuild;
|
||||||
|
|
||||||
|
@TestTemplate
|
||||||
|
void noGenerateAotSourcesTaskWithoutAotPluginApplied() {
|
||||||
|
assertThat(this.gradleBuild.build("taskExists", "-PtaskName=generateAotSources").getOutput())
|
||||||
|
.contains("generateAotSources exists = false");
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestTemplate
|
||||||
|
void applyingAotPluginCreatesGenerateAotSourcesTask() {
|
||||||
|
assertThat(this.gradleBuild.build("taskExists", "-PtaskName=generateAotSources").getOutput())
|
||||||
|
.contains("generateAotSources exists = true");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,11 @@
|
|||||||
|
plugins {
|
||||||
|
id 'org.springframework.boot' version '{version}'
|
||||||
|
}
|
||||||
|
|
||||||
|
apply plugin: 'org.graalvm.buildtools.native'
|
||||||
|
|
||||||
|
task('aotPluginApplied') {
|
||||||
|
doFirst {
|
||||||
|
println "org.springframework.boot.aot applied = ${plugins.hasPlugin('org.springframework.boot.aot')}"
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,11 @@
|
|||||||
|
plugins {
|
||||||
|
id 'org.springframework.boot'
|
||||||
|
id 'org.springframework.boot.aot'
|
||||||
|
id 'java'
|
||||||
|
}
|
||||||
|
|
||||||
|
task('taskExists') {
|
||||||
|
doFirst {
|
||||||
|
println "${taskName} exists = ${tasks.findByName(taskName) != null}"
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,10 @@
|
|||||||
|
plugins {
|
||||||
|
id 'org.springframework.boot'
|
||||||
|
id 'java'
|
||||||
|
}
|
||||||
|
|
||||||
|
task('taskExists') {
|
||||||
|
doFirst {
|
||||||
|
println "${taskName} exists = ${tasks.findByName(taskName) != null}"
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue