Skip processAot and processTestAot if there is no main or test code

Closes gh-32424
pull/32205/head
Andy Wilkinson 2 years ago
parent 64f4da80cb
commit fa81e8ca79

@ -71,18 +71,21 @@ public class SpringBootAotPlugin implements Plugin<Project> {
PluginContainer plugins = project.getPlugins();
plugins.withType(JavaPlugin.class).all((javaPlugin) -> {
plugins.withType(SpringBootPlugin.class).all((bootPlugin) -> {
SourceSet aotSourceSet = configureSourceSet(project, "aot", SourceSet.MAIN_SOURCE_SET_NAME);
registerProcessAotTask(project, aotSourceSet);
SourceSet aotTestSourceSet = configureSourceSet(project, "aotTest", SourceSet.TEST_SOURCE_SET_NAME);
registerProcessTestAotTask(project, aotTestSourceSet);
JavaPluginExtension javaPluginExtension = project.getExtensions().getByType(JavaPluginExtension.class);
SourceSetContainer sourceSets = javaPluginExtension.getSourceSets();
SourceSet mainSourceSet = sourceSets.getByName(SourceSet.MAIN_SOURCE_SET_NAME);
SourceSet aotSourceSet = configureSourceSet(project, "aot", mainSourceSet);
registerProcessAotTask(project, aotSourceSet, mainSourceSet);
SourceSet testSourceSet = sourceSets.getByName(SourceSet.TEST_SOURCE_SET_NAME);
SourceSet aotTestSourceSet = configureSourceSet(project, "aotTest", testSourceSet);
registerProcessTestAotTask(project, aotTestSourceSet, testSourceSet);
});
});
}
private SourceSet configureSourceSet(Project project, String newSourceSetName, String existingSourceSetName) {
private SourceSet configureSourceSet(Project project, String newSourceSetName, SourceSet existingSourceSet) {
JavaPluginExtension javaPluginExtension = project.getExtensions().getByType(JavaPluginExtension.class);
SourceSetContainer sourceSets = javaPluginExtension.getSourceSets();
SourceSet existingSourceSet = sourceSets.getByName(existingSourceSetName);
return sourceSets.create(newSourceSetName, (sourceSet) -> {
sourceSet.getJava().setSrcDirs(List.of("build/generated/" + newSourceSetName + "Sources"));
sourceSet.getResources().setSrcDirs(List.of("build/generated/" + newSourceSetName + "Resources"));
@ -112,13 +115,13 @@ public class SpringBootAotPlugin implements Plugin<Project> {
attributes.attribute(Usage.USAGE_ATTRIBUTE, javaRuntime);
}
private void registerProcessAotTask(Project project, SourceSet aotSourceSet) {
private void registerProcessAotTask(Project project, SourceSet aotSourceSet, SourceSet mainSourceSet) {
TaskProvider<ResolveMainClassName> resolveMainClassName = project.getTasks()
.named(SpringBootPlugin.RESOLVE_MAIN_CLASS_NAME_TASK_NAME, ResolveMainClassName.class);
Provider<Directory> aotClasses = project.getLayout().getBuildDirectory().dir("generated/aotClasses");
TaskProvider<ProcessAot> processAot = project.getTasks().register(PROCESS_AOT_TASK_NAME, ProcessAot.class,
(task) -> {
configureAotTask(project, aotSourceSet, task, aotClasses);
configureAotTask(project, aotSourceSet, task, aotClasses, mainSourceSet);
task.getApplicationClass()
.set(resolveMainClassName.flatMap(ResolveMainClassName::readMainClassName));
});
@ -128,13 +131,14 @@ public class SpringBootAotPlugin implements Plugin<Project> {
}
private void configureAotTask(Project project, SourceSet sourceSet, AbstractAot task,
Provider<Directory> generatedClasses) {
Provider<Directory> generatedClasses, SourceSet inputSourceSet) {
task.setClasspath(sourceSet.getCompileClasspath());
task.getSourcesDir().set(sourceSet.getJava().getSrcDirs().iterator().next());
task.getResourcesDir().set(sourceSet.getResources().getSrcDirs().iterator().next());
task.getClassesDir().set(generatedClasses);
task.getSourcesOutput().set(sourceSet.getJava().getSrcDirs().iterator().next());
task.getResourcesOutput().set(sourceSet.getResources().getSrcDirs().iterator().next());
task.getClassesOutput().set(generatedClasses);
task.getGroupId().set(project.provider(() -> String.valueOf(project.getGroup())));
task.getArtifactId().set(project.provider(() -> project.getName()));
task.setInputClasses(inputSourceSet.getOutput().getClassesDirs());
}
private void configureDependsOn(Project project, SourceSet aotSourceSet,
@ -145,15 +149,13 @@ public class SpringBootAotPlugin implements Plugin<Project> {
.configure((processResources) -> processResources.dependsOn(processAot));
}
private void registerProcessTestAotTask(Project project, SourceSet aotTestSourceSet) {
JavaPluginExtension javaPluginExtension = project.getExtensions().getByType(JavaPluginExtension.class);
SourceSetContainer sourceSets = javaPluginExtension.getSourceSets();
SourceSet testSourceSet = sourceSets.getByName(SourceSet.TEST_SOURCE_SET_NAME);
private void registerProcessTestAotTask(Project project, SourceSet aotTestSourceSet, SourceSet testSourceSet) {
Provider<Directory> aotTestClasses = project.getLayout().getBuildDirectory().dir("generated/aotTestClasses");
TaskProvider<ProcessTestAot> processTestAot = project.getTasks().register(PROCESS_TEST_AOT_TASK_NAME,
ProcessTestAot.class, (task) -> {
configureAotTask(project, aotTestSourceSet, task, aotTestClasses);
task.setTestSourceSet(testSourceSet);
configureAotTask(project, aotTestSourceSet, task, aotTestClasses, testSourceSet);
task.setTestRuntimeClasspath(
project.getConfigurations().getByName(testSourceSet.getImplementationConfigurationName()));
});
project.getDependencies().add(aotTestSourceSet.getImplementationConfigurationName(),
project.files(aotTestClasses));

@ -20,10 +20,16 @@ import java.util.ArrayList;
import java.util.List;
import org.gradle.api.file.DirectoryProperty;
import org.gradle.api.file.FileCollection;
import org.gradle.api.provider.Property;
import org.gradle.api.tasks.IgnoreEmptyDirectories;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.InputFiles;
import org.gradle.api.tasks.JavaExec;
import org.gradle.api.tasks.OutputDirectory;
import org.gradle.api.tasks.PathSensitive;
import org.gradle.api.tasks.PathSensitivity;
import org.gradle.api.tasks.SkipWhenEmpty;
import org.gradle.work.DisableCachingByDefault;
/**
@ -46,6 +52,8 @@ public abstract class AbstractAot extends JavaExec {
private final Property<String> artifactId;
private FileCollection inputClasses;
protected AbstractAot() {
this.sourcesDir = getProject().getObjects().directoryProperty();
this.resourcesDir = getProject().getObjects().directoryProperty();
@ -65,25 +73,37 @@ public abstract class AbstractAot extends JavaExec {
}
@OutputDirectory
public final DirectoryProperty getSourcesDir() {
public final DirectoryProperty getSourcesOutput() {
return this.sourcesDir;
}
@OutputDirectory
public final DirectoryProperty getResourcesDir() {
public final DirectoryProperty getResourcesOutput() {
return this.resourcesDir;
}
@OutputDirectory
public final DirectoryProperty getClassesDir() {
public final DirectoryProperty getClassesOutput() {
return this.classesDir;
}
@InputFiles
@SkipWhenEmpty
@IgnoreEmptyDirectories
@PathSensitive(PathSensitivity.RELATIVE)
public final FileCollection getInputClasses() {
return this.inputClasses.getAsFileTree().matching((filter) -> filter.include((spec) -> !spec.isDirectory()));
}
public void setInputClasses(FileCollection inputClasses) {
this.inputClasses = inputClasses;
}
List<String> processorArgs() {
List<String> args = new ArrayList<>();
args.add(getSourcesDir().getAsFile().get().getAbsolutePath());
args.add(getResourcesDir().getAsFile().get().getAbsolutePath());
args.add(getClassesDir().getAsFile().get().getAbsolutePath());
args.add(getSourcesOutput().getAsFile().get().getAbsolutePath());
args.add(getResourcesOutput().getAsFile().get().getAbsolutePath());
args.add(getClassesOutput().getAsFile().get().getAbsolutePath());
args.add(getGroupId().get());
args.add(getArtifactId().get());
args.addAll(super.getArgs());

@ -29,7 +29,6 @@ import org.gradle.api.file.FileCollection;
import org.gradle.api.tasks.CacheableTask;
import org.gradle.api.tasks.Classpath;
import org.gradle.api.tasks.JavaExec;
import org.gradle.api.tasks.SourceSet;
import org.gradle.api.tasks.TaskAction;
import org.springframework.boot.gradle.plugin.SpringBootPlugin;
@ -45,8 +44,6 @@ public class ProcessTestAot extends AbstractAot {
private final Configuration junitPlatformLauncher;
private FileCollection testClassesDirs;
public ProcessTestAot() {
getMainClass().set("org.springframework.test.context.aot.TestAotProcessor");
this.junitPlatformLauncher = createJUnitPlatformLauncher();
@ -63,15 +60,6 @@ public class ProcessTestAot extends AbstractAot {
return configuration;
}
@Classpath
public FileCollection getTestClassesDirs() {
return this.testClassesDirs;
}
public void setTestClassesDirs(FileCollection testClassesDirs) {
this.testClassesDirs = testClassesDirs;
}
@Classpath
FileCollection getJUnitPlatformLauncher() {
return this.junitPlatformLauncher;
@ -81,7 +69,7 @@ public class ProcessTestAot extends AbstractAot {
@TaskAction
public void exec() {
List<String> args = new ArrayList<>();
args.add(this.testClassesDirs.getFiles().stream().filter(File::exists).map(File::getAbsolutePath)
args.add(this.getInputClasses().getFiles().stream().filter(File::exists).map(File::getAbsolutePath)
.collect(Collectors.joining(File.pathSeparator)));
args.addAll(processorArgs());
this.setArgs(args);
@ -89,10 +77,8 @@ public class ProcessTestAot extends AbstractAot {
super.exec();
}
public void setTestSourceSet(SourceSet testSourceSet) {
setTestClassesDirs(testSourceSet.getOutput().getClassesDirs());
this.junitPlatformLauncher.extendsFrom(
getProject().getConfigurations().getByName(testSourceSet.getImplementationConfigurationName()));
public void setTestRuntimeClasspath(Configuration configuration) {
this.junitPlatformLauncher.extendsFrom(configuration);
}
}

@ -21,6 +21,7 @@ import java.io.IOException;
import java.nio.file.Files;
import java.util.List;
import org.gradle.testkit.runner.TaskOutcome;
import org.junit.jupiter.api.TestTemplate;
import org.springframework.boot.gradle.junit.GradleCompatibility;
@ -94,4 +95,16 @@ class SpringBootAotPluginIntegrationTests {
assertThat(output).contains("org.jboss.logging" + File.separatorChar + "jboss-logging");
}
@TestTemplate
void processAotIsSkippedWhenProjectHasNoMainSource() {
assertThat(this.gradleBuild.build("processAot").task(":processAot").getOutcome())
.isEqualTo(TaskOutcome.NO_SOURCE);
}
@TestTemplate
void processTestAotIsSkippedWhenProjectHasNoTestSource() {
assertThat(this.gradleBuild.build("processTestAot").task(":processTestAot").getOutcome())
.isEqualTo(TaskOutcome.NO_SOURCE);
}
}

@ -0,0 +1,13 @@
plugins {
id 'org.springframework.boot'
id 'org.springframework.boot.aot'
id 'java'
}
repositories {
mavenCentral()
}
springBoot {
mainClass = 'com.example.Application'
}

@ -0,0 +1,13 @@
plugins {
id 'org.springframework.boot'
id 'org.springframework.boot.aot'
id 'java'
}
repositories {
mavenCentral()
}
springBoot {
mainClass = 'com.example.Application'
}
Loading…
Cancel
Save