Skip to content

Visitor-based changes to parent poms are not seen by child poms #4949

@crankydillo

Description

@crankydillo

These changes need to be 'seen' somehow so that you can do something like UpgradeParentVersion to the 'targeted parent', which is add dependencies that are no longer managed to a dependency management section.

My ultimate goal is to upgrade the parent pom of a multi-module Maven project and to remove any project dependency versions that are lower that what the new parent manages. We initially started with UpgradeParentVersion, but doesn't seem to work well with modules. I'm suspicious that UpgradeMavenModel isn't propagating all changes to the 'POM tree', but I may be misunderstanding how that works. I have a few other issues/comments about this (apologies for not linking yet). Regardless, in later recipes we are messing with dependencies , so added a RemoveRedundantDependencyVersions recipe at the end. This covers multiple cases that you'd think would be covered by UpgradeParentVersion, but it doesn't seem to handle the case where if a.group:a.artifact:1.0 did not exist in the old parent, but does in the new. In that case, we expect all project usage of a.group:a.artifact:1.0 to be replaced with a.group:a.artifact (no version).

The following test, which I added to UpgradeParentVersionTest, is the shortest form of what we're experiencing. jakarta.annotation-api was added in spring-boot-dependencies:3. When you run, you will see that dependency version does not get removed from the child module pom. If you only run UpgradeParentVersion in the test (remove RemoveRedundant..), you'll see neither gets removed. If I bring that back and set onlyIfManagedVersionIs to ANY, it does remove it. However, we don't want to override project-level dependencies that are higher than what we manage.

Originally posted in this discussion

What version of OpenRewrite are you using?

Test below was run on that main branch in the rewrite-maven module

What is the smallest, simplest way to reproduce the problem?

    @Test
    void sams() {
        rewriteRun(
          spec -> spec.recipe(
            new CompositeRecipe(
              Arrays.asList(
                new UpgradeParentVersion("org.springframework.boot", "spring-boot-dependencies", "3.4.1", null, null)
               , new RemoveRedundantDependencyVersions(null, null, RemoveRedundantDependencyVersions.Comparator.GTE, null)
              )
            )
          ),
            pomXml("""
              <project>
                <parent>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-dependencies</artifactId>
                  <version>2.4.0</version>
                </parent>
                <groupId>cool.stuff</groupId>
                <artifactId>parent</artifactId>
                <version>1.0-SNAPSHOT</version>
                <packaging>pom</packaging>
                <modules>
                  <module>child</module>
                </modules>
                <dependencies>
                  <dependency>
                    <groupId>jakarta.annotation</groupId>
                    <artifactId>jakarta.annotation-api</artifactId>
                    <version>2.1.1</version>
                  </dependency>
                  <dependency>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-api</artifactId>
                    <version>1.7.30</version>
                  </dependency>
                </dependencies>
              </project>
              """, """
              <project>
                <parent>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-dependencies</artifactId>
                  <version>3.4.1</version>
                </parent>
                <groupId>cool.stuff</groupId>
                <artifactId>parent</artifactId>
                <version>1.0-SNAPSHOT</version>
                <packaging>pom</packaging>
                <modules>
                  <module>child</module>
                </modules>
                <dependencies>
                  <dependency>
                    <groupId>jakarta.annotation</groupId>
                    <artifactId>jakarta.annotation-api</artifactId>
                  </dependency>
                  <dependency>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-api</artifactId>
                  </dependency>
                </dependencies>
              </project>
              """),
              mavenProject("child", pomXml("""
                  <project>
                    <parent>
                      <groupId>cool.stuff</groupId>
                      <artifactId>parent</artifactId>
                      <version>1.0-SNAPSHOT</version>
                    </parent>
                    <artifactId>child</artifactId>
                    <version>1.0-SNAPSHOT</version>
                    <dependencies>
                      <dependency>
                        <groupId>jakarta.annotation</groupId>
                        <artifactId>jakarta.annotation-api</artifactId>
                        <version>2.1.1</version>
                      </dependency>
                      <dependency>
                        <groupId>org.slf4j</groupId>
                        <artifactId>slf4j-api</artifactId>
                        <version>1.7.30</version>
                      </dependency>
                    </dependencies>
                  </project>
                """,
                """
                  <project>
                    <parent>
                      <groupId>cool.stuff</groupId>
                      <artifactId>parent</artifactId>
                      <version>1.0-SNAPSHOT</version>
                    </parent>
                    <artifactId>child</artifactId>
                    <version>1.0-SNAPSHOT</version>
                    <dependencies>
                      <dependency>
                        <groupId>jakarta.annotation</groupId>
                        <artifactId>jakarta.annotation-api</artifactId>
                      </dependency>
                      <dependency>
                        <groupId>org.slf4j</groupId>
                        <artifactId>slf4j-api</artifactId>
                      </dependency>
                    </dependencies>
                  </project>
                """
              ))
        );
    }

Update

Original title: Update: Original title: Removing managed dependency problem: Upgrade parent version or remove redundant version recipe (GTE)

#5051 OP discusses the root problem.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingmaventest providedAlready replicated with a unit test, using JUnit pioneer's ExpectedToFail

    Type

    No type

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions