@@ -229,8 +229,7 @@ public Set<File> getJarsToScan() {
229
229
@ TaskAction
230
230
public void runThirdPartyAudit () throws IOException {
231
231
Set <File > jars = getJarsToScan ();
232
-
233
- extractJars (jars );
232
+ Set <File > extractedJars = extractJars (jars );
234
233
235
234
final String forbiddenApisOutput = runForbiddenAPIsCli ();
236
235
@@ -248,7 +247,7 @@ public void runThirdPartyAudit() throws IOException {
248
247
249
248
Set <String > jdkJarHellClasses = null ;
250
249
if (this .jarHellEnabled ) {
251
- jdkJarHellClasses = runJdkJarHellCheck ();
250
+ jdkJarHellClasses = runJdkJarHellCheck (extractedJars );
252
251
}
253
252
254
253
if (missingClassExcludes != null ) {
@@ -301,16 +300,26 @@ private void logForbiddenAPIsOutput(String forbiddenApisOutput) {
301
300
getLogger ().error ("Forbidden APIs output:\n {}==end of forbidden APIs==" , forbiddenApisOutput );
302
301
}
303
302
304
- private void extractJars (Set <File > jars ) {
303
+ /**
304
+ * Extract project jars to build directory as specified by getJarExpandDir.
305
+ * Handle multi release jars by keeping versions closest to `targetCompatibility` version.
306
+ * @param jars to extract to build dir
307
+ * @return File set of extracted jars
308
+ */
309
+ private Set <File > extractJars (Set <File > jars ) {
310
+ Set <File > extractedJars = new TreeSet <>();
305
311
File jarExpandDir = getJarExpandDir ();
306
312
// We need to clean up to make sure old dependencies don't linger
307
313
getProject ().delete (jarExpandDir );
308
314
309
315
jars .forEach (jar -> {
316
+ String jarPrefix = jar .getName ().replace (".jar" , "" );
317
+ File jarSubDir = new File (jarExpandDir , jarPrefix );
318
+ extractedJars .add (jarSubDir );
310
319
FileTree jarFiles = getProject ().zipTree (jar );
311
320
getProject ().copy (spec -> {
312
321
spec .from (jarFiles );
313
- spec .into (jarExpandDir );
322
+ spec .into (jarSubDir );
314
323
// exclude classes from multi release jars
315
324
spec .exclude ("META-INF/versions/**" );
316
325
});
@@ -329,14 +338,16 @@ private void extractJars(Set<File> jars) {
329
338
Integer .parseInt (targetCompatibility .get ().getMajorVersion ())
330
339
).forEach (majorVersion -> getProject ().copy (spec -> {
331
340
spec .from (getProject ().zipTree (jar ));
332
- spec .into (jarExpandDir );
341
+ spec .into (jarSubDir );
333
342
String metaInfPrefix = "META-INF/versions/" + majorVersion ;
334
343
spec .include (metaInfPrefix + "/**" );
335
344
// Drop the version specific prefix
336
345
spec .eachFile (details -> details .setPath (details .getPath ().replace (metaInfPrefix , "" )));
337
346
spec .setIncludeEmptyDirs (false );
338
347
}));
339
348
});
349
+
350
+ return extractedJars ;
340
351
}
341
352
342
353
private void assertNoJarHell (Set <String > jdkJarHellClasses ) {
@@ -398,7 +409,12 @@ private String runForbiddenAPIsCli() throws IOException {
398
409
return forbiddenApisOutput ;
399
410
}
400
411
401
- private Set <String > runJdkJarHellCheck () throws IOException {
412
+ /**
413
+ * Execute java with JDK_JAR_HELL_MAIN_CLASS against provided jars with OpenSearch core in the classpath.
414
+ * @param jars to scan for jarHell violations.
415
+ * @return standard out of jarHell process.
416
+ */
417
+ private Set <String > runJdkJarHellCheck (Set <File > jars ) throws IOException {
402
418
ByteArrayOutputStream standardOut = new ByteArrayOutputStream ();
403
419
InjectedExecOps execOps = getProject ().getObjects ().newInstance (InjectedExecOps .class );
404
420
ExecResult execResult = execOps .getExecOps ().javaexec (spec -> {
@@ -407,9 +423,8 @@ private Set<String> runJdkJarHellCheck() throws IOException {
407
423
getRuntimeConfiguration (),
408
424
getProject ().getConfigurations ().getByName (CompileOnlyResolvePlugin .RESOLVEABLE_COMPILE_ONLY_CONFIGURATION_NAME )
409
425
);
410
-
411
426
spec .getMainClass ().set (JDK_JAR_HELL_MAIN_CLASS );
412
- spec .args (getJarExpandDir () );
427
+ spec .args (jars );
413
428
spec .setIgnoreExitValue (true );
414
429
if (javaHome != null ) {
415
430
spec .setExecutable (javaHome + "/bin/java" );
0 commit comments