Skip to content

Wrong counting of licenses in groovy #6

@vietz-dev

Description

@vietz-dev

Hello

currently i am planning to include this library into my project. Unfortunately I have some issues using this one.

The issue i want to present here is the following:

in the LicensesTask.groovy file starting from the line 197 you can see the addLicensesFromPom() method.

The method looks like this:

protected void addLicensesFromPom(File artifactFile, String artifactName,
        String group) {
        String pomFileName = artifactFile.getName().replaceFirst(FILE_EXTENSION,
            ".pom")

        // Search for pom file. When the artifact is cached in gradle cache, the
        // pom file will be stored in a hashed directory.
        FileTree tree = project.fileTree(
            dir: artifactFile.parentFile.parentFile,
            include: ["**/${pomFileName}", pomFileName])
        for (File pomFile : tree) {
            def rootNode = new XmlSlurper().parse(pomFile)
            if (rootNode.licenses.size() == 0) continue

            String licenseKey = "${group}:${artifactName}"
            if (rootNode.licenses.size() > 1) {
                rootNode.licenses.license.each { node ->
                    String nodeName = node.name
                    String nodeUrl = node.url
                    appendLicense("${licenseKey} ${nodeName}", nodeUrl.length(),
                        nodeUrl)
                }
            } else {
                String nodeUrl = rootNode.licenses.license.url
                appendLicense(licenseKey, nodeUrl.length(), nodeUrl)
            }
        }
    }

As you can see it iterates each tree it can find and there is the if statement in line 212.
As far as I understand this line checks if there are multiple licenses inside of the *.pom file.
Unfortunately checking rootNode.licenses.size() counts how many <licenses>...</licenses>-Tags it can find.
Declaring multiple <licenses>...</licenses>-Tags causes an Error if you want to build an for example maven project.

The correct way to declare multiple licenses in your pom.xml is:

<licenses>
	<!--FIRST LICENSE-->
	<license>
		<name>First-License</name>
		<url>https://first-license-url.com/</url>
		<distribution>repo</distribution>
	</license>
	
	<!--SECOND LICENSE-->
	<license>
		<name>Second-License</name>
		<url>https://second-license-url.com/</url>
		<distribution>repo</distribution>
	</license>
	
	<!--THIRD LICENSE-->
	<license>
		<name>Third-License</name>
		<url>https://third-license-url.com/</url>
		<distribution>repo</distribution>
	</license>
</licenes>

So instead checking how many <licenses>...</licenses>-Tags there are you should check how many <license>...</license>-Tags are there.

So it should be:

if (rootNode.licenses.license.size() > 1) {

instead of

if (rootNode.licenses.size() > 1) {

If I'am wrong please tell me.
I hope i could help.
Thanks in advance.

Metadata

Metadata

Assignees

No one assigned

    Labels

    oss-licensesIssues related to com.google.android.gms:oss-licenses-plugin

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions