|
24 | 24 | import org.gradle.api.Project; |
25 | 25 | import org.gradle.api.Task; |
26 | 26 | import org.gradle.api.artifacts.Configuration; |
27 | | -import org.gradle.api.artifacts.ConfigurationContainer; |
28 | 27 | import org.gradle.api.file.FileCollection; |
29 | 28 | import org.gradle.api.plugins.JavaPlugin; |
30 | 29 | import org.gradle.api.provider.ProviderFactory; |
|
35 | 34 | import java.io.File; |
36 | 35 | import java.util.List; |
37 | 36 | import java.util.Map; |
| 37 | +import java.util.stream.Stream; |
38 | 38 |
|
39 | 39 | import javax.inject.Inject; |
40 | 40 |
|
| 41 | +import static java.util.stream.Collectors.joining; |
41 | 42 | import static org.elasticsearch.gradle.internal.util.ParamsUtils.loadBuildParams; |
42 | 43 | import static org.elasticsearch.gradle.util.FileUtils.mkdirs; |
43 | 44 | import static org.elasticsearch.gradle.util.GradleUtils.maybeConfigure; |
@@ -232,57 +233,64 @@ public void execute(Task t) { |
232 | 233 | } |
233 | 234 | }); |
234 | 235 | }); |
235 | | - configureJavaBasePatch(project); |
| 236 | + configureJavaBaseModuleOptions(project); |
236 | 237 | configureEntitlements(project); |
237 | 238 | } |
238 | 239 |
|
239 | 240 | /** |
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. |
243 | 242 | */ |
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) { |
245 | 274 | String patchProject = ":test:immutable-collections-patch"; |
246 | 275 | 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 |
248 | 277 | } |
249 | 278 | String configurationName = "immutableCollectionsPatch"; |
250 | 279 | FileCollection patchedFileCollection = project.getConfigurations() |
251 | 280 | .create(configurationName, config -> config.setCanBeConsumed(false)); |
252 | 281 | var deps = project.getDependencies(); |
253 | 282 | deps.add(configurationName, deps.project(Map.of("path", patchProject, "configuration", "patch"))); |
| 283 | + return patchedFileCollection; |
| 284 | + } |
254 | 285 |
|
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"); |
280 | 288 | } |
281 | 289 |
|
282 | 290 | /** |
283 | 291 | * Sets the required JVM options and system properties to enable entitlement enforcement on tests. |
284 | 292 | * <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, |
286 | 294 | * since the command line can have only one {@code --patch-module} option for a given module. |
287 | 295 | */ |
288 | 296 | private static void configureEntitlements(Project project) { |
|
0 commit comments