Skip to content

Commit 57f54a1

Browse files
committed
feat: dependency inherit parent
1 parent 15c5be0 commit 57f54a1

File tree

3 files changed

+39
-7
lines changed

3 files changed

+39
-7
lines changed

opensca/sca/java/mvn.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ func inheritModules(poms []*Pom) {
183183
type getPomFunc func(dep PomDependency, repos ...[]string) *Pom
184184

185185
// inheritPom 继承pom所需内容
186-
func inheritPom(pom *Pom, inheritDependencies bool, getpom getPomFunc) {
186+
func inheritPom(pom *Pom, getpom getPomFunc) {
187187

188188
// 记录统计过的parent 避免pom循环引用
189189
parentSet := map[string]bool{}
@@ -218,9 +218,7 @@ func inheritPom(pom *Pom, inheritDependencies bool, getpom getPomFunc) {
218218
pom.DependencyManagement = append(pom.DependencyManagement, parentPom.DependencyManagement...)
219219

220220
// 继承dependencies
221-
if inheritDependencies {
222-
pom.Dependencies = append(pom.Dependencies, parentPom.Dependencies...)
223-
}
221+
pom.Dependencies = append(pom.Dependencies, parentPom.Dependencies...)
224222

225223
// 继承repo&mirror
226224
pom.Repositories = append(pom.Repositories, parentPom.Repositories...)
@@ -294,7 +292,7 @@ func parsePom(ctx context.Context, pom *Pom, getpom getPomFunc) *model.DepGraph
294292
pom.Update(&pom.PomDependency)
295293

296294
// 继承pom
297-
inheritPom(pom, true, getpom)
295+
inheritPom(pom, getpom)
298296

299297
// 记录在根pom的dependencyManagement中非import组件信息
300298
rootPomManagement := map[string]*PomDependency{}
@@ -406,8 +404,8 @@ func parsePom(ctx context.Context, pom *Pom, getpom getPomFunc) *model.DepGraph
406404
subpom.PomDependency = *dep
407405
// 继承根pom的exclusion
408406
subpom.Exclusions = append(subpom.Exclusions, np.Exclusions...)
409-
// 子依赖不继承parent的依赖项
410-
inheritPom(subpom, false, getpom)
407+
// 依赖继承parent
408+
inheritPom(subpom, getpom)
411409
sub.Expand = subpom
412410
}
413411

test/java/15/pom.xml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>my.foo</groupId>
8+
<version>1.0</version>
9+
<artifactId>demo</artifactId>
10+
11+
<dependencies>
12+
13+
<dependency>
14+
<groupId>com.fasterxml.jackson.datatype</groupId>
15+
<artifactId>jackson-datatype-jsr310</artifactId>
16+
<version>2.17.0</version>
17+
</dependency>
18+
19+
</dependencies>
20+
21+
</project>

test/java/java_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,19 @@ var cases = []tool.TaskCase{
161161
),
162162
),
163163
)},
164+
165+
// 直接依赖继承parent
166+
{Path: "15", Result: tool.Dep("", "",
167+
tool.Dep3("my.foo", "demo", "1.0",
168+
tool.Dep3("com.fasterxml.jackson.datatype", "jackson-datatype-jsr310", "2.17.0",
169+
tool.Dep3("com.fasterxml.jackson.core", "jackson-annotations", "2.17.0"),
170+
tool.Dep3("com.fasterxml.jackson.core", "jackson-core", "2.17.0"),
171+
tool.Dep3("com.fasterxml.jackson.core", "jackson-databind", "2.17.0",
172+
tool.Dep3("net.bytebuddy", "byte-buddy", "1.14.9"),
173+
),
174+
),
175+
),
176+
)},
164177
}
165178

166179
func Test_JavaWithStatic(t *testing.T) {

0 commit comments

Comments
 (0)