Skip to content
This repository has been archived by the owner on Feb 23, 2023. It is now read-only.

Commit

Permalink
Disable devtools in AOT mode
Browse files Browse the repository at this point in the history
Prior to this commit, Spring Native would fail if Spring Boot Devtools
is detected on the classpath, mentioning an incompatibility with this
library.

Because Devtools is about recompiling user classes+restarting the
application context during development, this approach is currently not
compatible with AOT since AOT sources are not re-generated during this
lifecycle.

This commit now ignores completely Devtools during AOT generation and
runtime in AOT mode.

Closes gh-1419
  • Loading branch information
bclozel committed Jan 11, 2022
1 parent 319e101 commit e278b9e
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
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.artifacts.Dependency;
import org.gradle.api.file.DuplicatesStrategy;
import org.gradle.api.file.FileCollection;
Expand Down Expand Up @@ -112,7 +113,7 @@ public void apply(final Project project) {
SourceSetContainer sourceSets = project.getConvention().getPlugin(JavaPluginConvention.class).getSourceSets();

// Create a detached configuration that holds dependencies for AOT generation
SourceSet aotMainSourceSet = createAotMainSourceSet(sourceSets, generatedFilesPath);
SourceSet aotMainSourceSet = createAotMainSourceSet(sourceSets, project.getConfigurations(), generatedFilesPath);
GenerateAotSources generateAotSources = createGenerateAotSourcesTask(project, sourceSets);
project.getTasks().named(aotMainSourceSet.getCompileJavaTaskName(), JavaCompile.class, (aotCompileJava) -> {
aotCompileJava.source(generateAotSources.getSourcesOutputDirectory());
Expand All @@ -138,7 +139,7 @@ public void apply(final Project project) {
});

// Create a detached configuration that holds dependencies for AOT test generation
SourceSet aotTestSourceSet = createAotTestSourceSet(sourceSets, generatedFilesPath);
SourceSet aotTestSourceSet = createAotTestSourceSet(sourceSets, project.getConfigurations(), generatedFilesPath);
GenerateAotTestSources generateAotTestSources = createGenerateAotTestSourcesTask(project, sourceSets);
project.getTasks().named(aotTestSourceSet.getCompileJavaTaskName(), JavaCompile.class, (aotTestCompileJava) -> {
aotTestCompileJava.source(generateAotTestSources.getGeneratedSourcesOutputDirectory());
Expand Down Expand Up @@ -171,8 +172,8 @@ public void apply(final Project project) {
nativeCompile.dependsOn(generatedSourcesJar);
});
graal.getBinaries().named(NativeImagePlugin.NATIVE_MAIN_EXTENSION).configure(options -> {
Provider<RegularFile> generatedSources = generatedSourcesJar.getArchiveFile();
options.classpath(generatedSources);
Provider<RegularFile> generatedSources = generatedSourcesJar.getArchiveFile();
options.classpath(generatedSources);
});
graal.getBinaries().named(NativeImagePlugin.NATIVE_TEST_EXTENSION).configure(options -> {
Provider<RegularFile> generatedTestSources = generatedTestSourcesJar.getArchiveFile();
Expand Down Expand Up @@ -218,11 +219,13 @@ private Path createGeneratedSourcesFolder(Project project) {
}
}

private SourceSet createAotMainSourceSet(SourceSetContainer sourceSets, Path generatedFilesPath) {
private SourceSet createAotMainSourceSet(SourceSetContainer sourceSets, ConfigurationContainer configurations, Path generatedFilesPath) {
File aotSourcesDirectory = generatedFilesPath.resolve("sources").resolve(AOT_MAIN_SOURCE_SET_NAME).toFile();
File aotResourcesDirectory = generatedFilesPath.resolve("resources").resolve(AOT_MAIN_SOURCE_SET_NAME).toFile();
SourceSet aotMainSourceSet = sourceSets.create(AOT_MAIN_SOURCE_SET_NAME);
aotMainSourceSet.setCompileClasspath(sourceSets.findByName(SourceSet.MAIN_SOURCE_SET_NAME).getRuntimeClasspath());
FileCollection aotCompileClasspath = sourceSets.findByName(SourceSet.MAIN_SOURCE_SET_NAME).getRuntimeClasspath()
.minus(configurations.getByName(SpringBootPlugin.DEVELOPMENT_ONLY_CONFIGURATION_NAME));
aotMainSourceSet.setCompileClasspath(aotCompileClasspath);
aotMainSourceSet.getJava().setSrcDirs(Collections.singletonList(aotSourcesDirectory));
aotMainSourceSet.getResources().setSrcDirs(Collections.singletonList(aotResourcesDirectory));
return aotMainSourceSet;
Expand Down Expand Up @@ -268,11 +271,13 @@ private Configuration createAotGenerationConfiguration(Project project) {
return detachedConfiguration;
}

private SourceSet createAotTestSourceSet(SourceSetContainer sourceSets, Path generatedFilesPath) {
private SourceSet createAotTestSourceSet(SourceSetContainer sourceSets, ConfigurationContainer configurations, Path generatedFilesPath) {
File aotTestSourcesDirectory = generatedFilesPath.resolve("sources").resolve(AOT_TEST_SOURCE_SET_NAME).toFile();
File aotTestResourcesDirectory = generatedFilesPath.resolve("resources").resolve(AOT_TEST_SOURCE_SET_NAME).toFile();
SourceSet aotTestSourceSet = sourceSets.create(AOT_TEST_SOURCE_SET_NAME);
aotTestSourceSet.setCompileClasspath(sourceSets.findByName(SourceSet.TEST_SOURCE_SET_NAME).getRuntimeClasspath());
FileCollection aotTestCompileClasspath = sourceSets.findByName(SourceSet.TEST_SOURCE_SET_NAME).getRuntimeClasspath()
.minus(configurations.getByName(SpringBootPlugin.DEVELOPMENT_ONLY_CONFIGURATION_NAME));
aotTestSourceSet.setCompileClasspath(aotTestCompileClasspath);
aotTestSourceSet.getJava().setSrcDirs(Collections.singletonList(aotTestSourcesDirectory));
aotTestSourceSet.getResources().setSrcDirs(Collections.singletonList(aotTestResourcesDirectory));
return aotTestSourceSet;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,19 @@ void pluginRegistersAotSourceSet() {
assertThat(aotSourceSet.getCompileClasspath()).containsAll(mainSourceSet.getRuntimeClasspath());
}

@Test
void devtoolsIsIgnoredDuringAotGeneration() {
Project project = createTestProject();
project.getDependencies().add(SpringBootPlugin.DEVELOPMENT_ONLY_CONFIGURATION_NAME, "org.springframework.boot:spring-boot-devtools");
JavaPluginExtension java = project.getExtensions().findByType(JavaPluginExtension.class);
SourceSet aotSourceSet = java.getSourceSets().findByName(SpringAotGradlePlugin.AOT_MAIN_SOURCE_SET_NAME);
assertThat(aotSourceSet).isNotNull();
assertThat(aotSourceSet.getCompileClasspath().getFiles()).noneMatch(file -> file.getName().contains("spring-boot-devtools"));
SourceSet aotTestSourceSet = java.getSourceSets().findByName(SpringAotGradlePlugin.AOT_TEST_SOURCE_SET_NAME);
assertThat(aotTestSourceSet).isNotNull();
assertThat(aotTestSourceSet.getCompileClasspath().getFiles()).noneMatch(file -> file.getName().contains("spring-boot-devtools"));
}

@Test
void pluginRegistersAotTestSourceSet() {
Project project = createTestProject();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import org.apache.maven.model.Resource;
import org.apache.maven.plugin.MojoExecutionException;
Expand Down Expand Up @@ -88,7 +89,10 @@ public void execute() throws MojoExecutionException, MojoFailureException {
Path sourcesPath = this.generatedSourcesDirectory.toPath().resolve(Paths.get("src", "main", "java"));
Path resourcesPath = this.generatedSourcesDirectory.toPath().resolve(Paths.get("src", "main", "resources"));
try {
List<String> runtimeClasspathElements = project.getRuntimeClasspathElements();
List<String> runtimeClasspathElements = project.getRuntimeClasspathElements()
.stream()
.filter(element -> !element.contains("spring-boot-devtools"))
.collect(Collectors.toList());

findJarFile(this.pluginArtifacts, "org.springframework.experimental", "spring-native-configuration")
.ifPresent(artifact -> prependDependency(artifact, runtimeClasspathElements));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import org.apache.maven.model.Resource;
import org.apache.maven.plugin.MojoExecutionException;
Expand Down Expand Up @@ -79,7 +80,10 @@ public void execute() throws MojoExecutionException, MojoFailureException {
return;
}
try {
List<String> testClasspathElements = this.project.getTestClasspathElements();
List<String> testClasspathElements = this.project.getTestClasspathElements()
.stream()
.filter(element -> !element.contains("spring-boot-devtools"))
.collect(Collectors.toList());
Files.createDirectories(this.generatedTestSourcesDirectory.toPath());

Path sourcesPath = this.generatedTestSourcesDirectory.toPath().resolve(Paths.get("src", "test", "java"));
Expand Down
5 changes: 5 additions & 0 deletions spring-aot/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,11 @@
<artifactId>spring-boot-starter-security</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ private boolean passesFilterCheck(BuildContext context, SpringFactory factory) {
} else if (factoryName.equals("org.springframework.boot.env.YamlPropertySourceLoader")) {
return !aotOptions.isRemoveYamlSupport();
} else if (factoryName.startsWith("org.springframework.boot.devtools")) {
throw new IllegalStateException("Devtools is not supported yet, please remove the related dependency for now.");
return false;
}
return true;
}
Expand Down
2 changes: 2 additions & 0 deletions spring-native-docs/src/main/asciidoc/build-setup.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,8 @@ This means that running the application or its tests from the IDE or the command
Any application using Spring AOT can use the `springAot` System property in order to use the AOT classes with a regular JVM.
This is mainly useful for debugging purposes in case of issues during native image generation.

NOTE: When AOT more is enabled, Spring Boot Developer Tools are ignored as they are not compatible with an AOT approach.

You can set such a property when running an executable Jar from the command line:

[source,bash,subs="attributes,verbatim"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public static Properties getNativeProperties() {
props.put("spring.cloud.refresh.enabled", "false"); // Sampler is a class and can't be proxied
props.put("spring.sleuth.async.enabled", "false"); // Too much proxy created
props.put("spring.cloud.compatibility-verifier.enabled", "false"); // To avoid false positive due to SpringApplication patched copy
props.put("spring.devtools.restart.enabled", "false"); // Deactivate dev tools
return props;
}
}

0 comments on commit e278b9e

Please sign in to comment.