Skip to content

Commit

Permalink
fix(java): correctly overwrite version from depManagement if dependen…
Browse files Browse the repository at this point in the history
…cy uses `project.*` props (#8050)
  • Loading branch information
DmitriyLewen authored Dec 5, 2024
1 parent 7389961 commit 9d9f80d
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 2 deletions.
46 changes: 46 additions & 0 deletions pkg/dependency/parser/java/pom/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1470,6 +1470,52 @@ func TestPom_Parse(t *testing.T) {
},
},
},
{
name: "overwrite artifact version from dependencyManagement in the root POM when dependency uses `project.*` props",
inputFile: filepath.Join("testdata", "root-pom-dep-management-for-deps-with-project-props", "pom.xml"),
local: true,
want: []ftypes.Package{
{
ID: "com.example:root-pom-dep-management-for-deps-with-project-props:1.0.0",
Name: "com.example:root-pom-dep-management-for-deps-with-project-props",
Version: "1.0.0",
Relationship: ftypes.RelationshipRoot,
},
{
ID: "org.example:example-dependency:1.7.30",
Name: "org.example:example-dependency",
Version: "1.7.30",
Relationship: ftypes.RelationshipDirect,
Locations: ftypes.Locations{
{
StartLine: 21,
EndLine: 25,
},
},
},
{
ID: "org.example:example-api:2.0.0",
Name: "org.example:example-api",
Version: "2.0.0",
Licenses: []string{"The Apache Software License, Version 2.0"},
Relationship: ftypes.RelationshipIndirect,
},
},
wantDeps: []ftypes.Dependency{
{
ID: "com.example:root-pom-dep-management-for-deps-with-project-props:1.0.0",
DependsOn: []string{
"org.example:example-dependency:1.7.30",
},
},
{
ID: "org.example:example-dependency:1.7.30",
DependsOn: []string{
"org.example:example-api:2.0.0",
},
},
},
},
{
name: "transitive dependencyManagement should not be inherited",
inputFile: filepath.Join("testdata", "transitive-dependency-management", "pom.xml"),
Expand Down
4 changes: 2 additions & 2 deletions pkg/dependency/parser/java/pom/pom.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ func (d pomDependency) Resolve(props map[string]string, depManagement, rootDepMa

// If this dependency is managed in the root POM,
// we need to overwrite fields according to the managed dependency.
if managed, found := findDep(d.Name(), rootDepManagement); found { // dependencyManagement from the root POM
if managed, found := findDep(dep.Name(), rootDepManagement); found { // dependencyManagement from the root POM
if managed.Version != "" {
dep.Version = evaluateVariable(managed.Version, props, nil)
}
Expand All @@ -264,7 +264,7 @@ func (d pomDependency) Resolve(props map[string]string, depManagement, rootDepMa
}

// Inherit version, scope and optional from dependencyManagement if empty
if managed, found := findDep(d.Name(), depManagement); found { // dependencyManagement from parent
if managed, found := findDep(dep.Name(), depManagement); found { // dependencyManagement from parent
if dep.Version == "" {
dep.Version = evaluateVariable(managed.Version, props, nil)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?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</artifactId>
<version>1.7.30</version>

<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>example-api</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<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>com.example</groupId>
<artifactId>root-pom-dep-management-for-deps-with-project-props</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.example</groupId>
<artifactId>example-api</artifactId>
<version>2.0.0</version>
</dependency>
</dependencies>
</dependencyManagement>

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

</project>

0 comments on commit 9d9f80d

Please sign in to comment.