From 339023b1663a80e39629df1a9543b1896d4927c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Champeau?= Date: Fri, 20 Oct 2023 18:26:05 +0200 Subject: [PATCH] Fix test resources client on optimizer classpath (#853) The AOT process should optimize applications for the production environment. As such it doesn't make sense to have the test resources client on the classpath of the application being scanned during the AOT optimization process. This bugfix has the potential to break builds of people who run `./gradlew build` and had builds passing because the AOT optimizer was using test resources. Fixes #852 --- .../gradle/aot/MicronautAotPlugin.java | 43 ------------------- .../TestResourcesWithAotSpec.groovy | 31 +++++++++++++ .../internal/TestResourcesAOT.java | 2 - 3 files changed, 31 insertions(+), 45 deletions(-) diff --git a/aot-plugin/src/main/java/io/micronaut/gradle/aot/MicronautAotPlugin.java b/aot-plugin/src/main/java/io/micronaut/gradle/aot/MicronautAotPlugin.java index 89133511..b8fa8da0 100644 --- a/aot-plugin/src/main/java/io/micronaut/gradle/aot/MicronautAotPlugin.java +++ b/aot-plugin/src/main/java/io/micronaut/gradle/aot/MicronautAotPlugin.java @@ -50,7 +50,6 @@ import org.gradle.api.plugins.JavaPlugin; import org.gradle.api.provider.Provider; import org.gradle.api.provider.ProviderFactory; -import org.gradle.api.specs.Spec; import org.gradle.api.tasks.JavaExec; import org.gradle.api.tasks.SourceSet; import org.gradle.api.tasks.TaskContainer; @@ -449,48 +448,6 @@ private static String normalizePath(String path) { } } - private static class ExclusionSpec implements Spec { - private final Provider filterFile; - private final Set prefixes; - private final Logger logger; - private Set excludes; - - private ExclusionSpec(Provider filterFile, - Set prefixes, - Logger logger) { - this.filterFile = filterFile; - this.prefixes = prefixes; - this.logger = logger; - } - - @Override - public boolean isSatisfiedBy(File file) { - if (excludes == null) { - File resourceFilter = filterFile.get().getAsFile(); - try { - excludes = new HashSet<>(); - Files.readAllLines(resourceFilter.toPath()) - .stream() - .map(JarExclusionSpec::normalizePath) - .forEach(excludes::add); - } catch (IOException e) { - throw new RuntimeException(e); - } - logger.debug("Excluded resources: {} ", excludes); - } - var relativePath = file.toString(); - if (excludes.contains(normalizePath(relativePath)) || prefixes.stream().anyMatch(p -> normalizePath(relativePath).startsWith(p))) { - return false; - } - return true; - } - - private static String normalizePath(String path) { - return path.replace('\\', '/'); - } - - } - private static final class Configurations { private final Configuration aotOptimizerRuntimeClasspath; private final Configuration aotApplication; diff --git a/functional-tests/src/test/groovy/io/micronaut/gradle/testresources/TestResourcesWithAotSpec.groovy b/functional-tests/src/test/groovy/io/micronaut/gradle/testresources/TestResourcesWithAotSpec.groovy index b69139f5..9c93793c 100644 --- a/functional-tests/src/test/groovy/io/micronaut/gradle/testresources/TestResourcesWithAotSpec.groovy +++ b/functional-tests/src/test/groovy/io/micronaut/gradle/testresources/TestResourcesWithAotSpec.groovy @@ -50,4 +50,35 @@ micronaut { !result.output.contains("Configuration with name 'optimizedRuntimeClasspath' not found.") result.task(':help').outcome == TaskOutcome.SUCCESS } + + @Issue("https://github.com/micronaut-projects/micronaut-gradle-plugin/issues/852") + def "test resources client shouldn't be on the AOT classpath"() { + settingsFile << "rootProject.name = 'test'" + buildFile << """plugins { + id("io.micronaut.minimal.library") + id("io.micronaut.aot") + id("io.micronaut.test-resources") +} + +repositories { + mavenCentral() +} + +micronaut { + version "$micronautVersion" +} + +tasks.named("prepareJitOptimizations") { + doFirst { + assert classpath.files.stream().noneMatch { it.name.startsWith("micronaut-test-resources-client-") } + } +} + +""" + when: + def result = build 'prepareJitOptimizations' + + then: + result.task(":prepareJitOptimizations").outcome == TaskOutcome.SUCCESS + } } diff --git a/test-resources-plugin/src/main/java/io/micronaut/gradle/testresources/internal/TestResourcesAOT.java b/test-resources-plugin/src/main/java/io/micronaut/gradle/testresources/internal/TestResourcesAOT.java index 68621324..7202fd91 100644 --- a/test-resources-plugin/src/main/java/io/micronaut/gradle/testresources/internal/TestResourcesAOT.java +++ b/test-resources-plugin/src/main/java/io/micronaut/gradle/testresources/internal/TestResourcesAOT.java @@ -36,8 +36,6 @@ public final class TestResourcesAOT { public static void configure(Project project, Configuration client) { AOTExtension aot = PluginsHelper.findMicronautExtension(project).getExtensions().getByType(AOTExtension.class); ConfigurationContainer configurations = project.getConfigurations(); - Configuration aotAppClasspath = configurations.getByName(MicronautAotPlugin.AOT_APPLICATION_CLASSPATH); - aotAppClasspath.extendsFrom(client); project.getPluginManager().withPlugin("io.micronaut.minimal.application", unused -> { Configuration optimizedRuntimeClasspath = configurations.getByName(MicronautAotPlugin.OPTIMIZED_RUNTIME_CLASSPATH_CONFIGURATION_NAME); optimizedRuntimeClasspath.extendsFrom(client);