Skip to content

Commit

Permalink
include and skip configurations with regex
Browse files Browse the repository at this point in the history
Signed-off-by: skhokhlov <me@skhlv.nyc>
  • Loading branch information
skhokhlov committed Jan 18, 2024
1 parent cab158d commit ded2da3
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 4 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/cyclonedx/gradle/CycloneDxTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

}

0 comments on commit ded2da3

Please sign in to comment.