Skip to content

Commit e15af9f

Browse files
authored
Merge pull request #544 from jeremylong/fix/avoid-deprecated-tools
fix: avoid using deprecated tools section
2 parents 82a5e0d + 2d578d4 commit e15af9f

File tree

3 files changed

+93
-6
lines changed

3 files changed

+93
-6
lines changed

build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ dependencies {
3838

3939
tasks.withType<Test> {
4040
useJUnitPlatform()
41+
maxParallelForks = (Runtime.getRuntime().availableProcessors() / 2).coerceAtLeast(1)
4142
}
4243

4344
tasks.withType<JavaCompile>().configureEach {

src/main/java/org/cyclonedx/gradle/SbomBuilder.java

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import org.cyclonedx.model.Metadata;
5151
import org.cyclonedx.model.Property;
5252
import org.cyclonedx.model.Tool;
53+
import org.cyclonedx.model.metadata.ToolInformation;
5354
import org.cyclonedx.util.BomUtils;
5455
import org.gradle.api.logging.Logger;
5556

@@ -72,7 +73,7 @@ class SbomBuilder {
7273

7374
SbomBuilder(final Logger logger, final CycloneDxTask task) {
7475
this.logger = logger;
75-
this.version = CycloneDxUtils.DEFAULT_SCHEMA_VERSION;
76+
this.version = CycloneDxUtils.schemaVersion(task.getSchemaVersion().get());
7677
this.artifactHashes = new HashMap<>();
7778
this.mavenHelper = new MavenHelper(logger, task.getIncludeLicenseText().get());
7879
this.task = task;
@@ -125,11 +126,23 @@ private Metadata buildMetadata(final SbomComponent parentComponent) {
125126

126127
final Properties pluginProperties = readPluginProperties();
127128
if (!pluginProperties.isEmpty()) {
128-
final Tool tool = new Tool();
129-
tool.setVendor(pluginProperties.getProperty("vendor"));
130-
tool.setName(pluginProperties.getProperty("name"));
131-
tool.setVersion(pluginProperties.getProperty("version"));
132-
metadata.addTool(tool);
129+
// if schema version is 1.5 or higher use tools instead of tool
130+
if (version.compareTo(Version.VERSION_15) >= 0) {
131+
final Component component = new Component();
132+
component.setType(Component.Type.APPLICATION);
133+
component.setAuthor(pluginProperties.getProperty("vendor"));
134+
component.setName(pluginProperties.getProperty("name"));
135+
component.setVersion(pluginProperties.getProperty("version"));
136+
final ToolInformation tool = new ToolInformation();
137+
tool.setComponents(Collections.singletonList(component));
138+
metadata.setToolChoice(tool);
139+
} else {
140+
final Tool tool = new Tool();
141+
tool.setVendor(pluginProperties.getProperty("vendor"));
142+
tool.setName(pluginProperties.getProperty("name"));
143+
tool.setVersion(pluginProperties.getProperty("version"));
144+
metadata.addTool(tool);
145+
}
133146
}
134147

135148
return metadata;

src/test/groovy/org/cyclonedx/gradle/PluginConfigurationSpec.groovy

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import com.fasterxml.jackson.databind.ObjectMapper
44
import org.cyclonedx.gradle.utils.CycloneDxUtils
55
import org.cyclonedx.model.Bom
66
import org.cyclonedx.model.Component
7+
import org.cyclonedx.model.Tool
78
import org.gradle.testkit.runner.GradleRunner
89
import org.gradle.testkit.runner.TaskOutcome
910
import spock.lang.Specification
@@ -619,4 +620,76 @@ class PluginConfigurationSpec extends Specification {
619620
File jsonBom = new File(testDir, "build/reports/bom.json")
620621
assert !jsonBom.text.contains("\"id\" : \"Apache-2.0\"")
621622
}
623+
624+
def "should not use depecrated tool section if schema is 1.5 or higher"() {
625+
given:
626+
File testDir = TestUtils.createFromString("""
627+
plugins {
628+
id 'org.cyclonedx.bom'
629+
id 'java'
630+
}
631+
repositories {
632+
mavenCentral()
633+
}
634+
group = 'com.example'
635+
version = '1.0.0'
636+
cyclonedxBom {
637+
schemaVersion = "1.6"
638+
}
639+
dependencies {
640+
implementation group: 'org.apache.logging.log4j', name: 'log4j-core', version:'2.15.0'
641+
}""", "rootProject.name = 'hello-world'")
642+
643+
when:
644+
def result = GradleRunner.create()
645+
.withProjectDir(testDir)
646+
.withArguments("cyclonedxBom", "--configuration-cache")
647+
.withPluginClasspath()
648+
.build()
649+
650+
then:
651+
result.task(":cyclonedxBom").outcome == TaskOutcome.SUCCESS
652+
File jsonBom = new File(testDir, "build/reports/bom.json")
653+
Bom bom = new ObjectMapper().readValue(jsonBom, Bom.class)
654+
assert bom.getMetadata().getToolChoice().getComponents().size() == 1
655+
Component cycloneDxTool = bom.getMetadata().getToolChoice().getComponents().get(0)
656+
assert cycloneDxTool.getName() == "cyclonedx-gradle-plugin"
657+
assert cycloneDxTool.getAuthor() == "CycloneDX"
658+
}
659+
660+
def "should use legacy tools section if schema is below 1.5"() {
661+
given:
662+
File testDir = TestUtils.createFromString("""
663+
plugins {
664+
id 'org.cyclonedx.bom'
665+
id 'java'
666+
}
667+
repositories {
668+
mavenCentral()
669+
}
670+
group = 'com.example'
671+
version = '1.0.0'
672+
cyclonedxBom {
673+
schemaVersion = "1.4"
674+
}
675+
dependencies {
676+
implementation group: 'org.apache.logging.log4j', name: 'log4j-core', version:'2.15.0'
677+
}""", "rootProject.name = 'hello-world'")
678+
679+
when:
680+
def result = GradleRunner.create()
681+
.withProjectDir(testDir)
682+
.withArguments("cyclonedxBom", "--configuration-cache")
683+
.withPluginClasspath()
684+
.build()
685+
686+
then:
687+
result.task(":cyclonedxBom").outcome == TaskOutcome.SUCCESS
688+
File jsonBom = new File(testDir, "build/reports/bom.json")
689+
Bom bom = new ObjectMapper().readValue(jsonBom, Bom.class)
690+
assert bom.getMetadata().getTools().size() == 1
691+
Tool tool = bom.getMetadata().getTools().get(0);
692+
assert tool.getName() == "cyclonedx-gradle-plugin"
693+
assert tool.getVendor() == "CycloneDX"
694+
}
622695
}

0 commit comments

Comments
 (0)