Skip to content

Commit

Permalink
jk1#322 Pick the best matching pom.xml in the jar, instead of randoml…
Browse files Browse the repository at this point in the history
…y choosing the first one.
  • Loading branch information
AlexanderBartash committed Oct 10, 2024
1 parent 8da16c1 commit 0b3ebde
Showing 1 changed file with 30 additions and 14 deletions.
44 changes: 30 additions & 14 deletions src/main/groovy/com/github/jk1/license/reader/PomReader.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import org.xml.sax.SAXException
import java.util.zip.ZipEntry
import java.util.zip.ZipFile


class PomReader {
private Logger LOGGER = Logging.getLogger(ReportTask.class)

Expand All @@ -48,7 +47,7 @@ class PomReader {

PomData readPomData(Project project, ResolvedArtifact artifact) {
resolver = new CachingArtifactResolver(project)
GPathResult pomContent = findAndSlurpPom(artifact.file)
GPathResult pomContent = findAndSlurpPom(artifact.file, artifact)
boolean pomRepresentsArtifact = true
boolean pomHasLicense = true

Expand Down Expand Up @@ -76,11 +75,11 @@ class PomReader {

PomData readPomData(Project project, ResolvedArtifactResult artifact) {
resolver = new CachingArtifactResolver(project)
GPathResult pomContent = findAndSlurpPom(artifact.file)
GPathResult pomContent = findAndSlurpPom(artifact.file, null)
return readPomFile(pomContent)
}

private GPathResult findAndSlurpPom(File toSlurp) {
private GPathResult findAndSlurpPom(File toSlurp, ResolvedArtifact artifact) {
if (toSlurp.name == "pom.xml") {
LOGGER.debug("Slurping pom from pom.xml file: $toSlurp")
return slurpPomItself(toSlurp)
Expand All @@ -98,29 +97,46 @@ class PomReader {
case "zip":
case "jar":
LOGGER.debug("Processing pom from archive: $toSlurp")
return slurpFirstPomFromZip(toSlurp)
return slurpBestMatchPomFromZip(artifact)
}

LOGGER.debug("No idea how to process a pom from: $toSlurp")
return null
}

private GPathResult slurpFirstPomFromZip(File archiveToSearch) {
private GPathResult slurpBestMatchPomFromZip(ResolvedArtifact artifact) {
File archiveToSearch = artifact.file
ZipFile archive = new ZipFile(archiveToSearch, ZipFile.OPEN_READ)
ZipEntry pomEntry = archive.entries().toList().find { ZipEntry entry ->
List<ZipEntry> pomEntries = archive.entries().toList().<ZipEntry>findAll { ZipEntry entry ->
entry.name.endsWith("pom.xml") || entry.name.endsWith(".pom")
}
LOGGER.debug("Searching for POM file in $archiveToSearch -- found ${pomEntry?.name}")
if (!pomEntry) return null
LOGGER.debug("Searching for POM file in $archiveToSearch -- found ${pomEntries?.size()}")
if (!pomEntries) return null
try {
return createParser().parse(archive.getInputStream(pomEntry))
if (1 == pomEntries.size()) {
LOGGER.debug("Only one POM file was found in $archiveToSearch")
return createParser().parse(archive.getInputStream(zipEntry))
}

for (final ZipEntry zipEntry in pomEntries) {
final GPathResult pom = createParser().parse(archive.getInputStream(zipEntry))

if (areArtifactAndPomGroupAndArtifactIdEqual(artifact, pom)) {
LOGGER.debug("POM file in $archiveToSearch matched the artifact.")
return pom
} else {
LOGGER.debug("POM file in $archiveToSearch does not match the artifact, trying another one.")
}
}
} catch (SAXException e) {
LOGGER.warn("Error parsing $pomEntry.name in $archiveToSearch", e)
LOGGER.warn("Error parsing $pomEntries.name in $archiveToSearch", e)
return null
} catch (IOException e) {
LOGGER.warn("Error reading $pomEntry.name in $archiveToSearch", e)
LOGGER.warn("Error reading $pomEntries.name in $archiveToSearch", e)
return null
}

return null
}

private GPathResult fetchRemoteArtifactPom(ResolvedArtifact artifact) {
Expand All @@ -129,7 +145,7 @@ class PomReader {

return artifacts.collect {
try {
findAndSlurpPom(it.file)
findAndSlurpPom(it.file, artifact)
} catch (Exception e) {
LOGGER.warn("Error slurping pom from $it.file", e)
null
Expand Down Expand Up @@ -271,7 +287,7 @@ class PomReader {
private static boolean areArtifactAndPomGroupAndArtifactIdEqual(ResolvedArtifact artifact, GPathResult pom) {
if (artifact == null) return false
artifact.moduleVersion.id.group == tryReadGroupId(pom) &&
artifact.moduleVersion.id.name == pom.artifactId.text()
artifact.moduleVersion.id.name == pom.artifactId.text()
}

private static boolean hasLicense(GPathResult pom) {
Expand Down

0 comments on commit 0b3ebde

Please sign in to comment.