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

fix(pom): recursive check all nested depManagements with import scope #285

Merged
Merged
Show file tree
Hide file tree
Changes from 5 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
7 changes: 6 additions & 1 deletion pkg/java/pom/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,9 +407,14 @@ func (p *parser) resolveDepManagement(props map[string]string, depManagement []p
if err != nil {
continue
}

// We need to recursively check all nested depManagements,
// so that we don't miss dependencies on nested depManagements with `Import` scope.
newProps := utils.MergeMaps(props, result.properties)
result.dependencyManagement = p.resolveDepManagement(newProps, result.dependencyManagement)
for k, dd := range result.dependencyManagement {
// Evaluate variables and overwrite dependencyManagement
result.dependencyManagement[k] = dd.Resolve(result.properties, nil, nil)
result.dependencyManagement[k] = dd.Resolve(newProps, nil, nil)
}
newDepManagement = p.mergeDependencyManagements(newDepManagement, result.dependencyManagement)
}
Expand Down
7 changes: 3 additions & 4 deletions pkg/java/pom/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -670,10 +670,9 @@ func TestPom_Parse(t *testing.T) {
License: "Apache 2.0",
},
{
ID: "org.example:example-api:1.7.30",
ID: "org.example:example-api:2.2.2",
Name: "org.example:example-api",
Version: "1.7.30",
License: "The Apache Software License, Version 2.0",
Version: "2.2.2",
Locations: types.Locations{
{
StartLine: 34,
Expand All @@ -686,7 +685,7 @@ func TestPom_Parse(t *testing.T) {
{
ID: "com.example:import:2.0.0",
DependsOn: []string{
"org.example:example-api:1.7.30",
"org.example:example-api:2.2.2",
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion pkg/java/pom/testdata/import-dependency-management/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<dependency>
<groupId>org.example</groupId>
<artifactId>example-dependency-management</artifactId>
<version>2.2.2</version>
<version>3.3.3</version>
<scope>import</scope>
<type>pom</type>
</dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>org.example</groupId>
<artifactId>example-dependency-management</artifactId>
<version>3.3.3</version>

<packaging>pom</packaging>
<name>Example API Dependency Management</name>
<description>The example API</description>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.example</groupId>
<artifactId>example-dependency-management2</artifactId>
<version>${project.version}</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.example</groupId>
<artifactId>example-dependency</artifactId>
<version>1.2.3</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>org.example</groupId>
<artifactId>example-dependency-management2</artifactId>
<version>3.3.3</version>

<packaging>pom</packaging>
<name>Example API Dependency Management</name>
<description>The example API</description>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.example</groupId>
<artifactId>example-dependency-management3</artifactId>
<version>2.2.2</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>org.example</groupId>
<artifactId>example-dependency-management3</artifactId>
<version>2.2.2</version>

<packaging>pom</packaging>
<name>Example API Dependency Management</name>
<description>The example API</description>

<!-- This dependencyManagement doesn't affect dependencies of example-dependency-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.example</groupId>
<artifactId>example-api</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</dependencyManagement>

</project>
6 changes: 4 additions & 2 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,12 @@ func MergeMaps(parent, child map[string]string) map[string]string {
if parent == nil {
return child
}
// Clone parent map to avoid shadow overwrite
newParent := maps.Clone(parent)
for k, v := range child {
parent[k] = v
newParent[k] = v
}
return parent
return newParent
}

func PackageID(name, version string) string {
Expand Down