Skip to content

Commit 54ae54a

Browse files
Create sub directories for ThirdPartyAudit dependency metadata (opensearch-project#16844)
* Extract jars to sub dirs during thirdPartyAudit task. Signed-off-by: Finn Carroll <carrofin@amazon.com> * Change regex to split on '-'/'.'. Ignore version. Signed-off-by: Finn Carroll <carrofin@amazon.com> * Split on .jar for sub folder prefix. Signed-off-by: Finn Carroll <carrofin@amazon.com> --------- Signed-off-by: Finn Carroll <carrofin@amazon.com>
1 parent 6b41e4f commit 54ae54a

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed

buildSrc/src/main/java/org/opensearch/gradle/precommit/ThirdPartyAuditTask.java

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,7 @@ public Set<File> getJarsToScan() {
229229
@TaskAction
230230
public void runThirdPartyAudit() throws IOException {
231231
Set<File> jars = getJarsToScan();
232-
233-
extractJars(jars);
232+
Set<File> extractedJars = extractJars(jars);
234233

235234
final String forbiddenApisOutput = runForbiddenAPIsCli();
236235

@@ -248,7 +247,7 @@ public void runThirdPartyAudit() throws IOException {
248247

249248
Set<String> jdkJarHellClasses = null;
250249
if (this.jarHellEnabled) {
251-
jdkJarHellClasses = runJdkJarHellCheck();
250+
jdkJarHellClasses = runJdkJarHellCheck(extractedJars);
252251
}
253252

254253
if (missingClassExcludes != null) {
@@ -301,16 +300,26 @@ private void logForbiddenAPIsOutput(String forbiddenApisOutput) {
301300
getLogger().error("Forbidden APIs output:\n{}==end of forbidden APIs==", forbiddenApisOutput);
302301
}
303302

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<>();
305311
File jarExpandDir = getJarExpandDir();
306312
// We need to clean up to make sure old dependencies don't linger
307313
getProject().delete(jarExpandDir);
308314

309315
jars.forEach(jar -> {
316+
String jarPrefix = jar.getName().replace(".jar", "");
317+
File jarSubDir = new File(jarExpandDir, jarPrefix);
318+
extractedJars.add(jarSubDir);
310319
FileTree jarFiles = getProject().zipTree(jar);
311320
getProject().copy(spec -> {
312321
spec.from(jarFiles);
313-
spec.into(jarExpandDir);
322+
spec.into(jarSubDir);
314323
// exclude classes from multi release jars
315324
spec.exclude("META-INF/versions/**");
316325
});
@@ -329,14 +338,16 @@ private void extractJars(Set<File> jars) {
329338
Integer.parseInt(targetCompatibility.get().getMajorVersion())
330339
).forEach(majorVersion -> getProject().copy(spec -> {
331340
spec.from(getProject().zipTree(jar));
332-
spec.into(jarExpandDir);
341+
spec.into(jarSubDir);
333342
String metaInfPrefix = "META-INF/versions/" + majorVersion;
334343
spec.include(metaInfPrefix + "/**");
335344
// Drop the version specific prefix
336345
spec.eachFile(details -> details.setPath(details.getPath().replace(metaInfPrefix, "")));
337346
spec.setIncludeEmptyDirs(false);
338347
}));
339348
});
349+
350+
return extractedJars;
340351
}
341352

342353
private void assertNoJarHell(Set<String> jdkJarHellClasses) {
@@ -398,7 +409,12 @@ private String runForbiddenAPIsCli() throws IOException {
398409
return forbiddenApisOutput;
399410
}
400411

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 {
402418
ByteArrayOutputStream standardOut = new ByteArrayOutputStream();
403419
InjectedExecOps execOps = getProject().getObjects().newInstance(InjectedExecOps.class);
404420
ExecResult execResult = execOps.getExecOps().javaexec(spec -> {
@@ -407,9 +423,8 @@ private Set<String> runJdkJarHellCheck() throws IOException {
407423
getRuntimeConfiguration(),
408424
getProject().getConfigurations().getByName(CompileOnlyResolvePlugin.RESOLVEABLE_COMPILE_ONLY_CONFIGURATION_NAME)
409425
);
410-
411426
spec.getMainClass().set(JDK_JAR_HELL_MAIN_CLASS);
412-
spec.args(getJarExpandDir());
427+
spec.args(jars);
413428
spec.setIgnoreExitValue(true);
414429
if (javaHome != null) {
415430
spec.setExecutable(javaHome + "/bin/java");

0 commit comments

Comments
 (0)