diff --git a/spring-context/src/main/java/org/springframework/context/aot/AbstractAotProcessor.java b/spring-context/src/main/java/org/springframework/context/aot/AbstractAotProcessor.java index de9bffc103..e538de87ab 100644 --- a/spring-context/src/main/java/org/springframework/context/aot/AbstractAotProcessor.java +++ b/spring-context/src/main/java/org/springframework/context/aot/AbstractAotProcessor.java @@ -23,6 +23,7 @@ import org.springframework.aot.generate.FileSystemGeneratedFiles; import org.springframework.aot.generate.GeneratedFiles.Kind; import org.springframework.aot.hint.RuntimeHints; import org.springframework.aot.nativex.FileNativeConfigurationWriter; +import org.springframework.lang.Nullable; import org.springframework.util.FileSystemUtils; /** @@ -43,77 +44,30 @@ import org.springframework.util.FileSystemUtils; */ public abstract class AbstractAotProcessor { - private final Path sourceOutput; - - private final Path resourceOutput; - - private final Path classOutput; - - private final String groupId; - - private final String artifactId; + private final Settings settings; /** - * Create a new processor instance. - * @param sourceOutput the location of generated sources - * @param resourceOutput the location of generated resources - * @param classOutput the location of generated classes - * @param groupId the group ID of the application, used to locate - * {@code native-image.properties} - * @param artifactId the artifact ID of the application, used to locate - * {@code native-image.properties} + * Create a new processor instance with the supplied {@linkplain Settings settings}. */ - protected AbstractAotProcessor(Path sourceOutput, Path resourceOutput, - Path classOutput, String groupId, String artifactId) { - - this.sourceOutput = sourceOutput; - this.resourceOutput = resourceOutput; - this.classOutput = classOutput; - this.groupId = groupId; - this.artifactId = artifactId; + protected AbstractAotProcessor(Settings settings) { + this.settings = settings; } - /** - * Get the output directory for generated sources. - */ - protected Path getSourceOutput() { - return this.sourceOutput; - } /** - * Get the output directory for generated resources. + * Get the {@linkplain Settings settings} for this AOT processor. */ - protected Path getResourceOutput() { - return this.resourceOutput; - } - - /** - * Get the output directory for generated classes. - */ - protected Path getClassOutput() { - return this.classOutput; - } - - /** - * Get the group ID of the application. - */ - protected String getGroupId() { - return this.groupId; - } - - /** - * Get the artifact ID of the application. - */ - protected String getArtifactId() { - return this.artifactId; + protected Settings getSettings() { + return this.settings; } /** * Delete the source, resource, and class output directories. */ protected void deleteExistingOutput() { - deleteExistingOutput(getSourceOutput(), getResourceOutput(), getClassOutput()); + deleteExistingOutput(getSettings().getSourceOutput(), + getSettings().getResourceOutput(), getSettings().getClassOutput()); } private void deleteExistingOutput(Path... paths) { @@ -133,16 +87,121 @@ public abstract class AbstractAotProcessor { private Path getRoot(Kind kind) { return switch (kind) { - case SOURCE -> getSourceOutput(); - case RESOURCE -> getResourceOutput(); - case CLASS -> getClassOutput(); + case SOURCE -> getSettings().getSourceOutput(); + case RESOURCE -> getSettings().getResourceOutput(); + case CLASS -> getSettings().getClassOutput(); }; } protected void writeHints(RuntimeHints hints) { - FileNativeConfigurationWriter writer = - new FileNativeConfigurationWriter(getResourceOutput(), getGroupId(), getArtifactId()); + FileNativeConfigurationWriter writer = new FileNativeConfigurationWriter( + getSettings().getResourceOutput(), getSettings().getGroupId(), getSettings().getArtifactId()); writer.write(hints); } + /** + * Common settings for AOT processors. + */ + public static class Settings { + + @Nullable + private Path sourceOutput; + + @Nullable + private Path resourceOutput; + + @Nullable + private Path classOutput; + + @Nullable + private String groupId; + + @Nullable + private String artifactId; + + + /** + * Set the output directory for generated sources. + * @param sourceOutput the location of generated sources + */ + public void setSourceOutput(Path sourceOutput) { + this.sourceOutput = sourceOutput; + } + + /** + * Get the output directory for generated sources. + */ + @Nullable + public Path getSourceOutput() { + return this.sourceOutput; + } + + /** + * Set the output directory for generated resources. + * @param resourceOutput the location of generated resources + */ + public void setResourceOutput(Path resourceOutput) { + this.resourceOutput = resourceOutput; + } + + /** + * Get the output directory for generated resources. + */ + @Nullable + public Path getResourceOutput() { + return this.resourceOutput; + } + + /** + * Set the output directory for generated classes. + * @param classOutput the location of generated classes + */ + public void setClassOutput(Path classOutput) { + this.classOutput = classOutput; + } + + /** + * Get the output directory for generated classes. + */ + @Nullable + public Path getClassOutput() { + return this.classOutput; + } + + /** + * Set the group ID of the application. + * @param groupId the group ID of the application, used to locate + * {@code native-image.properties} + */ + public void setGroupId(String groupId) { + this.groupId = groupId; + } + + /** + * Get the group ID of the application. + */ + @Nullable + public String getGroupId() { + return this.groupId; + } + + /** + * Set the artifact ID of the application. + * @param artifactId the artifact ID of the application, used to locate + * {@code native-image.properties} + */ + public void setArtifactId(String artifactId) { + this.artifactId = artifactId; + } + + /** + * Get the artifact ID of the application. + */ + @Nullable + public String getArtifactId() { + return this.artifactId; + } + + } + } diff --git a/spring-context/src/main/java/org/springframework/context/aot/ContextAotProcessor.java b/spring-context/src/main/java/org/springframework/context/aot/ContextAotProcessor.java index 0fc952f4c9..145c6555a5 100644 --- a/spring-context/src/main/java/org/springframework/context/aot/ContextAotProcessor.java +++ b/spring-context/src/main/java/org/springframework/context/aot/ContextAotProcessor.java @@ -50,20 +50,13 @@ public abstract class ContextAotProcessor extends AbstractAotProcessor { private final Class application; /** - * Create a new processor instance. + * Create a new processor for the specified application entry point and + * common settings. * @param application the application entry point - * @param sourceOutput the location of generated sources - * @param resourceOutput the location of generated resources - * @param classOutput the location of generated classes - * @param groupId the group ID of the application, used to locate - * {@code native-image.properties} - * @param artifactId the artifact ID of the application, used to locate - * {@code native-image.properties} + * @param settings the settings to apply */ - protected ContextAotProcessor(Class application, Path sourceOutput, Path resourceOutput, - Path classOutput, String groupId, String artifactId) { - - super(sourceOutput, resourceOutput, classOutput, groupId, artifactId); + protected ContextAotProcessor(Class application, Settings settings) { + super(settings); this.application = application; } @@ -160,8 +153,8 @@ public abstract class ContextAotProcessor extends AbstractAotProcessor { StringBuilder sb = new StringBuilder(); sb.append("Args = "); sb.append(String.join(String.format(" \\%n"), args)); - Path file = getResourceOutput() - .resolve("META-INF/native-image/" + getGroupId() + "/" + getArtifactId() + "/native-image.properties"); + Path file = getSettings().getResourceOutput().resolve("META-INF/native-image/" + + getSettings().getGroupId() + "/" + getSettings().getArtifactId() + "/native-image.properties"); try { if (!Files.exists(file)) { Files.createDirectories(file.getParent()); diff --git a/spring-context/src/test/java/org/springframework/context/aot/ContextAotProcessorTests.java b/spring-context/src/test/java/org/springframework/context/aot/ContextAotProcessorTests.java index 1b306574fe..f500ac9648 100644 --- a/spring-context/src/test/java/org/springframework/context/aot/ContextAotProcessorTests.java +++ b/spring-context/src/test/java/org/springframework/context/aot/ContextAotProcessorTests.java @@ -118,14 +118,23 @@ class ContextAotProcessorTests { private static class DemoContextAotProcessor extends ContextAotProcessor { - DemoContextAotProcessor(Class application, - Path sourceOutput, Path resourceOutput, Path classOutput) { - super(application, sourceOutput, resourceOutput, classOutput, "com.example", "example"); + DemoContextAotProcessor(Class application, Path rootPath) { + this(application, rootPath.resolve("source"), rootPath.resolve("resource"), rootPath.resolve("class")); } - DemoContextAotProcessor(Class application, Path rootPath) { - super(application, rootPath.resolve("source"), rootPath.resolve("resource"), - rootPath.resolve("class"), "com.example", "example"); + DemoContextAotProcessor(Class application, Path sourceOutput, Path resourceOutput, Path classOutput) { + super(application, createSettings(sourceOutput, resourceOutput, classOutput, "com.example", "example")); + } + + private static Settings createSettings(Path sourceOutput, Path resourceOutput, + Path classOutput, String groupId, String artifactId) { + Settings settings = new Settings(); + settings.setSourceOutput(sourceOutput); + settings.setResourceOutput(resourceOutput); + settings.setClassOutput(classOutput); + settings.setArtifactId(artifactId); + settings.setGroupId(groupId); + return settings; } @Override diff --git a/spring-test/src/main/java/org/springframework/test/context/aot/TestAotProcessor.java b/spring-test/src/main/java/org/springframework/test/context/aot/TestAotProcessor.java index bf2da8d2ef..3a6dc91b5a 100644 --- a/spring-test/src/main/java/org/springframework/test/context/aot/TestAotProcessor.java +++ b/spring-test/src/main/java/org/springframework/test/context/aot/TestAotProcessor.java @@ -43,20 +43,12 @@ public abstract class TestAotProcessor extends AbstractAotProcessor { /** * Create a new processor for the specified test classpath roots and - * general settings. + * common settings. * @param classpathRoots the classpath roots to scan for test classes - * @param sourceOutput the location of generated sources - * @param resourceOutput the location of generated resources - * @param classOutput the location of generated classes - * @param groupId the group ID of the application, used to locate - * {@code native-image.properties} - * @param artifactId the artifact ID of the application, used to locate - * {@code native-image.properties} + * @param settings the settings to apply */ - public TestAotProcessor(Set classpathRoots, Path sourceOutput, Path resourceOutput, Path classOutput, - String groupId, String artifactId) { - - super(sourceOutput, resourceOutput, classOutput, groupId, artifactId); + protected TestAotProcessor(Set classpathRoots, Settings settings) { + super(settings); this.classpathRoots = classpathRoots; } diff --git a/spring-test/src/test/java/org/springframework/test/context/aot/TestAotProcessorTests.java b/spring-test/src/test/java/org/springframework/test/context/aot/TestAotProcessorTests.java index c98c3ca2f2..4cccdcd3cd 100644 --- a/spring-test/src/test/java/org/springframework/test/context/aot/TestAotProcessorTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/aot/TestAotProcessorTests.java @@ -104,8 +104,20 @@ class TestAotProcessorTests extends AbstractAotTests { DemoTestAotProcessor(Set classpathRoots, Path sourceOutput, Path resourceOutput, Path classOutput, String groupId, String artifactId) { - super(classpathRoots, sourceOutput, resourceOutput, classOutput, groupId, artifactId); + super(classpathRoots, createSettings(sourceOutput, resourceOutput, classOutput, groupId, artifactId)); } + private static Settings createSettings(Path sourceOutput, Path resourceOutput, Path classOutput, String groupId, + String artifactId) { + Settings settings = new Settings(); + settings.setSourceOutput(sourceOutput); + settings.setResourceOutput(resourceOutput); + settings.setClassOutput(classOutput); + settings.setArtifactId(artifactId); + settings.setGroupId(groupId); + return settings; + } } + } +