Fix Maven-based AOT generation on Windows

Closes gh-30525
pull/31104/head
Andy Wilkinson 3 years ago
parent 3e0994fb53
commit c4beca3e01

@ -30,6 +30,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* Integration tests for the Maven plugin's AOT support. * Integration tests for the Maven plugin's AOT support.
* *
* @author Stephane Nicoll * @author Stephane Nicoll
* @author Andy Wilkinson
*/ */
@ExtendWith(MavenBuildExtension.class) @ExtendWith(MavenBuildExtension.class)
public class AotGenerateTests { public class AotGenerateTests {
@ -38,12 +39,15 @@ public class AotGenerateTests {
void whenAotRunsSourcesAreGenerated(MavenBuild mavenBuild) { void whenAotRunsSourcesAreGenerated(MavenBuild mavenBuild) {
mavenBuild.project("aot").goals("package").execute((project) -> { mavenBuild.project("aot").goals("package").execute((project) -> {
Path aotDirectory = project.toPath().resolve("target/spring-aot/main"); Path aotDirectory = project.toPath().resolve("target/spring-aot/main");
assertThat(collectRelativeFileNames(aotDirectory.resolve("sources"))) assertThat(collectRelativePaths(aotDirectory.resolve("sources")))
.contains("org/test/SampleApplication__ApplicationContextInitializer.java"); .contains(Path.of("org", "test", "SampleApplication__ApplicationContextInitializer.java"));
assertThat(collectRelativeFileNames(aotDirectory.resolve("resources"))).containsOnly( assertThat(collectRelativePaths(aotDirectory.resolve("resources"))).containsOnly(
"META-INF/native-image/org.springframework.boot.maven.it/aot/reflect-config.json", Path.of("META-INF", "native-image", "org.springframework.boot.maven.it", "aot",
"META-INF/native-image/org.springframework.boot.maven.it/aot/resource-config.json", "reflect-config.json"),
"META-INF/native-image/org.springframework.boot.maven.it/aot/native-image.properties"); Path.of("META-INF", "native-image", "org.springframework.boot.maven.it", "aot",
"resource-config.json"),
Path.of("META-INF", "native-image", "org.springframework.boot.maven.it", "aot",
"native-image.properties"));
}); });
} }
@ -51,8 +55,8 @@ public class AotGenerateTests {
void whenAotRunsSourcesAreCompiled(MavenBuild mavenBuild) { void whenAotRunsSourcesAreCompiled(MavenBuild mavenBuild) {
mavenBuild.project("aot").goals("package").execute((project) -> { mavenBuild.project("aot").goals("package").execute((project) -> {
Path classesDirectory = project.toPath().resolve("target/classes"); Path classesDirectory = project.toPath().resolve("target/classes");
assertThat(collectRelativeFileNames(classesDirectory)) assertThat(collectRelativePaths(classesDirectory))
.contains("org/test/SampleApplication__ApplicationContextInitializer.class"); .contains(Path.of("org", "test", "SampleApplication__ApplicationContextInitializer.class"));
}); });
} }
@ -60,17 +64,17 @@ public class AotGenerateTests {
void whenAotRunsResourcesAreCopiedToTargetClasses(MavenBuild mavenBuild) { void whenAotRunsResourcesAreCopiedToTargetClasses(MavenBuild mavenBuild) {
mavenBuild.project("aot").goals("package").execute((project) -> { mavenBuild.project("aot").goals("package").execute((project) -> {
Path classesDirectory = project.toPath().resolve("target/classes/META-INF/native-image"); Path classesDirectory = project.toPath().resolve("target/classes/META-INF/native-image");
assertThat(collectRelativeFileNames(classesDirectory)).contains( assertThat(collectRelativePaths(classesDirectory)).contains(
"org.springframework.boot.maven.it/aot/reflect-config.json", Path.of("org.springframework.boot.maven.it", "aot", "reflect-config.json"),
"org.springframework.boot.maven.it/aot/resource-config.json", Path.of("org.springframework.boot.maven.it", "aot", "resource-config.json"),
"org.springframework.boot.maven.it/aot/native-image.properties"); Path.of("org.springframework.boot.maven.it", "aot", "native-image.properties"));
}); });
} }
Stream<String> collectRelativeFileNames(Path sourceDirectory) { Stream<Path> collectRelativePaths(Path sourceDirectory) {
try { try {
return Files.walk(sourceDirectory).filter(Files::isRegularFile) return Files.walk(sourceDirectory).filter(Files::isRegularFile)
.map((path) -> path.subpath(sourceDirectory.getNameCount(), path.getNameCount()).toString()); .map((path) -> path.subpath(sourceDirectory.getNameCount(), path.getNameCount()));
} }
catch (IOException ex) { catch (IOException ex) {
throw new IllegalStateException(ex); throw new IllegalStateException(ex);

@ -23,11 +23,9 @@ import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.StandardCopyOption; import java.nio.file.StandardCopyOption;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.stream.Collectors;
import javax.tools.Diagnostic; import javax.tools.Diagnostic;
import javax.tools.DiagnosticListener; import javax.tools.DiagnosticListener;
@ -83,7 +81,7 @@ public class AotGenerateMojo extends AbstractRunMojo {
throws MojoExecutionException, MojoFailureException { throws MojoExecutionException, MojoFailureException {
try { try {
generateAotAssets(workingDirectory, startClassName, environmentVariables); generateAotAssets(workingDirectory, startClassName, environmentVariables);
compileSourceFiles(getClassPathUrls()); compileSourceFiles();
copyNativeConfiguration(this.generatedResources.toPath()); copyNativeConfiguration(this.generatedResources.toPath());
} }
catch (Exception ex) { catch (Exception ex) {
@ -139,16 +137,17 @@ public class AotGenerateMojo extends AbstractRunMojo {
} }
} }
private void compileSourceFiles(URL[] classpathUrls) throws IOException { private void compileSourceFiles() throws IOException, MojoExecutionException {
List<Path> sourceFiles = Files.walk(this.generatedSources.toPath()).filter(Files::isRegularFile).toList(); List<Path> sourceFiles = Files.walk(this.generatedSources.toPath()).filter(Files::isRegularFile).toList();
if (sourceFiles.isEmpty()) { if (sourceFiles.isEmpty()) {
return; return;
} }
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
try (StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null)) { try (StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null)) {
List<String> options = List.of("-cp", List<String> options = new ArrayList<>();
Arrays.stream(classpathUrls).map(URL::toString).collect(Collectors.joining(File.pathSeparator)), addClasspath(options);
"-d", this.classesDirectory.toPath().toAbsolutePath().toString()); options.add("-d");
options.add(this.classesDirectory.toPath().toAbsolutePath().toString());
Iterable<? extends JavaFileObject> compilationUnits = fm.getJavaFileObjectsFromPaths(sourceFiles); Iterable<? extends JavaFileObject> compilationUnits = fm.getJavaFileObjectsFromPaths(sourceFiles);
Errors errors = new Errors(); Errors errors = new Errors();
CompilationTask task = compiler.getTask(null, fm, errors, options, null, compilationUnits); CompilationTask task = compiler.getTask(null, fm, errors, options, null, compilationUnits);

Loading…
Cancel
Save