From ded2da341d598099f9734bd7da770ebfbd82ab46 Mon Sep 17 00:00:00 2001 From: skhokhlov Date: Thu, 18 Jan 2024 14:16:44 +0300 Subject: [PATCH] include and skip configurations with regex Signed-off-by: skhokhlov --- README.md | 4 +- .../org/cyclonedx/gradle/CycloneDxTask.java | 4 +- .../gradle/PluginConfigurationSpec.groovy | 71 +++++++++++++++++++ 3 files changed, 75 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 4d26af2d..75b33e1c 100644 --- a/README.md +++ b/README.md @@ -43,9 +43,9 @@ You can add the following configuration to `build.gradle` to control various opt ```groovy cyclonedxBom { - // includeConfigs is the list of configuration names to include when generating the BOM (leave empty to include every configuration) + // includeConfigs is the list of configuration names to include when generating the BOM (leave empty to include every configuration), regex is supported includeConfigs = ["runtimeClasspath"] - // skipConfigs is a list of configuration names to exclude when generating the BOM + // skipConfigs is a list of configuration names to exclude when generating the BOM, regex is supported skipConfigs = ["compileClasspath", "testCompileClasspath"] // skipProjects is a list of project names to exclude when generating the BOM skipProjects = [rootProject.name, "yourTestSubProject"] diff --git a/src/main/java/org/cyclonedx/gradle/CycloneDxTask.java b/src/main/java/org/cyclonedx/gradle/CycloneDxTask.java index f4ed2ec0..b8acbe00 100644 --- a/src/main/java/org/cyclonedx/gradle/CycloneDxTask.java +++ b/src/main/java/org/cyclonedx/gradle/CycloneDxTask.java @@ -613,11 +613,11 @@ private Component convertArtifact(ResolvedArtifact artifact, CycloneDxSchema.Ver } private boolean shouldIncludeConfiguration(Configuration configuration) { - return getIncludeConfigs().get().isEmpty() || getIncludeConfigs().get().contains(configuration.getName()); + return getIncludeConfigs().get().isEmpty() || getIncludeConfigs().get().stream().anyMatch(configuration.getName()::matches); } private boolean shouldSkipConfiguration(Configuration configuration) { - return getSkipConfigs().get().contains(configuration.getName()); + return getSkipConfigs().get().stream().anyMatch(configuration.getName()::matches); } private boolean shouldSkipProject(Project project) { diff --git a/src/test/groovy/org/cyclonedx/gradle/PluginConfigurationSpec.groovy b/src/test/groovy/org/cyclonedx/gradle/PluginConfigurationSpec.groovy index 93439b30..704cc19c 100644 --- a/src/test/groovy/org/cyclonedx/gradle/PluginConfigurationSpec.groovy +++ b/src/test/groovy/org/cyclonedx/gradle/PluginConfigurationSpec.groovy @@ -374,5 +374,76 @@ class PluginConfigurationSpec extends Specification { } + def "should skip configurations with regex"() { + given: + File testDir = TestUtils.createFromString(""" + plugins { + id 'org.cyclonedx.bom' + id 'java' + } + repositories { + mavenCentral() + } + group = 'com.example' + version = '1.0.0' + cyclonedxBom { + schemaVersion = '1.3' + skipConfigs = ['.*'] + } + dependencies { + implementation group: 'org.apache.logging.log4j', name: 'log4j-core', version:'2.15.0' + }""", "rootProject.name = 'hello-world'") + + when: + def result = GradleRunner.create() + .withProjectDir(testDir) + .withArguments("cyclonedxBom") + .withPluginClasspath() + .build() + + then: + result.task(":cyclonedxBom").outcome == TaskOutcome.SUCCESS + File jsonBom = new File(testDir, "build/reports/bom.json") + Bom bom = new ObjectMapper().readValue(jsonBom, Bom.class) + Component log4jCore = bom.getComponents().find(c -> c.name == 'log4j-core') + + assert log4jCore == null + } + + def "should include configurations with regex"() { + given: + File testDir = TestUtils.createFromString(""" + plugins { + id 'org.cyclonedx.bom' + id 'java' + } + repositories { + mavenCentral() + } + group = 'com.example' + version = '1.0.0' + cyclonedxBom { + schemaVersion = '1.3' + includeConfigs = ['implement.*'] + } + dependencies { + implementation group: 'org.apache.logging.log4j', name: 'log4j-core', version:'2.15.0' + }""", "rootProject.name = 'hello-world'") + + when: + def result = GradleRunner.create() + .withProjectDir(testDir) + .withArguments("cyclonedxBom") + .withPluginClasspath() + .build() + + then: + result.task(":cyclonedxBom").outcome == TaskOutcome.SUCCESS + File jsonBom = new File(testDir, "build/reports/bom.json") + Bom bom = new ObjectMapper().readValue(jsonBom, Bom.class) + Component log4jCore = bom.getComponents().find(c -> c.name == 'log4j-core') + + assert log4jCore == null + } }