Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

include and skip configurations with regex #383

Merged
merged 2 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 = ['.*']
}
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.getBomRef() == 'pkg:maven/org.apache.logging.log4j/[email protected]?type=jar'
}

}