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.
*
* @author Stephane Nicoll
* @author Andy Wilkinson
*/
@ExtendWith(MavenBuildExtension.class)
public class AotGenerateTests {
@ -38,12 +39,15 @@ public class AotGenerateTests {
void whenAotRunsSourcesAreGenerated(MavenBuild mavenBuild) {
mavenBuild.project("aot").goals("package").execute((project) -> {
Path aotDirectory = project.toPath().resolve("target/spring-aot/main");
assertThat(collectRelativeFileNames(aotDirectory.resolve("sources")))
.contains("org/test/SampleApplication__ApplicationContextInitializer.java");
assertThat(collectRelativeFileNames(aotDirectory.resolve("resources"))).containsOnly(
"META-INF/native-image/org.springframework.boot.maven.it/aot/reflect-config.json",
"META-INF/native-image/org.springframework.boot.maven.it/aot/resource-config.json",
"META-INF/native-image/org.springframework.boot.maven.it/aot/native-image.properties");
assertThat(collectRelativePaths(aotDirectory.resolve("sources")))
.contains(Path.of("org", "test", "SampleApplication__ApplicationContextInitializer.java"));
assertThat(collectRelativePaths(aotDirectory.resolve("resources"))).containsOnly(
Path.of("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",
"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) {
mavenBuild.project("aot").goals("package").execute((project) -> {
Path classesDirectory = project.toPath().resolve("target/classes");
assertThat(collectRelativeFileNames(classesDirectory))
.contains("org/test/SampleApplication__ApplicationContextInitializer.class");
assertThat(collectRelativePaths(classesDirectory))
.contains(Path.of("org", "test", "SampleApplication__ApplicationContextInitializer.class"));
});
}
@ -60,17 +64,17 @@ public class AotGenerateTests {
void whenAotRunsResourcesAreCopiedToTargetClasses(MavenBuild mavenBuild) {
mavenBuild.project("aot").goals("package").execute((project) -> {
Path classesDirectory = project.toPath().resolve("target/classes/META-INF/native-image");
assertThat(collectRelativeFileNames(classesDirectory)).contains(
"org.springframework.boot.maven.it/aot/reflect-config.json",
"org.springframework.boot.maven.it/aot/resource-config.json",
"org.springframework.boot.maven.it/aot/native-image.properties");
assertThat(collectRelativePaths(classesDirectory)).contains(
Path.of("org.springframework.boot.maven.it", "aot", "reflect-config.json"),
Path.of("org.springframework.boot.maven.it", "aot", "resource-config.json"),
Path.of("org.springframework.boot.maven.it", "aot", "native-image.properties"));
});
}
Stream<String> collectRelativeFileNames(Path sourceDirectory) {
Stream<Path> collectRelativePaths(Path sourceDirectory) {
try {
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) {
throw new IllegalStateException(ex);

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

Loading…
Cancel
Save