This commit is contained in:
Phillip Webb
2022-07-29 12:07:36 +01:00
parent 41e8697445
commit e08c16dfd6
17 changed files with 134 additions and 111 deletions

View File

@@ -22,6 +22,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.attributes.AttributeContainer;
import org.gradle.api.attributes.LibraryElements;
import org.gradle.api.file.Directory;
import org.gradle.api.plugins.JavaPlugin;
@@ -77,12 +78,17 @@ public class SpringBootAotPlugin implements Plugin<Project> {
aotImplementation.extendsFrom(configurations.getByName(main.getImplementationConfigurationName()));
aotImplementation.extendsFrom(configurations.getByName(main.getRuntimeOnlyConfigurationName()));
configurations.getByName(aot.getCompileClasspathConfigurationName())
.attributes((attributes) -> attributes.attribute(LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE,
project.getObjects().named(LibraryElements.class, LibraryElements.CLASSES_AND_RESOURCES)));
.attributes((attributes) -> addLibraryElementsAttribute(project, attributes));
});
return aotSourceSet;
}
private AttributeContainer addLibraryElementsAttribute(Project project, AttributeContainer attributes) {
LibraryElements libraryElements = project.getObjects().named(LibraryElements.class,
LibraryElements.CLASSES_AND_RESOURCES);
return attributes.attribute(LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE, libraryElements);
}
private void registerGenerateAotSourcesTask(Project project, SourceSet aotSourceSet) {
TaskProvider<ResolveMainClassName> resolveMainClassName = project.getTasks()
.named(SpringBootPlugin.RESOLVE_MAIN_CLASS_NAME_TASK_NAME, ResolveMainClassName.class);

View File

@@ -157,9 +157,9 @@ public abstract class AbstractDependencyFilterMojo extends AbstractMojo {
return cleaned.toString();
}
static class TestArtifactFilter extends AbstractArtifactFeatureFilter {
protected static class TestScopeArtifactFilter extends AbstractArtifactFeatureFilter {
TestArtifactFilter() {
TestScopeArtifactFilter() {
super("", Artifact.SCOPE_TEST);
}

View File

@@ -371,7 +371,7 @@ public abstract class AbstractRunMojo extends AbstractDependencyFilterMojo {
private void addDependencies(List<URL> urls) throws MalformedURLException, MojoExecutionException {
Set<Artifact> artifacts = (this.useTestClasspath) ? filterDependencies(this.project.getArtifacts())
: filterDependencies(this.project.getArtifacts(), new TestArtifactFilter());
: filterDependencies(this.project.getArtifacts(), new TestScopeArtifactFilter());
for (Artifact artifact : artifacts) {
if (artifact.getFile() != null) {
urls.add(artifact.getFile().toURI().toURL());

View File

@@ -154,6 +154,18 @@ public class AotGenerateMojo extends AbstractDependencyFilterMojo {
private void generateAotAssets() throws MojoExecutionException {
String applicationClass = (this.mainClass != null) ? this.mainClass
: SpringBootApplicationClassFinder.findSingleClass(this.classesDirectory);
List<String> command = CommandLineBuilder.forMainClass(AOT_PROCESSOR_CLASS_NAME)
.withSystemProperties(this.systemPropertyVariables)
.withJvmArguments(new RunArguments(this.jvmArguments).asArray()).withClasspath(getClassPathUrls())
.withArguments(getAotArguments(applicationClass)).build();
if (getLog().isDebugEnabled()) {
getLog().debug("Generating AOT assets using command: " + command);
}
JavaProcessExecutor processExecutor = new JavaProcessExecutor(this.session, this.toolchainManager);
processExecutor.run(this.project.getBasedir(), command, Collections.emptyMap());
}
private String[] getAotArguments(String applicationClass) {
List<String> aotArguments = new ArrayList<>();
aotArguments.add(applicationClass);
aotArguments.add(this.generatedSources.toString());
@@ -164,25 +176,13 @@ public class AotGenerateMojo extends AbstractDependencyFilterMojo {
if (!ObjectUtils.isEmpty(this.profiles)) {
aotArguments.add("--spring.profiles.active=" + String.join(",", this.profiles));
}
// @formatter:off
List<String> args = CommandLineBuilder.forMainClass(AOT_PROCESSOR_CLASS_NAME)
.withSystemProperties(this.systemPropertyVariables)
.withJvmArguments(new RunArguments(this.jvmArguments).asArray())
.withClasspath(getClassPathUrls())
.withArguments(aotArguments.toArray(String[]::new))
.build();
// @formatter:on
if (getLog().isDebugEnabled()) {
getLog().debug("Generating AOT assets using command: " + args);
}
JavaProcessExecutor processExecutor = new JavaProcessExecutor(this.session, this.toolchainManager);
processExecutor.run(this.project.getBasedir(), args, Collections.emptyMap());
return aotArguments.toArray(String[]::new);
}
private URL[] getClassPathUrls() throws MojoExecutionException {
List<URL> urls = new ArrayList<>();
urls.add(toURL(this.classesDirectory));
urls.addAll(getDependencyURLs(new TestArtifactFilter()));
urls.addAll(getDependencyURLs(new TestScopeArtifactFilter()));
return urls.toArray(URL[]::new);
}