Skip to content

Commit 40e89d1

Browse files
committed
Moritz's configureJavaBaseModuleOptions
1 parent 4be45a2 commit 40e89d1

File tree

1 file changed

+41
-33
lines changed

1 file changed

+41
-33
lines changed

build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/ElasticsearchTestBasePlugin.java

Lines changed: 41 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import org.gradle.api.Project;
2525
import org.gradle.api.Task;
2626
import org.gradle.api.artifacts.Configuration;
27-
import org.gradle.api.artifacts.ConfigurationContainer;
2827
import org.gradle.api.file.FileCollection;
2928
import org.gradle.api.plugins.JavaPlugin;
3029
import org.gradle.api.provider.ProviderFactory;
@@ -35,9 +34,11 @@
3534
import java.io.File;
3635
import java.util.List;
3736
import java.util.Map;
37+
import java.util.stream.Stream;
3838

3939
import javax.inject.Inject;
4040

41+
import static java.util.stream.Collectors.joining;
4142
import static org.elasticsearch.gradle.internal.util.ParamsUtils.loadBuildParams;
4243
import static org.elasticsearch.gradle.util.FileUtils.mkdirs;
4344
import static org.elasticsearch.gradle.util.GradleUtils.maybeConfigure;
@@ -232,57 +233,64 @@ public void execute(Task t) {
232233
}
233234
});
234235
});
235-
configureJavaBasePatch(project);
236+
configureJavaBaseModuleOptions(project);
236237
configureEntitlements(project);
237238
}
238239

239240
/**
240-
* Computes and sets the {@code --patch-module=java.base} JVM command line option.
241-
* <p>
242-
* Since each module can be patched only once, this method computes all patching required for {@code java.base}.
241+
* Computes and sets the {@code --patch-module=java.base} and {@code --add-opens=java.base} JVM command line options.
243242
*/
244-
private void configureJavaBasePatch(Project project) {
243+
private void configureJavaBaseModuleOptions(Project project) {
244+
project.getTasks().withType(Test.class).matching(task -> task.getName().equals("test")).configureEach(test -> {
245+
FileCollection patchedImmutableCollections = patchedImmutableCollections(project);
246+
if (patchedImmutableCollections != null) {
247+
test.getInputs().files(patchedImmutableCollections);
248+
test.systemProperty("tests.hackImmutableCollections", "true");
249+
}
250+
251+
FileCollection entitlementBridgeJar = entitlementBridgeJar(project);
252+
if (entitlementBridgeJar != null) {
253+
test.getInputs().files(entitlementBridgeJar);
254+
}
255+
256+
test.getJvmArgumentProviders().add(() -> {
257+
String javaBasePatch = Stream.concat(
258+
singleFilePath(patchedImmutableCollections).map(str -> str + "/java.base"),
259+
singleFilePath(entitlementBridgeJar)
260+
).collect(joining(File.pathSeparator));
261+
262+
return javaBasePatch.isEmpty()
263+
? List.of()
264+
: List.of("--patch-module=java.base=" + javaBasePatch, "--add-opens=java.base/java.util=ALL-UNNAMED");
265+
});
266+
});
267+
}
268+
269+
private Stream<String> singleFilePath(FileCollection collection) {
270+
return Stream.ofNullable(collection).map(FileCollection::getSingleFile).map(File::toString);
271+
}
272+
273+
private static FileCollection patchedImmutableCollections(Project project) {
245274
String patchProject = ":test:immutable-collections-patch";
246275
if (project.findProject(patchProject) == null) {
247-
return; // build tests may not have this project, just skip
276+
return null; // build tests may not have this project, just skip
248277
}
249278
String configurationName = "immutableCollectionsPatch";
250279
FileCollection patchedFileCollection = project.getConfigurations()
251280
.create(configurationName, config -> config.setCanBeConsumed(false));
252281
var deps = project.getDependencies();
253282
deps.add(configurationName, deps.project(Map.of("path", patchProject, "configuration", "patch")));
283+
return patchedFileCollection;
284+
}
254285

255-
// If ElasticsearchJavaBasePlugin has specified a dependency on the entitlement bridge jar,
256-
// then it needs to be added to the --patch-module=java.base command line option.
257-
ConfigurationContainer configurations = project.getConfigurations();
258-
259-
project.getTasks().withType(Test.class).matching(task -> task.getName().equals("test")).configureEach(test -> {
260-
test.getInputs().files(patchedFileCollection);
261-
test.systemProperty("tests.hackImmutableCollections", "true");
262-
263-
Configuration bridgeConfig = configurations.findByName("entitlementBridge");
264-
String bridgeJarPart;
265-
if (bridgeConfig == null) {
266-
bridgeJarPart = "";
267-
} else {
268-
test.getInputs().files(bridgeConfig);
269-
bridgeJarPart = File.pathSeparator + bridgeConfig.getSingleFile().getAbsolutePath();
270-
}
271-
272-
test.getJvmArgumentProviders()
273-
.add(
274-
() -> List.of(
275-
"--patch-module=java.base=" + patchedFileCollection.getSingleFile() + "/java.base" + bridgeJarPart,
276-
"--add-opens=java.base/java.util=ALL-UNNAMED"
277-
)
278-
);
279-
});
286+
private static FileCollection entitlementBridgeJar(Project project) {
287+
return project.getConfigurations().findByName("entitlementBridgeJar");
280288
}
281289

282290
/**
283291
* Sets the required JVM options and system properties to enable entitlement enforcement on tests.
284292
* <p>
285-
* One command line option is set in {@link #configureJavaBasePatch} out of necessity,
293+
* One command line option is set in {@link #configureJavaBaseModuleOptions} out of necessity,
286294
* since the command line can have only one {@code --patch-module} option for a given module.
287295
*/
288296
private static void configureEntitlements(Project project) {

0 commit comments

Comments
 (0)