Repository: openrewrite/rewrite (2026-04-06T12:05:18Z)

Issue:
UpdateScmFromGitOrigin - update only parent pom
<!--
Thank you for suggesting an improvement to OpenRewrite!
Feel free to delete any sections that don't apply to your request.
-->
## What problem are you trying to solve?
Since this MR https://github.com/openrewrite/rewrite/pull/5798, `UpdateScmFromGitOrigin` recipe with `addIfMissing=true` update all pom of a Maven project, root pom and all modules pom.

## Describe the solution you'd like
Add an new option to update only root pom when `addIfMissing=true`


Discussion:
Yes makes sense, thanks! I think we can even make this the default without adding further options; I don't immediately see a reason why we'd ever add this anywhere else but in the root pom.
Did a very quick attempt but so far it's not yet working as expected:
```diff
diff --git i/rewrite-maven/src/main/java/org/openrewrite/maven/UpdateScmFromGitOrigin.java w/rewrite-maven/src/main/java/org/openrewrite/maven/UpdateScmFromGitOrigin.java
index 7c038b703..068cedaa0 100644
--- i/rewrite-maven/src/main/java/org/openrewrite/maven/UpdateScmFromGitOrigin.java
+++ w/rewrite-maven/src/main/java/org/openrewrite/maven/UpdateScmFromGitOrigin.java
@@ -23,6 +23,7 @@
 import org.openrewrite.Recipe;
 import org.openrewrite.TreeVisitor;
 import org.openrewrite.marker.GitProvenance;
+import org.openrewrite.maven.tree.MavenResolutionResult;
 import org.openrewrite.xml.AddToTagVisitor;
 import org.openrewrite.xml.ChangeTagValueVisitor;
 import org.openrewrite.xml.tree.Xml;
@@ -40,7 +41,7 @@ public class UpdateScmFromGitOrigin extends Recipe {
 
     @Option(displayName = "Add if missing",
             description = "If set to `true`, the recipe will add a `<scm>` section if it is missing. " +
-                          "If set to `false` (default), the recipe will only update existing `<scm>` sections.",
+                    "If set to `false` (default), the recipe will only update existing `<scm>` sections.",
             required = false)
     @Nullable
     Boolean addIfMissing;
@@ -53,7 +54,7 @@ public String getDisplayName() {
     @Override
     public String getDescription() {
         return "Updates or adds the Maven `<scm>` tag based on the Git remote origin. " +
-               "By default, only existing Source Control Management (SCM) sections are updated. Set `addIfMissing` to `true` to also add missing SCM sections.";
+                "By default, only existing Source Control Management (SCM) sections are updated. Set `addIfMissing` to `true` to also add missing SCM sections to root POMs (POMs without a parent element).";
     }
 
     @Override
@@ -68,13 +69,20 @@ public Xml visitDocument(Xml.Document document, ExecutionContext ctx) {
                 Optional<GitOrigin> maybeOrigin = document.getMarkers().findFirst(GitProvenance.class)
                         .map(GitProvenance::getOrigin)
                         .map(GitOrigin::parseGitUrl);
-                if(!maybeOrigin.isPresent()) {
+                if (!maybeOrigin.isPresent()) {
                     return document;
                 }
                 gitOrigin = maybeOrigin.get();
 
-                if(!document.getRoot().getChild("scm").isPresent()) {
+                if (!document.getRoot().getChild("scm").isPresent()) {
                     if (Boolean.TRUE.equals(addIfMissing)) {
+                        Optional<MavenResolutionResult> mavenResolutionResult = document.getMarkers()
+                                .findFirst(MavenResolutionResult.class)
+                                .map(MavenResolutionResult::getParent);
+                        if (mavenResolutionResult.isPresent()) {
+                            return document;
+                        }
+
                         // Build the SCM tag with all required elements
                         String httpUrl = "https://" + gitOrigin.getHost() + "/" + gitOrigin.getPath();
                         String gitPath = gitOrigin.getPath().endsWith(".git") ?
@@ -84,7 +92,7 @@ public Xml visitDocument(Xml.Document document, ExecutionContext ctx) {
                                 "  <connection>scm:git:" + httpUrl + (httpUrl.endsWith(".git") ? "" : ".git") + "</connection>\n" +
                                 "  <developerConnection>scm:git:git@" + gitOrigin.getHost() + ":" + gitPath + ".git</developerConnection>\n" +
                                 "</scm>";
-                        document = (Xml.Document) new AddToTagVisitor<>(document.getRoot(), Xml.Tag.build(scmContent),
+                        return new AddToTagVisitor<>(document.getRoot(), Xml.Tag.build(scmContent),
                                 new MavenTagInsertionComparator(document.getRoot().getChildren()))
                                 .visitNonNull(document, ctx, Objects.requireNonNull(getCursor().getParent()));
 
diff --git i/rewrite-maven/src/test/java/org/openrewrite/maven/UpdateScmFromGitOriginTest.java w/rewrite-maven/src/test/java/org/openrewrite/maven/UpdateScmFromGitOriginTest.java
index 871d7ac91..d93d6957a 100644
--- i/rewrite-maven/src/test/java/org/openrewrite/maven/UpdateScmFromGitOriginTest.java
+++ w/rewrite-maven/src/test/java/org/openrewrite/maven/UpdateScmFromGitOriginTest.java
@@ -18,11 +18,13 @@
 import org.junit.jupiter.api.Nested;
 import org.junit.jupiter.api.Test;
 import org.openrewrite.DocumentExample;
+import org.openrewrite.Issue;
 import org.openrewrite.marker.GitProvenance;
 import org.openrewrite.test.RewriteTest;
 
 import java.util.UUID;
 
+import static org.openrewrite.java.Assertions.mavenProject;
 import static org.openrewrite.maven.Assertions.pomXml;
 
 class UpdateScmFromGitOriginTest implements RewriteTest {
@@ -462,6 +464,62 @@ void stillUpdatesExistingScmWhenAddIfMissingIsTrue() {
               )
             );
         }
+
+        @Issue("https://github.com/openrewrite/rewrite/issues/5812")
+        @Test
+        void addsScmToRootPomOnly() {
+            rewriteRun(
+              spec -> spec.recipe(new UpdateScmFromGitOrigin(true)),
+              mavenProject("parent",
+                pomXml(
+                  """
+                    <project>
+                      <modelVersion>4.0.0</modelVersion>
+                      <groupId>com.mycompany.app</groupId>
+                      <artifactId>my-app-parent</artifactId>
+                      <version>1</version>
+                      <packaging>pom</packaging>
+                      <modules>
+                        <module>module1</module>
+                      </modules>
+                    </project>
+                    """,
+                  """
+                    <project>
+                      <modelVersion>4.0.0</modelVersion>
+                      <groupId>com.mycompany.app</groupId>
+                      <artifactId>my-app-parent</artifactId>
+                      <version>1</version>
+                      <packaging>pom</packaging>
+                      <modules>
+                        <module>module1</module>
+                      </modules>
+                      <scm>
+                        <url>https://github.com/example/repo</url>
+                        <connection>scm:git:https://github.com/example/repo.git</connection>
+                        <developerConnection>scm:git:git@github.com:example/repo.git</developerConnection>
+                      </scm>
+                    </project>
+                    """,
+                  spec -> spec.markers(gitProvenance("https://github.com/example/repo.git"))
+                ),
+                mavenProject("module1",
+                  pomXml(
+                    """
+                      <project>
+                        <modelVersion>4.0.0</modelVersion>
+                        <parent>
+                          <groupId>com.mycompany.app</groupId>
+                          <artifactId>my-app-parent</artifactId>
+                          <version>1</version>
+                        </parent>
+                        <artifactId>module1</artifactId>
+                      </project>
+                      """
+                  ))
+              )
+            );
+        }
     }
 
     @Nested
```



Patch:
diff --git a/rewrite-maven/src/main/java/org/openrewrite/maven/UpdateScmFromGitOrigin.java b/rewrite-maven/src/main/java/org/openrewrite/maven/UpdateScmFromGitOrigin.java
index c2d838af2b5..9e2b5da6ae9 100644
--- a/rewrite-maven/src/main/java/org/openrewrite/maven/UpdateScmFromGitOrigin.java
+++ b/rewrite-maven/src/main/java/org/openrewrite/maven/UpdateScmFromGitOrigin.java
@@ -23,6 +23,7 @@
 import org.openrewrite.Recipe;
 import org.openrewrite.TreeVisitor;
 import org.openrewrite.marker.GitProvenance;
+import org.openrewrite.maven.tree.MavenResolutionResult;
 import org.openrewrite.xml.AddToTagVisitor;
 import org.openrewrite.xml.ChangeTagValueVisitor;
 import org.openrewrite.xml.tree.Xml;
@@ -48,7 +49,7 @@ public class UpdateScmFromGitOrigin extends Recipe {
     String displayName = "Update SCM with Git origin";
 
     String description = "Updates or adds the Maven `<scm>` tag based on the Git remote origin. " +
-               "By default, only existing Source Control Management (SCM) sections are updated. Set `addIfMissing` to `true` to also add missing SCM sections.";
+               "By default, only existing Source Control Management (SCM) sections are updated. Set `addIfMissing` to `true` to also add missing SCM sections to root POMs (POMs without a parent element).";
 
     @Override
     public TreeVisitor<?, ExecutionContext> getVisitor() {
@@ -69,6 +70,15 @@ public Xml visitDocument(Xml.Document document, ExecutionContext ctx) {
 
                 if(!document.getRoot().getChild("scm").isPresent()) {
                     if (Boolean.TRUE.equals(addIfMissing)) {
+                        // Only add SCM to root POMs (those without a parent)
+                        boolean hasParent = document.getMarkers()
+                                .findFirst(MavenResolutionResult.class)
+                                .map(mrr -> mrr.getParent() != null)
+                                .orElse(false);
+                        if (hasParent) {
+                            return document;
+                        }
+
                         // Build the SCM tag with all required elements
                         String httpUrl = "https://" + gitOrigin.getHost() + "/" + gitOrigin.getPath();
                         String gitPath = gitOrigin.getPath().endsWith(".git") ?


Test patch:
diff --git a/rewrite-maven/src/test/java/org/openrewrite/maven/UpdateScmFromGitOriginTest.java b/rewrite-maven/src/test/java/org/openrewrite/maven/UpdateScmFromGitOriginTest.java
index 333ad3ba421..482c1e1730c 100644
--- a/rewrite-maven/src/test/java/org/openrewrite/maven/UpdateScmFromGitOriginTest.java
+++ b/rewrite-maven/src/test/java/org/openrewrite/maven/UpdateScmFromGitOriginTest.java
@@ -18,12 +18,14 @@
 import org.junit.jupiter.api.Nested;
 import org.junit.jupiter.api.Test;
 import org.openrewrite.DocumentExample;
+import org.openrewrite.Issue;
 import org.openrewrite.marker.GitProvenance;
 import org.openrewrite.test.RecipeSpec;
 import org.openrewrite.test.RewriteTest;
 
 import java.util.UUID;
 
+import static org.openrewrite.java.Assertions.mavenProject;
 import static org.openrewrite.maven.Assertions.pomXml;
 
 class UpdateScmFromGitOriginTest implements RewriteTest {
@@ -460,6 +462,62 @@ void stillUpdatesExistingScmWhenAddIfMissingIsTrue() {
               )
             );
         }
+
+        @Issue("https://github.com/openrewrite/rewrite/issues/5812")
+        @Test
+        void addsScmToRootPomOnly() {
+            rewriteRun(
+              spec -> spec.recipe(new UpdateScmFromGitOrigin(true)),
+              mavenProject("parent",
+                pomXml(
+                  """
+                    <project>
+                      <modelVersion>4.0.0</modelVersion>
+                      <groupId>com.mycompany.app</groupId>
+                      <artifactId>my-app-parent</artifactId>
+                      <version>1</version>
+                      <packaging>pom</packaging>
+                      <modules>
+                        <module>module1</module>
+                      </modules>
+                    </project>
+                    """,
+                  """
+                    <project>
+                      <modelVersion>4.0.0</modelVersion>
+                      <groupId>com.mycompany.app</groupId>
+                      <artifactId>my-app-parent</artifactId>
+                      <version>1</version>
+                      <packaging>pom</packaging>
+                      <modules>
+                        <module>module1</module>
+                      </modules>
+                      <scm>
+                        <url>https://github.com/example/repo</url>
+                        <connection>scm:git:https://github.com/example/repo.git</connection>
+                        <developerConnection>scm:git:git@github.com:example/repo.git</developerConnection>
+                      </scm>
+                    </project>
+                    """,
+                  spec -> spec.markers(gitProvenance("https://github.com/example/repo.git"))
+                ),
+                mavenProject("module1",
+                  pomXml(
+                    """
+                      <project>
+                        <modelVersion>4.0.0</modelVersion>
+                        <parent>
+                          <groupId>com.mycompany.app</groupId>
+                          <artifactId>my-app-parent</artifactId>
+                          <version>1</version>
+                        </parent>
+                        <artifactId>module1</artifactId>
+                      </project>
+                      """
+                  ))
+              )
+            );
+        }
     }
 
     @Nested