Skip to content

Commit

Permalink
Fix test resources client on optimizer classpath (#853)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
melix authored Oct 20, 2023
1 parent 677acf7 commit 339023b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -449,48 +448,6 @@ private static String normalizePath(String path) {
}
}

private static class ExclusionSpec implements Spec<File> {
private final Provider<RegularFile> filterFile;
private final Set<String> prefixes;
private final Logger logger;
private Set<String> excludes;

private ExclusionSpec(Provider<RegularFile> filterFile,
Set<String> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 339023b

Please sign in to comment.