Skip to content

Commit

Permalink
Do not record Maven compile scope in dependency groups (#1003)
Browse files Browse the repository at this point in the history
We should only record non-default dependency groups. 

For Maven, `compile` is the default scope so it should not be recorded.

https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#dependency-scope
  • Loading branch information
cuixq committed May 30, 2024
1 parent d4657bf commit e94c6b5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
6 changes: 6 additions & 0 deletions pkg/lockfile/fixtures/maven/with-scope.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
<project>
<dependencies>
<dependency>
<groupId>abc</groupId>
<artifactId>xyz</artifactId>
<version>1.2.3</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
10 changes: 6 additions & 4 deletions pkg/lockfile/parse-maven-lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,9 @@ func (e MavenLockExtractor) Extract(f DepFile) ([]PackageDetails, error) {
Ecosystem: MavenEcosystem,
CompareAs: MavenEcosystem,
}
if strings.TrimSpace(lockPackage.Scope) != "" {
pkgDetails.DepGroups = append(pkgDetails.DepGroups, lockPackage.Scope)
if scope := strings.TrimSpace(lockPackage.Scope); scope != "" && scope != "compile" {
// Only append non-default scope (compile is the default scope).
pkgDetails.DepGroups = append(pkgDetails.DepGroups, scope)
}
details[finalName] = pkgDetails
}
Expand All @@ -145,8 +146,9 @@ func (e MavenLockExtractor) Extract(f DepFile) ([]PackageDetails, error) {
Ecosystem: MavenEcosystem,
CompareAs: MavenEcosystem,
}
if strings.TrimSpace(lockPackage.Scope) != "" {
pkgDetails.DepGroups = append(pkgDetails.DepGroups, lockPackage.Scope)
if scope := strings.TrimSpace(lockPackage.Scope); scope != "" && scope != "compile" {
// Only append non-default scope (compile is the default scope).
pkgDetails.DepGroups = append(pkgDetails.DepGroups, scope)
}
details[finalName] = pkgDetails
}
Expand Down
6 changes: 6 additions & 0 deletions pkg/lockfile/parse-maven-lock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,12 @@ func TestParseMavenLock_WithScope(t *testing.T) {
}

expectPackages(t, packages, []lockfile.PackageDetails{
{
Name: "abc:xyz",
Version: "1.2.3",
Ecosystem: lockfile.MavenEcosystem,
CompareAs: lockfile.MavenEcosystem,
},
{
Name: "junit:junit",
Version: "4.12",
Expand Down

0 comments on commit e94c6b5

Please sign in to comment.