Skip to content

Commit 0bef0d0

Browse files
fupganghboutemy
authored andcommitted
fixes #382
License information with empty name and url (in pom.xml) are ignored. Signed-off-by: fupgang <[email protected]>
1 parent cf7d300 commit 0bef0d0

File tree

3 files changed

+115
-2
lines changed

3 files changed

+115
-2
lines changed

src/main/java/org/cyclonedx/maven/DefaultModelConverter.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,8 @@ private void extractComponentMetadata(MavenProject project, Component component,
222222
component.setDescription(project.getDescription());
223223
}
224224
if (component.getLicenseChoice() == null || component.getLicenseChoice().getLicenses() == null || component.getLicenseChoice().getLicenses().isEmpty()) {
225-
// If we don't already have license information, retrieve it.
226-
if (project.getLicenses() != null) {
225+
// If we don't already have license information, retrieve it, as long as it is not empty.
226+
if (project.getLicenses() != null && project.getLicenses().stream().anyMatch(l -> !isLicenseBlank(l))) {
227227
component.setLicenseChoice(resolveMavenLicenses(project.getLicenses(), schemaVersion, includeLicenseText));
228228
}
229229
}
@@ -425,4 +425,9 @@ private Component.Type resolveProjectType(String projectType) {
425425
private static boolean isURLBlank(String url) {
426426
return url == null || url.isEmpty() || url.trim().length() == 0;
427427
}
428+
429+
private static boolean isLicenseBlank(org.apache.maven.model.License license) {
430+
return (license.getName() == null || license.getName().isEmpty() || license.getName().trim().length() == 0)
431+
&& (license.getUrl() == null || license.getUrl().isEmpty() || license.getUrl().trim().length() == 0);
432+
}
428433
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package org.cyclonedx.maven;
2+
3+
import io.takari.maven.testing.executor.MavenRuntime.MavenRuntimeBuilder;
4+
import io.takari.maven.testing.executor.MavenVersions;
5+
import io.takari.maven.testing.executor.junit.MavenJUnitTestRunner;
6+
import org.junit.Test;
7+
import org.junit.runner.RunWith;
8+
9+
import java.io.File;
10+
import java.io.IOException;
11+
12+
import static io.takari.maven.testing.TestResources.assertFilesPresent;
13+
import static org.junit.Assert.assertFalse;
14+
15+
/**
16+
* Test for <a href="https://github.com/CycloneDX/cyclonedx-maven-plugin/issues/382">issue #382</a>:
17+
* Plugin does not gracefully handle present, but empty license data
18+
*/
19+
@RunWith(MavenJUnitTestRunner.class)
20+
@MavenVersions({"3.6.3"})
21+
public class Issue382Test extends BaseMavenVerifier {
22+
23+
public Issue382Test(MavenRuntimeBuilder runtimeBuilder) throws Exception {
24+
super(runtimeBuilder);
25+
}
26+
27+
@Test
28+
public void test() throws Exception {
29+
File projDir = resources.getBasedir("issue-382");
30+
31+
verifier
32+
.forProject(projDir)
33+
.withCliOption("-Dcurrent.version=" + getCurrentVersion()) // inject cyclonedx-maven-plugin version
34+
.withCliOption("-X") // debug
35+
.withCliOption("-B")
36+
.execute("clean", "verify")
37+
.assertErrorFreeLog();
38+
39+
assertFileNotContains(projDir, "target/bom.xml", "The BOM does not conform to the CycloneDX BOM standard");
40+
}
41+
42+
private static void assertFileNotContains(File basedir, String expectedFile, String expectedContent) throws IOException {
43+
assertFilesPresent(basedir, expectedFile);
44+
String bomContents = fileRead(new File(basedir, expectedFile), true);
45+
assertFalse(String.format("%s contains %s", expectedFile, expectedContent), bomContents.contains(expectedContent));
46+
}
47+
}

src/test/resources/issue-382/pom.xml

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
5+
6+
<modelVersion>4.0.0</modelVersion>
7+
8+
<groupId>com.example</groupId>
9+
<artifactId>issue-382</artifactId>
10+
<packaging>jar</packaging>
11+
<version>1.0.0</version>
12+
13+
<name>Issue-64</name>
14+
15+
<licenses>
16+
<license>
17+
<name/>
18+
<url/>
19+
<distribution/>
20+
</license>
21+
</licenses>
22+
23+
<properties>
24+
<maven.compiler.source>8</maven.compiler.source>
25+
<maven.compiler.target>8</maven.compiler.target>
26+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
27+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
28+
</properties>
29+
30+
<dependencies>
31+
<dependency> <!-- has empty license information -->
32+
<groupId>com.aliyun</groupId>
33+
<artifactId>aliyun-java-sdk-ram</artifactId>
34+
<version>3.1.0</version>
35+
</dependency>
36+
</dependencies>
37+
38+
<build>
39+
<plugins>
40+
<plugin>
41+
<groupId>org.cyclonedx</groupId>
42+
<artifactId>cyclonedx-maven-plugin</artifactId>
43+
<version>${current.version}</version>
44+
<executions>
45+
<execution>
46+
<phase>verify</phase>
47+
<goals>
48+
<goal>makeAggregateBom</goal>
49+
</goals>
50+
</execution>
51+
</executions>
52+
<configuration>
53+
<projectType>library</projectType>
54+
<schemaVersion>1.6</schemaVersion>
55+
<includeLicenseText>true</includeLicenseText>
56+
</configuration>
57+
</plugin>
58+
</plugins>
59+
</build>
60+
61+
</project>

0 commit comments

Comments
 (0)