Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed & refactored loading pom.properties #727

Merged
merged 2 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/repotests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ jobs:
curl --output-dir standalone-jar-files -LO https://repo1.maven.org/maven2/com/fasterxml/jackson/core/jackson-core/2.16.0/jackson-core-2.16.0.jar
curl --output-dir standalone-jar-files -LO https://repo1.maven.org/maven2/junit/junit/4.13.2/junit-4.13.2.jar
curl --output-dir standalone-jar-files -LO https://repo1.maven.org/maven2/wsdl4j/wsdl4j/1.6.3/wsdl4j-1.6.3.jar
curl --output-dir standalone-jar-files -LO https://repo1.maven.org/maven2/org/apache/maven/maven-core/3.9.2/maven-core-3.9.2.jar
FETCH_LICENSE=true bin/cdxgen.js -p standalone-jar-files -o bomresults/bom-standalone-jar-files.json --validate
shell: bash
- name: repotests 1.4
Expand Down
73 changes: 44 additions & 29 deletions utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -6538,6 +6538,44 @@ export const encodeForPurl = (s) => {
: s;
};

/**
* Method to get pom properties from maven directory
*
* @param {string} mavenDir Path to maven directory
*
* @return array with pom properties
*/
export const getPomPropertiesFromMavenDir = function (mavenDir) {
let pomProperties = {};
if (existsSync(mavenDir) && lstatSync(mavenDir).isDirectory()) {
let mavenDirEntries = readdirSync(mavenDir, { withFileTypes: true });
mavenDirEntries.forEach((mavenDirEntry) => {
if (mavenDirEntry.isDirectory()) {
let groupDirEntries = readdirSync(
join(mavenDirEntry.path, mavenDirEntry.name),
{ withFileTypes: true }
);
groupDirEntries.forEach((groupDirEntry) => {
if (groupDirEntry.isDirectory()) {
let pomPropertiesFile = join(
groupDirEntry.path,
groupDirEntry.name,
"pom.properties"
);
if (existsSync(pomPropertiesFile)) {
const pomPropertiesString = readFileSync(pomPropertiesFile, {
encoding: "utf-8"
});
pomProperties = parsePomProperties(pomPropertiesString);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need a break or return here since this is inside a loop?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately a return/break does not work with forEach. But usually the jar should anyway contain one directory on each level inside the META-INF/maven directory. And as I learned optionally the extension.xml. Therefore I thought we can keep the implementation that way, because there will be no performance loss.

}
}
});
}
});
}
return pomProperties;
};

/**
* Method to extract a war or ear file
*
Expand Down Expand Up @@ -6636,38 +6674,15 @@ export const extractJarArchive = function (
if (jarResult.status !== 0) {
console.error(jarResult.stdout, jarResult.stderr);
} else {
let group = "",
name = "",
version = "",
confidence = 1,
technique = "manifest-analysis";
// When maven descriptor is available take group, name and version from pom.properties
// META-INF/maven/${groupId}/${artifactId}/pom.properties
// see https://maven.apache.org/shared/maven-archiver/index.html
if (existsSync(mavenDir)) {
let groupDir = readdirSync(mavenDir);
if (groupDir && groupDir.length) {
let artifactDir = readdirSync(join(mavenDir, groupDir[0]));
if (artifactDir && artifactDir.length) {
let pomPropertiesFile = join(
mavenDir,
groupDir[0],
artifactDir[0],
"pom.properties"
);
if (existsSync(pomPropertiesFile)) {
const pomProperties = parsePomProperties(
readFileSync(pomPropertiesFile, {
encoding: "utf-8"
})
);
group = pomProperties["groupId"];
name = pomProperties["artifactId"];
version = pomProperties["version"];
}
}
}
}
const pomProperties = getPomPropertiesFromMavenDir(mavenDir);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since pomProperties can be an empty object, do we need a condition to skip?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apologies, it might be right below this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it is an empty object (e.g. wsdl4j-1.6.3.jar) group, name and version will be undefined, but this case is handled later.

let group = pomProperties["groupId"],
name = pomProperties["artifactId"],
version = pomProperties["version"],
confidence = 1,
technique = "manifest-analysis";
if ((!group || !name || !version) && existsSync(manifestFile)) {
confidence = 0.8;
const jarMetadata = parseJarManifest(
Expand Down
Loading