Skip to content

Commit

Permalink
feat: write Maven updates to parent pom.xml if possible (#1182)
Browse files Browse the repository at this point in the history
#1169

Currently, Maven updater only supports writing updates in base pom.xml,
but we would like to write updates to local parent pom.xml to minimize
diffs.

This PR supports writing updates to parent pom.xml:
- when building patches, local parent pom.xml files are walked through
to collect original dependencies and properties (this step may be done
when we merge parents in reading manifest). Paths to parent manifests
are recorded in the origin so we don't need to walk through the parents
again.
- when writing the patches, if any patch is found for specific parent
path, the manifest will be overwritten with the new patches.

TODO: we should investigate how to test the updated local parent pom.xml
which is not in this PR.
  • Loading branch information
cuixq authored Aug 19, 2024
1 parent aedd49a commit 62a848e
Show file tree
Hide file tree
Showing 4 changed files with 448 additions and 268 deletions.
10 changes: 5 additions & 5 deletions internal/manifest/maven.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func MergeMavenParents(ctx context.Context, mavenClient datasource.MavenRegistry
if err := xml.NewDecoder(f).Decode(&proj); err != nil {
return fmt.Errorf("failed to unmarshal project: %w", err)
}
if mavenProjectKey(proj) == current.ProjectKey && proj.Packaging == "pom" {
if MavenProjectKey(proj) == current.ProjectKey && proj.Packaging == "pom" {
// Only mark parent is found when the identifiers and packaging are exptected.
parentFound = true
}
Expand All @@ -188,13 +188,13 @@ func MergeMavenParents(ctx context.Context, mavenClient datasource.MavenRegistry
// A parent project should only be of "pom" packaging type.
return fmt.Errorf("invalid packaging for parent project %s", proj.Packaging)
}
if mavenProjectKey(proj) != current.ProjectKey {
if MavenProjectKey(proj) != current.ProjectKey {
// The identifiers in parent does not match what we want.
return fmt.Errorf("parent identifiers mismatch: %v, expect %v", proj.ProjectKey, current.ProjectKey)
}
}
// Empty JDK and ActivationOS indicates merging the default profiles.
if err := result.MergeProfiles("", maven.ActivationOS{}); err != nil {
if err := proj.MergeProfiles("", maven.ActivationOS{}); err != nil {
return err
}
result.MergeParent(proj)
Expand All @@ -204,9 +204,9 @@ func MergeMavenParents(ctx context.Context, mavenClient datasource.MavenRegistry
return result.Interpolate()
}

// mavenProjectKey returns a project key with empty groupId/version
// MavenProjectKey returns a project key with empty groupId/version
// filled by corresponding fields in parent.
func mavenProjectKey(proj maven.Project) maven.ProjectKey {
func MavenProjectKey(proj maven.Project) maven.ProjectKey {
if proj.GroupID == "" {
proj.GroupID = proj.Parent.GroupID
}
Expand Down
8 changes: 8 additions & 0 deletions internal/resolution/manifest/fixtures/maven/parent/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@
<aaa.version>1.1.1</aaa.version>
</properties>

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

<dependencyManagement>
<dependencies>
<dependency>
Expand Down
Loading

0 comments on commit 62a848e

Please sign in to comment.