Skip to content

Commit

Permalink
make Maven parent path relative on current project (#987)
Browse files Browse the repository at this point in the history
Previously, the path is always relative to the provided path to
`pom.xml`, however, this path should be relative to the current parent
path.

Also we observe that sometimes `pom.xml` is omitted in `</relativePath>`
so when this happens, we manually append `pom.xml` to file path.
  • Loading branch information
cuixq committed May 23, 2024
1 parent 5eed7e8 commit e2816e3
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
23 changes: 23 additions & 0 deletions internal/resolution/manifest/fixtures/parent/grandparent/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?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.grandparent</groupId>
<artifactId>grandparent-pom</artifactId>
<version>1.1.1</version>

<name>my-app</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>

<packaging>pom</packaging>

<parent>
<groupId>org.upstream</groupId>
<artifactId>parent-pom</artifactId>
<version>1.2.3</version>
</parent>

</project>
7 changes: 4 additions & 3 deletions internal/resolution/manifest/fixtures/parent/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
<packaging>pom</packaging>

<parent>
<groupId>org.upstream</groupId>
<artifactId>parent-pom</artifactId>
<version>1.2.3</version>
<groupId>org.grandparent</groupId>
<artifactId>grandparentparent-pom</artifactId>
<version>1.1.1</version>
<relativePath>./grandparent</relativePath>
</parent>

<properties>
Expand Down
8 changes: 7 additions & 1 deletion internal/resolution/manifest/maven.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ func (m MavenManifestIO) Read(df lockfile.DepFile) (Manifest, error) {
const MaxParent = 100

func (m MavenManifestIO) MergeParents(ctx context.Context, result *maven.Project, current maven.Parent, start int, path string, addRequirements func(maven.Project, string), prefix string) error {
currentPath := path
visited := make(map[maven.ProjectKey]bool, MaxParent)
for n := start; n < MaxParent; n++ {
if current.GroupID == "" || current.ArtifactID == "" || current.Version == "" {
Expand All @@ -200,7 +201,12 @@ func (m MavenManifestIO) MergeParents(ctx context.Context, result *maven.Project

var proj maven.Project
if current.RelativePath != "" {
f, err := os.Open(filepath.Join(filepath.Dir(path), string(current.RelativePath)))
currentPath = filepath.Join(filepath.Dir(currentPath), string(current.RelativePath))
if filepath.Base(currentPath) != "pom.xml" {
// If the base is not pom.xml, this path is a directory but not a file.
currentPath = filepath.Join(currentPath, "pom.xml")
}
f, err := os.Open(currentPath)
if err != nil {
return fmt.Errorf("failed to open parent file %s: %w", current.RelativePath, err)
}
Expand Down

0 comments on commit e2816e3

Please sign in to comment.