Skip to content
Open
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
27 changes: 16 additions & 11 deletions src/main/groovy/com/github/jk1/license/reader/PomReader.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import com.github.jk1.license.PomData
import com.github.jk1.license.PomDeveloper
import com.github.jk1.license.PomOrganization
import com.github.jk1.license.task.ReportTask
import com.github.jk1.license.util.CachingArtifactResolver
import com.github.jk1.license.util.CachingPomResolver
import com.github.jk1.license.util.Files
import groovy.xml.XmlSlurper
import groovy.xml.slurpersupport.GPathResult
Expand All @@ -39,14 +39,14 @@ class PomReader {
private Logger LOGGER = Logging.getLogger(ReportTask.class)

private LicenseReportExtension config
private CachingArtifactResolver resolver
private CachingPomResolver resolver

PomReader(LicenseReportExtension config) {
this.config = config
}

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

Expand All @@ -67,7 +67,7 @@ class PomReader {
}

PomData readPomData(Project project, ResolvedArtifactResult artifact) {
resolver = new CachingArtifactResolver(project)
resolver = new CachingPomResolver(project)
GPathResult pomContent = findAndSlurpPom(artifact.file, null)
return readPomFile(pomContent)
}
Expand Down Expand Up @@ -133,23 +133,28 @@ class PomReader {
}

private GPathResult fetchRemoteArtifactPom(ResolvedArtifact artifact) {
Collection<ResolvedArtifact> artifacts = fetchRemoteArtifactPoms(artifact.moduleVersion.id.group,
Collection<File> artifacts = fetchRemoteArtifactPoms(artifact.moduleVersion.id.group,
artifact.moduleVersion.id.name, artifact.moduleVersion.id.version)

return artifacts.collect {
try {
findAndSlurpPom(it.file, artifact)
findAndSlurpPom(it, artifact)
} catch (Exception e) {
LOGGER.warn("Error slurping pom from $it.file", e)
LOGGER.warn("Error slurping pom from $it", e)
null
}
}.find {
it != null
}
}

private Collection<ResolvedArtifact> fetchRemoteArtifactPoms(String group, String name, String version) {
String pomId = "${group.trim()}:${name.trim()}:${version.trim()}@pom"
private Collection<File> fetchRemoteArtifactPoms(String group, String name, String version) {
Map<String, String> pomId = [
"group" : group,
"name" : name,
"version": version
]

LOGGER.debug("Fetch: $pomId")
try {
resolver.resolveArtifacts(pomId)
Expand Down Expand Up @@ -177,10 +182,10 @@ class PomReader {
String artifactId = parentContent.artifactId.text()
String version = parentContent.version.text()

Collection<ResolvedArtifact> parentArtifacts = fetchRemoteArtifactPoms(groupId, artifactId, version)
Collection<File> parentArtifacts = fetchRemoteArtifactPoms(groupId, artifactId, version)

if (parentArtifacts) {
(parentArtifacts*.file as Set).each { File file ->
(parentArtifacts as Set).each { File file ->
LOGGER.debug("Processing parent POM file: $file")
GPathResult childPomGPath = slurpPomItself(file)

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2018 Evgeny Naumenko <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.jk1.license.util

import org.gradle.api.Project
import org.gradle.api.logging.Logger
import org.gradle.api.logging.Logging
import org.gradle.maven.MavenModule
import org.gradle.maven.MavenPomArtifact

class CachingPomResolver {

private static Logger LOGGER = Logging.getLogger(CachingPomResolver.class)
private Map<Map<String, String>, Collection<File>> cache = new HashMap<>()
private Project project

CachingPomResolver(Project project) {
this.project = project
}

Collection<File> resolveArtifacts(Map<String, String> spec) {
Map<String, String> copy = new HashMap<String, String>()
spec.each { copy.put(it.key.trim(), it.value.trim()) }
if (!cache.containsKey(copy)) {
cache.put(copy, doResolveArtifact(copy))
}
return cache.get(copy)
}

private Collection<File> doResolveArtifact(Object spec) {
try {
return project
.dependencies
.createArtifactResolutionQuery()
.forModule(spec.group, spec.name, spec.version)
.withArtifacts(MavenModule, MavenPomArtifact)
.execute()
.resolvedComponents
.collectMany { it.getArtifacts(MavenPomArtifact) }
*.file
} catch (Throwable ignored) {
LOGGER.info("Could not resolve $spec.group:$spec.name:$spec.version. It will be skipped.")
return null
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class ProjectReaderFuncSpec extends AbstractGradleRunnerFunctionalSpec {
buildFile << """
plugins {
id 'com.github.jk1.dependency-license-report'
id 'org.openjfx.javafxplugin' version '0.1.0'
}
configurations {
forTesting
Expand Down Expand Up @@ -268,6 +269,14 @@ class ProjectReaderFuncSpec extends AbstractGradleRunnerFunctionalSpec {
forTesting "org.springframework:spring-tx:3.2.3.RELEASE"
forTesting "org.ehcache:ehcache:3.3.1"
forTesting "org.apache.commons:commons-lang3:3.7"
forTesting("org.openjfx:javafx-base:22.0.1") {
attributes {
attribute(Category.CATEGORY_ATTRIBUTE, objects.named(Category, Category.LIBRARY))
attribute(Usage.USAGE_ATTRIBUTE, objects.named(Usage, Usage.JAVA_RUNTIME))
attribute(OperatingSystemFamily.OPERATING_SYSTEM_ATTRIBUTE, objects.named(OperatingSystemFamily, OperatingSystemFamily.LINUX))
attribute(MachineArchitecture.ARCHITECTURE_ATTRIBUTE, objects.named(MachineArchitecture, MachineArchitecture.X86_64))
}
}
}
"""

Expand All @@ -281,7 +290,7 @@ class ProjectReaderFuncSpec extends AbstractGradleRunnerFunctionalSpec {
then:
runResult.task(":generateLicenseReport").outcome == TaskOutcome.SUCCESS

configurationsString == prettyPrintJson(jsonSlurper.parse("""[
def expected = prettyPrintJson(jsonSlurper.parse("""[
{
"dependencies": [
{
Expand Down Expand Up @@ -480,6 +489,43 @@ class ProjectReaderFuncSpec extends AbstractGradleRunnerFunctionalSpec {
"empty": false,
"name": "ehcache"
},
{
"group": "org.openjfx",
"manifests": [
{
"licenseUrl": null,
"vendor": null,
"hasPackagedLicense": false,
"version": null,
"license": null,
"description": null,
"url": null,
"name": null
}
],
"hasArtifactFile": true,
"version": "22.0.1",
"poms": [
{
"inceptionYear": "",
"projectUrl": "",
"description": "",
"name": "",
"organization": null,
"licenses": [
{
"url": "https://openjdk.java.net/legal/gplv2+ce.html",
"name": "GPLv2+CE"
}
]
}
],
"licenseFiles": [

],
"empty": false,
"name": "javafx-base"
},
{
"group": "org.slf4j",
"manifests": [
Expand Down Expand Up @@ -683,6 +729,7 @@ class ProjectReaderFuncSpec extends AbstractGradleRunnerFunctionalSpec {
"name": "forTesting"
}
]""".toCharArray()))
configurationsString == expected
}


Expand Down