Skip to content

Commit

Permalink
Add test sourceset support to MRJAR plugin (elastic#106571)
Browse files Browse the repository at this point in the history
Mrjar plugin supports main sourcesets automatically configured with
newer java versions. This commit adds support for test sourcesets as
well. The key difference between main and test is that test sourcesets
also extend their main counterparts.
  • Loading branch information
rjernst authored Mar 21, 2024
1 parent edefbbc commit 3f8cc1e
Showing 1 changed file with 81 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,48 +60,30 @@ public void apply(Project project) {
project.getPluginManager().apply(ElasticsearchJavaBasePlugin.class);
var javaExtension = project.getExtensions().getByType(JavaPluginExtension.class);

var srcDir = project.getProjectDir().toPath().resolve("src");
List<Integer> mainVersions = new ArrayList<>();
try (var subdirStream = Files.list(srcDir)) {
for (Path sourceset : subdirStream.toList()) {
assert Files.isDirectory(sourceset);
String sourcesetName = sourceset.getFileName().toString();
Matcher sourcesetMatcher = MRJAR_SOURCESET_PATTERN.matcher(sourcesetName);
if (sourcesetMatcher.matches()) {
mainVersions.add(Integer.parseInt(sourcesetMatcher.group(1)));
}
}
} catch (IOException e) {
throw new UncheckedIOException(e);
}

Collections.sort(mainVersions);
List<String> parentSourceSets = new ArrayList<>();
parentSourceSets.add(SourceSet.MAIN_SOURCE_SET_NAME);
List<Integer> mainVersions = findSourceVersions(project);
List<String> mainSourceSets = new ArrayList<>();
mainSourceSets.add(SourceSet.MAIN_SOURCE_SET_NAME);
List<String> testSourceSets = new ArrayList<>(mainSourceSets);
testSourceSets.add(SourceSet.TEST_SOURCE_SET_NAME);
for (int javaVersion : mainVersions) {
String sourcesetName = "main" + javaVersion;
addMrjarSourceset(project, javaExtension, sourcesetName, parentSourceSets, javaVersion);
parentSourceSets.add(sourcesetName);
String mainSourceSetName = SourceSet.MAIN_SOURCE_SET_NAME + javaVersion;
SourceSet mainSourceSet = addSourceSet(project, javaExtension, mainSourceSetName, mainSourceSets, javaVersion);
configureSourceSetInJar(project, mainSourceSet, javaVersion);
mainSourceSets.add(mainSourceSetName);
testSourceSets.add(mainSourceSetName);

String testSourceSetName = SourceSet.TEST_SOURCE_SET_NAME + javaVersion;
SourceSet testSourceSet = addSourceSet(project, javaExtension, testSourceSetName, testSourceSets, javaVersion);
testSourceSets.add(testSourceSetName);
createTestTask(project, testSourceSet, javaVersion, mainSourceSets);
}
}

private void addMrjarSourceset(
Project project,
JavaPluginExtension javaExtension,
String sourcesetName,
List<String> parentSourceSets,
int javaVersion
) {
SourceSet sourceSet = javaExtension.getSourceSets().maybeCreate(sourcesetName);
for (String parentSourceSetName : parentSourceSets) {
GradleUtils.extendSourceSet(project, parentSourceSetName, sourcesetName);
}
configureMrjar(project);
}

private void configureMrjar(Project project) {
var jarTask = project.getTasks().withType(Jar.class).named(JavaPlugin.JAR_TASK_NAME);
jarTask.configure(task -> {
task.into("META-INF/versions/" + javaVersion, copySpec -> copySpec.from(sourceSet.getOutput()));
task.manifest(manifest -> { manifest.attributes(Map.of("Multi-Release", "true")); });
});
jarTask.configure(task -> { task.manifest(manifest -> { manifest.attributes(Map.of("Multi-Release", "true")); }); });

project.getTasks().withType(Test.class).named(JavaPlugin.TEST_TASK_NAME).configure(testTask -> {
testTask.dependsOn(jarTask);
Expand All @@ -111,6 +93,19 @@ private void addMrjarSourceset(
FileCollection testRuntime = sourceSets.getByName(SourceSet.TEST_SOURCE_SET_NAME).getRuntimeClasspath();
testTask.setClasspath(testRuntime.minus(mainRuntime).plus(project.files(jarTask)));
});
}

private SourceSet addSourceSet(
Project project,
JavaPluginExtension javaExtension,
String sourceSetName,
List<String> parentSourceSets,
int javaVersion
) {
SourceSet sourceSet = javaExtension.getSourceSets().maybeCreate(sourceSetName);
for (String parentSourceSetName : parentSourceSets) {
GradleUtils.extendSourceSet(project, parentSourceSetName, sourceSetName);
}

project.getTasks().withType(JavaCompile.class).named(sourceSet.getCompileJavaTaskName()).configure(compileTask -> {
compileTask.getJavaCompiler()
Expand All @@ -132,6 +127,55 @@ private void addMrjarSourceset(
project.getTasks().withType(CheckForbiddenApisTask.class).named(forbiddenApisTaskName).configure(forbiddenApisTask -> {
forbiddenApisTask.setIgnoreMissingClasses(true);
});

return sourceSet;
}

private void configureSourceSetInJar(Project project, SourceSet sourceSet, int javaVersion) {
var jarTask = project.getTasks().withType(Jar.class).named(JavaPlugin.JAR_TASK_NAME);
jarTask.configure(task -> task.into("META-INF/versions/" + javaVersion, copySpec -> copySpec.from(sourceSet.getOutput())));
}

private void createTestTask(Project project, SourceSet sourceSet, int javaVersion, List<String> mainSourceSets) {
var jarTask = project.getTasks().withType(Jar.class).named(JavaPlugin.JAR_TASK_NAME);
var testTaskProvider = project.getTasks().register(JavaPlugin.TEST_TASK_NAME + javaVersion, Test.class);
testTaskProvider.configure(testTask -> {
testTask.dependsOn(jarTask);

SourceSetContainer sourceSets = GradleUtils.getJavaSourceSets(project);
FileCollection testRuntime = sourceSet.getRuntimeClasspath();
for (String mainSourceSetName : mainSourceSets) {
FileCollection mainRuntime = sourceSets.getByName(mainSourceSetName).getOutput();
testRuntime = testRuntime.minus(mainRuntime);
}
testTask.setClasspath(testRuntime.plus(project.files(jarTask)));
testTask.setTestClassesDirs(sourceSet.getOutput().getClassesDirs());

testTask.getJavaLauncher()
.set(javaToolchains.launcherFor(spec -> spec.getLanguageVersion().set(JavaLanguageVersion.of(javaVersion))));
});

project.getTasks().named("check").configure(checkTask -> checkTask.dependsOn(testTaskProvider));
}

private static List<Integer> findSourceVersions(Project project) {
var srcDir = project.getProjectDir().toPath().resolve("src");
List<Integer> versions = new ArrayList<>();
try (var subdirStream = Files.list(srcDir)) {
for (Path sourceSetPath : subdirStream.toList()) {
assert Files.isDirectory(sourceSetPath);
String sourcesetName = sourceSetPath.getFileName().toString();
Matcher sourcesetMatcher = MRJAR_SOURCESET_PATTERN.matcher(sourcesetName);
if (sourcesetMatcher.matches()) {
versions.add(Integer.parseInt(sourcesetMatcher.group(1)));
}
}
} catch (IOException e) {
throw new UncheckedIOException(e);
}

Collections.sort(versions);
return versions;
}

private static void stripPreviewFromFiles(Path compileDir) {
Expand Down

0 comments on commit 3f8cc1e

Please sign in to comment.