Skip to content

Commit 9bb5f90

Browse files
wilkinsonasnicoll
authored andcommitted
Add support for invoking AOT in the Gradle plugin
1 parent 81b2d71 commit 9bb5f90

File tree

3 files changed

+120
-2
lines changed

3 files changed

+120
-2
lines changed

spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/dsl/SpringBootExtension.java

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors.
2+
* Copyright 2012-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,18 +17,26 @@
1717
package org.springframework.boot.gradle.dsl;
1818

1919
import java.io.File;
20+
import java.util.List;
2021

2122
import org.gradle.api.Action;
2223
import org.gradle.api.Project;
24+
import org.gradle.api.artifacts.Configuration;
25+
import org.gradle.api.artifacts.ConfigurationContainer;
26+
import org.gradle.api.file.FileCollection;
2327
import org.gradle.api.plugins.BasePlugin;
2428
import org.gradle.api.plugins.JavaPlugin;
2529
import org.gradle.api.plugins.JavaPluginExtension;
2630
import org.gradle.api.provider.Property;
2731
import org.gradle.api.tasks.SourceSet;
32+
import org.gradle.api.tasks.SourceSetContainer;
2833
import org.gradle.api.tasks.TaskContainer;
2934
import org.gradle.api.tasks.TaskProvider;
3035
import org.gradle.jvm.tasks.Jar;
3136

37+
import org.springframework.boot.gradle.plugin.ResolveMainClassName;
38+
import org.springframework.boot.gradle.plugin.SpringBootPlugin;
39+
import org.springframework.boot.gradle.tasks.aot.GenerateAotSources;
3240
import org.springframework.boot.gradle.tasks.buildinfo.BuildInfo;
3341
import org.springframework.boot.gradle.tasks.buildinfo.BuildInfoProperties;
3442

@@ -129,4 +137,37 @@ private Jar findArtifactTask() {
129137
return (Jar) this.project.getTasks().findByName("bootJar");
130138
}
131139

140+
public void aot() {
141+
this.project.getPlugins().withType(JavaPlugin.class).all((plugin) -> {
142+
JavaPluginExtension javaPluginExtension = this.project.getExtensions().getByType(JavaPluginExtension.class);
143+
SourceSetContainer sourceSets = javaPluginExtension.getSourceSets();
144+
SourceSet main = sourceSets.getByName(SourceSet.MAIN_SOURCE_SET_NAME);
145+
FileCollection runtimeClasspath = main.getRuntimeClasspath();
146+
TaskProvider<ResolveMainClassName> resolveMainClassName = this.project.getTasks()
147+
.named(SpringBootPlugin.RESOLVE_MAIN_CLASS_NAME_TASK_NAME, ResolveMainClassName.class);
148+
SourceSet aotSourceSet = sourceSets.create("aot", (aot) -> {
149+
aot.getJava().setSrcDirs(List.of("build/generated/aotSources"));
150+
aot.getResources().setSrcDirs(List.of("build/generated/aotResources"));
151+
aot.setCompileClasspath(aot.getCompileClasspath().plus(main.getOutput()));
152+
main.setRuntimeClasspath(runtimeClasspath.plus(aot.getOutput()));
153+
ConfigurationContainer configurations = this.project.getConfigurations();
154+
Configuration aotImplementation = configurations.getByName(aot.getImplementationConfigurationName());
155+
aotImplementation.extendsFrom(configurations.getByName(main.getImplementationConfigurationName()));
156+
aotImplementation.extendsFrom(configurations.getByName(main.getRuntimeOnlyConfigurationName()));
157+
});
158+
TaskProvider<GenerateAotSources> generateAotSources = this.project.getTasks().register("generateAotSources",
159+
GenerateAotSources.class, (task) -> {
160+
task.getApplicationClass()
161+
.set(resolveMainClassName.flatMap((thing) -> thing.readMainClassName()));
162+
task.setClasspath(aotSourceSet.getCompileClasspath());
163+
task.getSourcesDir().set(aotSourceSet.getJava().getSrcDirs().iterator().next());
164+
task.getResourcesDir().set(aotSourceSet.getResources().getSrcDirs().iterator().next());
165+
});
166+
this.project.getTasks().getByName(aotSourceSet.getCompileJavaTaskName(),
167+
(compile) -> compile.dependsOn(generateAotSources));
168+
this.project.getTasks().getByName(aotSourceSet.getProcessResourcesTaskName(),
169+
(processResources) -> processResources.dependsOn(generateAotSources));
170+
});
171+
}
172+
132173
}

spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/plugin/ResolveMainClassName.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ private String findMainClass(File file) {
144144
}
145145
}
146146

147-
Provider<String> readMainClassName() {
147+
public Provider<String> readMainClassName() {
148148
return this.outputFile.map(new ClassNameReader());
149149
}
150150

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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.tasks.aot;
18+
19+
import java.util.ArrayList;
20+
import java.util.List;
21+
22+
import org.gradle.api.file.DirectoryProperty;
23+
import org.gradle.api.provider.Property;
24+
import org.gradle.api.tasks.Input;
25+
import org.gradle.api.tasks.JavaExec;
26+
import org.gradle.api.tasks.OutputDirectory;
27+
import org.gradle.api.tasks.TaskAction;
28+
29+
/**
30+
* Custom {@link JavaExec} task for generating sources ahead of time.
31+
*
32+
* @author Andy Wilkinson
33+
* @since 3.0
34+
*/
35+
public class GenerateAotSources extends JavaExec {
36+
37+
private final Property<String> applicationClass;
38+
39+
private final DirectoryProperty sourcesDir;
40+
41+
private final DirectoryProperty resourcesDir;
42+
43+
public GenerateAotSources() {
44+
this.applicationClass = getProject().getObjects().property(String.class);
45+
this.sourcesDir = getProject().getObjects().directoryProperty();
46+
this.resourcesDir = getProject().getObjects().directoryProperty();
47+
getMainClass().set("org.springframework.boot.AotProcessor");
48+
}
49+
50+
@Input
51+
public Property<String> getApplicationClass() {
52+
return this.applicationClass;
53+
}
54+
55+
@OutputDirectory
56+
public DirectoryProperty getSourcesDir() {
57+
return this.sourcesDir;
58+
}
59+
60+
@OutputDirectory
61+
public DirectoryProperty getResourcesDir() {
62+
return this.resourcesDir;
63+
}
64+
65+
@Override
66+
@TaskAction
67+
public void exec() {
68+
List<String> args = new ArrayList<>();
69+
args.add(this.applicationClass.get());
70+
args.add(this.sourcesDir.getAsFile().get().getAbsolutePath());
71+
args.add(this.resourcesDir.getAsFile().get().getAbsolutePath());
72+
args.addAll(super.getArgs());
73+
this.setArgs(args);
74+
super.exec();
75+
}
76+
77+
}

0 commit comments

Comments
 (0)