|
1 | 1 | /*
|
2 |
| - * Copyright 2012-2021 the original author or authors. |
| 2 | + * Copyright 2012-2022 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
17 | 17 | package org.springframework.boot.gradle.dsl;
|
18 | 18 |
|
19 | 19 | import java.io.File;
|
| 20 | +import java.util.List; |
20 | 21 |
|
21 | 22 | import org.gradle.api.Action;
|
22 | 23 | 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; |
23 | 27 | import org.gradle.api.plugins.BasePlugin;
|
24 | 28 | import org.gradle.api.plugins.JavaPlugin;
|
25 | 29 | import org.gradle.api.plugins.JavaPluginExtension;
|
26 | 30 | import org.gradle.api.provider.Property;
|
27 | 31 | import org.gradle.api.tasks.SourceSet;
|
| 32 | +import org.gradle.api.tasks.SourceSetContainer; |
28 | 33 | import org.gradle.api.tasks.TaskContainer;
|
29 | 34 | import org.gradle.api.tasks.TaskProvider;
|
30 | 35 | import org.gradle.jvm.tasks.Jar;
|
31 | 36 |
|
| 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; |
32 | 40 | import org.springframework.boot.gradle.tasks.buildinfo.BuildInfo;
|
33 | 41 | import org.springframework.boot.gradle.tasks.buildinfo.BuildInfoProperties;
|
34 | 42 |
|
@@ -129,4 +137,37 @@ private Jar findArtifactTask() {
|
129 | 137 | return (Jar) this.project.getTasks().findByName("bootJar");
|
130 | 138 | }
|
131 | 139 |
|
| 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 | + |
132 | 173 | }
|
0 commit comments