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

#322 Pick the best matching pom.xml in the jar, instead of randomly choosing the first one. #323

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
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