Search in sources :

Example 1 with GenerateAotTestSources

use of org.springframework.aot.gradle.tasks.GenerateAotTestSources in project spring-native by spring-projects-experimental.

the class SpringAotGradlePlugin method apply.

@Override
public void apply(final Project project) {
    project.getPluginManager().apply(NativeImagePlugin.class);
    project.getPlugins().withType(JavaPlugin.class, javaPlugin -> {
        GraalVMExtension graal = project.getExtensions().findByType(GraalVMExtension.class);
        graal.getToolchainDetection().set(false);
        // Create the "springAot" DSL extension for user configuration
        project.getExtensions().create(EXTENSION_NAME, SpringAotExtension.class, project.getObjects());
        // Automatically add the spring-native dependency to the implementation configuration
        // as it's required for hints
        addSpringNativeDependency(project);
        Path generatedFilesPath = createGeneratedSourcesFolder(project);
        // deprecation replaced with new API introduced in Gradle 7.1
        // noinspection deprecation
        SourceSetContainer sourceSets = project.getConvention().getPlugin(JavaPluginConvention.class).getSourceSets();
        // Create a detached configuration that holds dependencies for AOT generation
        SourceSet aotMainSourceSet = createAotMainSourceSet(sourceSets, project.getConfigurations(), generatedFilesPath);
        GenerateAotSources generateAotSources = createGenerateAotSourcesTask(project, sourceSets);
        project.getTasks().named(aotMainSourceSet.getCompileJavaTaskName(), JavaCompile.class, (aotCompileJava) -> {
            aotCompileJava.source(generateAotSources.getSourcesOutputDirectory());
        });
        project.getTasks().named(aotMainSourceSet.getProcessResourcesTaskName(), Copy.class, (aotProcessResources) -> {
            aotProcessResources.from(generateAotSources.getResourcesOutputDirectory());
            aotProcessResources.setDuplicatesStrategy(DuplicatesStrategy.INCLUDE);
        });
        Jar generatedSourcesJar = project.getTasks().create(aotMainSourceSet.getJarTaskName(), Jar.class, jar -> {
            jar.from(aotMainSourceSet.getOutput());
            jar.setGroup(BasePlugin.BUILD_GROUP);
            jar.getArchiveClassifier().convention("aot");
        });
        createAotMainConfiguration(project, aotMainSourceSet);
        // Generated+compiled sources must be used by 'bootRun' and packaged by 'bootJar'
        project.getPlugins().withType(SpringBootPlugin.class, springBootPlugin -> {
            Provider<RegularFile> generatedSources = generatedSourcesJar.getArchiveFile();
            project.getTasks().named(SpringBootPlugin.BOOT_JAR_TASK_NAME, BootJar.class, (bootJar) -> {
                bootJar.setClasspath(project.files(generatedSources).plus(bootJar.getClasspath()));
            });
            project.getTasks().named("bootRun", BootRun.class, (bootRun) -> {
                bootRun.setClasspath(project.files(generatedSources).plus(bootRun.getClasspath()));
            });
        });
        // Create a detached configuration that holds dependencies for AOT test generation
        SourceSet aotTestSourceSet = createAotTestSourceSet(sourceSets, project.getConfigurations(), generatedFilesPath);
        GenerateAotTestSources generateAotTestSources = createGenerateAotTestSourcesTask(project, sourceSets);
        project.getTasks().named(aotTestSourceSet.getCompileJavaTaskName(), JavaCompile.class, (aotTestCompileJava) -> {
            aotTestCompileJava.source(generateAotTestSources.getGeneratedSourcesOutputDirectory());
        });
        project.getTasks().named(aotTestSourceSet.getProcessResourcesTaskName(), Copy.class, (aotTestProcessResources) -> {
            aotTestProcessResources.from(generateAotTestSources.getGeneratedResourcesOutputDirectory());
            aotTestProcessResources.setDuplicatesStrategy(DuplicatesStrategy.INCLUDE);
        });
        Jar generatedTestSourcesJar = project.getTasks().create(aotTestSourceSet.getJarTaskName(), Jar.class, jar -> {
            jar.from(aotTestSourceSet.getOutput());
            jar.setGroup(JavaBasePlugin.VERIFICATION_GROUP);
            jar.getArchiveClassifier().convention("aot-test");
        });
        // Create the aotTest task to allow execution of both regular and aotTest
        createAotTestTask(project, sourceSets, generatedTestSourcesJar);
        project.getPlugins().withType(NativeImagePlugin.class, nativeImagePlugin -> {
            project.getTasks().named(NativeImagePlugin.NATIVE_COMPILE_TASK_NAME).configure(nativeCompile -> {
                nativeCompile.dependsOn(generatedSourcesJar);
            });
            graal.getBinaries().named(NativeImagePlugin.NATIVE_MAIN_EXTENSION).configure(options -> {
                Provider<RegularFile> generatedSources = generatedSourcesJar.getArchiveFile();
                options.classpath(generatedSources);
            });
            graal.getBinaries().named(NativeImagePlugin.NATIVE_TEST_EXTENSION).configure(options -> {
                Provider<RegularFile> generatedTestSources = generatedTestSourcesJar.getArchiveFile();
                options.classpath(generatedTestSources);
                options.runtimeArgs("-Dspring.test.context.default.CacheAwareContextLoaderDelegate=" + "org.springframework.aot.test.AotCacheAwareContextLoaderDelegate");
            });
        });
        // Ensure that Kotlin compilation depends on AOT source generation
        project.getPlugins().withId("org.jetbrains.kotlin.jvm", kotlinPlugin -> {
            project.getTasks().named("compileAotMainKotlin").configure(compileKotlin -> compileKotlin.dependsOn(generateAotSources));
        });
    });
}
Also used : Path(java.nio.file.Path) SourceSet(org.gradle.api.tasks.SourceSet) GraalVMExtension(org.graalvm.buildtools.gradle.dsl.GraalVMExtension) JavaPluginConvention(org.gradle.api.plugins.JavaPluginConvention) RegularFile(org.gradle.api.file.RegularFile) BootJar(org.springframework.boot.gradle.tasks.bundling.BootJar) Jar(org.gradle.api.tasks.bundling.Jar) GenerateAotTestSources(org.springframework.aot.gradle.tasks.GenerateAotTestSources) SourceSetContainer(org.gradle.api.tasks.SourceSetContainer) GenerateAotSources(org.springframework.aot.gradle.tasks.GenerateAotSources)

Example 2 with GenerateAotTestSources

use of org.springframework.aot.gradle.tasks.GenerateAotTestSources in project spring-native by spring-projects-experimental.

the class SpringAotGradlePlugin method createGenerateAotTestSourcesTask.

private GenerateAotTestSources createGenerateAotTestSourcesTask(Project project, SourceSetContainer sourceSets) {
    SourceSet testSourceSet = sourceSets.findByName(SourceSet.TEST_SOURCE_SET_NAME);
    SourceSet aotTestSourceSet = sourceSets.findByName(AOT_TEST_SOURCE_SET_NAME);
    GenerateAotTestSources generate = project.getTasks().create(GENERATE_TEST_TASK_NAME, GenerateAotTestSources.class);
    generate.setTestSourceSetOutputDirectories(testSourceSet.getOutput());
    generate.getGeneratedSourcesOutputDirectory().set(aotTestSourceSet.getJava().getSourceDirectories().getSingleFile());
    generate.getGeneratedResourcesOutputDirectory().set(aotTestSourceSet.getResources().getSourceDirectories().getSingleFile());
    generate.setClasspath(createAotTestGenerationConfiguration(project).plus(aotTestSourceSet.getCompileClasspath()));
    generate.setResourceInputDirectories(testSourceSet.getResources());
    generate.setDebug(isDebug());
    generate.getDebugOptions().getPort().set(getDebugPort());
    generate.setGroup(JavaBasePlugin.VERIFICATION_GROUP);
    configureToolchainConvention(project, generate);
    return generate;
}
Also used : SourceSet(org.gradle.api.tasks.SourceSet) GenerateAotTestSources(org.springframework.aot.gradle.tasks.GenerateAotTestSources)

Example 3 with GenerateAotTestSources

use of org.springframework.aot.gradle.tasks.GenerateAotTestSources in project spring-native by spring-projects-experimental.

the class SpringAotGradlePluginTest method pluginRegistersAotTestTask.

@Test
void pluginRegistersAotTestTask() {
    Project project = createTestProject();
    JavaPluginExtension java = project.getExtensions().findByType(JavaPluginExtension.class);
    SourceSet aotTestSourceSet = java.getSourceSets().findByName(SpringAotGradlePlugin.AOT_TEST_SOURCE_SET_NAME);
    TaskProvider<GenerateAotTestSources> generateAotTestSourcesProvider = project.getTasks().withType(GenerateAotTestSources.class).named(SpringAotGradlePlugin.GENERATE_TEST_TASK_NAME);
    assertThat(generateAotTestSourcesProvider.isPresent()).isTrue();
    TaskProvider<Task> compileAotProvider = project.getTasks().named(aotTestSourceSet.getCompileJavaTaskName());
    assertThat(compileAotProvider.isPresent()).isTrue();
}
Also used : DefaultProject(org.gradle.api.internal.project.DefaultProject) Project(org.gradle.api.Project) SourceSet(org.gradle.api.tasks.SourceSet) Task(org.gradle.api.Task) JavaPluginExtension(org.gradle.api.plugins.JavaPluginExtension) GenerateAotTestSources(org.springframework.aot.gradle.tasks.GenerateAotTestSources) Test(org.junit.jupiter.api.Test)

Aggregations

SourceSet (org.gradle.api.tasks.SourceSet)3 GenerateAotTestSources (org.springframework.aot.gradle.tasks.GenerateAotTestSources)3 Path (java.nio.file.Path)1 GraalVMExtension (org.graalvm.buildtools.gradle.dsl.GraalVMExtension)1 Project (org.gradle.api.Project)1 Task (org.gradle.api.Task)1 RegularFile (org.gradle.api.file.RegularFile)1 DefaultProject (org.gradle.api.internal.project.DefaultProject)1 JavaPluginConvention (org.gradle.api.plugins.JavaPluginConvention)1 JavaPluginExtension (org.gradle.api.plugins.JavaPluginExtension)1 SourceSetContainer (org.gradle.api.tasks.SourceSetContainer)1 Jar (org.gradle.api.tasks.bundling.Jar)1 Test (org.junit.jupiter.api.Test)1 GenerateAotSources (org.springframework.aot.gradle.tasks.GenerateAotSources)1 BootJar (org.springframework.boot.gradle.tasks.bundling.BootJar)1