Skip to content

RemoveRedundantDependencyVersions It does not appear to support identifying when a property is declared to drive the dependency version #5144

Open
@wac84s

Description

@wac84s

RemoveRedundantDependencyVersions It does not appear to support identifying when a property is declared to drive the dependency version. I dont know if this is a specific spring boot thing or a common use case. Can this support be added?
https://docs.spring.io/spring-boot/appendix/dependency-versions/properties.html (edit

What version of OpenRewrite are you using?

I am using

  • OpenRewrite Bom 3.3.0
  • OpenRewrite v8.47.1
  • rewrite-maven v8.47.1

How are you running OpenRewrite?

I am using the Maven project to write a recipie, and my project is a single module project.

public class MyRecipeChain extends Recipe {

    @Override
    public String getDisplayName() {
        return "My Recipe Chain";
    }

    @Override
    public String getDescription() {
        return "A chain of recipes to perform multiple transformations.";
    }

    @Override
    public List<Recipe> getRecipeList() {
        String groupPattern=null;
        String artifactPattern=null;
        RemoveRedundantDependencyVersions.Comparator onlyIfManagedVersionIs=RemoveRedundantDependencyVersions.Comparator.GTE;
        List<String> except=null;

        return Arrays.asList(
            //new ChangeManagedDependencyGroupIdAndArtifactId("org.yaml","snakeyaml","org.yaml","snakeyaml","2.3.0")
                new org.openrewrite.maven.RemoveRedundantDependencyVersions(groupPattern,artifactPattern,onlyIfManagedVersionIs,except)
                //removes stuff it shouldnt and leaves stuff it shouldnt
                //,new org.openrewrite.maven.RemoveUnusedProperties(null)

            //new ChangeTypeRecipe("old.TypeB", "new.TypeB")
            // Add more recipes as needed
        );
    }
}

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

I picked latest spring parent starter 3.4.3 and purposely picked older dependencies it manages

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
                                 <modelVersion>4.0.0</modelVersion>
                                 <parent>
                                     <groupId>org.springframework.boot</groupId>
                                     <artifactId>spring-boot-starter-parent</artifactId>
                                     <version>3.4.3</version> <!-- lookup parent from repository -->
                                 </parent>
                                 <groupId>com.service</groupId>
                                 <artifactId>service</artifactId>
                                 <version>1.0-SNAPSHOT</version>
                                 <name>service</name>
                                 <description>Demo project for Spring Boot</description>
                             
                                 <properties>
                                     <java.version>20</java.version>
                                     <my-unused-prop>UNUSED</my-unused-prop>
                                     <netty.version>4.1.115.Final</netty.version><!-- should get bumped to 4.1.118.Final -->
                                     <snake.version>2.0</snake.version><!-- should get removed-->
                                     <spring-hateoas.version>2.3.0</spring-hateoas.version><!-- should bump to 2.4.1-->
                                 </properties>
                             
                                 <dependencies>
                                     <dependency>
                                         <groupId>org.springframework.boot</groupId>
                                         <artifactId>spring-boot-starter</artifactId>
                                     </dependency>
                                     <dependency>
                                         <groupId>org.yaml</groupId>
                                         <artifactId>snakeyaml</artifactId>
                                         <version>${snake.version}</version>
                                     </dependency>
                                     <dependency>
                                         <groupId>org.springframework.boot</groupId>
                                         <artifactId>spring-boot-starter-hateoas</artifactId>
                                     </dependency>
                                 </dependencies>
                               </project>

What did you expect to see?

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
                                 <modelVersion>4.0.0</modelVersion>
                                 <parent>
                                     <groupId>org.springframework.boot</groupId>
                                     <artifactId>spring-boot-starter-parent</artifactId>
                                     <version>3.4.3</version> <!-- lookup parent from repository -->
                                 </parent>
                                 <groupId>com.service</groupId>
                                 <artifactId>service</artifactId>
                                 <version>1.0-SNAPSHOT</version>
                                 <name>service</name>
                                 <description>Demo project for Spring Boot</description>
                             
                                 <properties>
                                     <java.version>20</java.version>
                                     <netty.version>4.1.118.Final</netty.version><!-- should get bumped to 4.1.118.Final AND removed at parity with what spring parent manages-->
                                     <spring-hateoas.version>2.4.1</spring-hateoas.version><!-- should bump to 2.4.1 AND removed at parity with what spring parent manages-->
                                 </properties>
                             
                                 <dependencies>
                                     <dependency>
                                         <groupId>org.springframework.boot</groupId>
                                         <artifactId>spring-boot-starter</artifactId>
                                     </dependency>
                                     <dependency>
                                         <groupId>org.yaml</groupId>
                                         <artifactId>snakeyaml</artifactId>
                                     </dependency>
                                     <dependency>
                                         <groupId>org.springframework.boot</groupId>
                                         <artifactId>spring-boot-starter-hateoas</artifactId>
                                     </dependency>
                                 </dependencies>
                               </project>

What did you see instead?

Image

What is the full stack trace of any errors you encountered?

org.opentest4j.AssertionFailedError: [Unexpected result in "pom.xml":
diff --git a/pom.xml b/pom.xml
index 10fab5c..396b945 100644
--- a/pom.xml
+++ b/pom.xml
@@ -1,37 +1,38 @@ 
 <?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
-             <modelVersion>4.0.0</modelVersion>
-             <parent>
-                 <groupId>org.springframework.boot</groupId>
-                 <artifactId>spring-boot-starter-parent</artifactId>
-                 <version>3.4.3</version> <!-- lookup parent from repository -->
-             </parent>
-             <groupId>com.service</groupId>
-             <artifactId>service</artifactId>
-             <version>1.0-SNAPSHOT</version>
-             <name>service</name>
-             <description>Demo project for Spring Boot</description>
+        <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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
+            <modelVersion>4.0.0</modelVersion>
+            <parent>
+                <groupId>org.springframework.boot</groupId>
+                <artifactId>spring-boot-starter-parent</artifactId>
+                <version>3.4.3</version> <!-- lookup parent from repository -->
+            </parent>
+            <groupId>com.service</groupId>
+            <artifactId>service</artifactId>
+            <version>1.0-SNAPSHOT</version>
+            <name>service</name>
+            <description>Demo project for Spring Boot</description>
 
-             <properties>
-                 <java.version>20</java.version>
-                 <netty.version>4.1.118.Final</netty.version><!-- should get bumped to 4.1.118.Final -->
-                 <snake.version>2.0</snake.version><!-- should get removed-->
-                 <spring-hateoas.version>2.4.1</spring-hateoas.version><!-- should bump to 2.4.1-->
-             </properties>
+            <properties>
+                <java.version>20</java.version>
+                <my-unused-prop>UNUSED</my-unused-prop>
+                <netty.version>4.1.115.Final</netty.version><!-- should get bumped to 4.1.118.Final -->
+                <snake.version>2.0</snake.version><!-- should get removed-->
+                <spring-hateoas.version>2.3.0</spring-hateoas.version><!-- should bump to 2.4.1-->
+            </properties>
 
-             <dependencies>
-                 <dependency>
-                     <groupId>org.springframework.boot</groupId>
-                     <artifactId>spring-boot-starter</artifactId>
-                 </dependency>
-                 <dependency>
-                     <groupId>org.yaml</groupId>
-                     <artifactId>snakeyaml</artifactId>
-                 </dependency>
-                 <dependency>
-                     <groupId>org.springframework.boot</groupId>
-                     <artifactId>spring-boot-starter-hateoas</artifactId>
-                 </dependency>
-             </dependencies>
-           </project>
\ No newline at end of file
+            <dependencies>
+                <dependency>
+                    <groupId>org.springframework.boot</groupId>
+                    <artifactId>spring-boot-starter</artifactId>
+                </dependency>
+                <dependency>
+                    <groupId>org.yaml</groupId>
+                    <artifactId>snakeyaml</artifactId>
+                </dependency>
+                <dependency>
+                    <groupId>org.springframework.boot</groupId>
+                    <artifactId>spring-boot-starter-hateoas</artifactId>
+                </dependency>
+            </dependencies>
+          </project>
\ No newline at end of file
] 
expected: 
  "<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
               <modelVersion>4.0.0</modelVersion>
               <parent>
                   <groupId>org.springframework.boot</groupId>
                   <artifactId>spring-boot-starter-parent</artifactId>
                   <version>3.4.3</version> <!-- lookup parent from repository -->
               </parent>
               <groupId>com.service</groupId>
               <artifactId>service</artifactId>
               <version>1.0-SNAPSHOT</version>
               <name>service</name>
               <description>Demo project for Spring Boot</description>
  
               <properties>
                   <java.version>20</java.version>
                   <netty.version>4.1.118.Final</netty.version><!-- should get bumped to 4.1.118.Final -->
                   <snake.version>2.0</snake.version><!-- should get removed-->
                   <spring-hateoas.version>2.4.1</spring-hateoas.version><!-- should bump to 2.4.1-->
               </properties>
  
               <dependencies>
                   <dependency>
                       <groupId>org.springframework.boot</groupId>
                       <artifactId>spring-boot-starter</artifactId>
                   </dependency>
                   <dependency>
                       <groupId>org.yaml</groupId>
                       <artifactId>snakeyaml</artifactId>
                   </dependency>
                   <dependency>
                       <groupId>org.springframework.boot</groupId>
                       <artifactId>spring-boot-starter-hateoas</artifactId>
                   </dependency>
               </dependencies>
             </project>"
 but was: 
  "<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
              <modelVersion>4.0.0</modelVersion>
              <parent>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter-parent</artifactId>
                  <version>3.4.3</version> <!-- lookup parent from repository -->
              </parent>
              <groupId>com.service</groupId>
              <artifactId>service</artifactId>
              <version>1.0-SNAPSHOT</version>
              <name>service</name>
              <description>Demo project for Spring Boot</description>
  
              <properties>
                  <java.version>20</java.version>
                  <my-unused-prop>UNUSED</my-unused-prop>
                  <netty.version>4.1.115.Final</netty.version><!-- should get bumped to 4.1.118.Final -->
                  <snake.version>2.0</snake.version><!-- should get removed-->
                  <spring-hateoas.version>2.3.0</spring-hateoas.version><!-- should bump to 2.4.1-->
              </properties>
  
              <dependencies>
                  <dependency>
                      <groupId>org.springframework.boot</groupId>
                      <artifactId>spring-boot-starter</artifactId>
                  </dependency>
                  <dependency>
                      <groupId>org.yaml</groupId>
                      <artifactId>snakeyaml</artifactId>
                  </dependency>
                  <dependency>
                      <groupId>org.springframework.boot</groupId>
                      <artifactId>spring-boot-starter-hateoas</artifactId>
                  </dependency>
              </dependencies>
            </project>"
<Click to see difference>


	at org.openrewrite.test.RewriteTest.assertContentEquals(RewriteTest.java:618)
	at org.openrewrite.test.RewriteTest.rewriteRun(RewriteTest.java:507)
	at org.openrewrite.test.RewriteTest.rewriteRun(RewriteTest.java:129)
	at org.openrewrite.test.RewriteTest.rewriteRun(RewriteTest.java:124)
	at com.openrewrite.CompareDependenciesRecipeTest.removeRedundantDependencyVersionsAndRemoveUnusedProperties(CompareDependenciesRecipeTest.java:25)
	at java.base/java.lang.reflect.Method.invoke(Method.java:580)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)


Process finished with exit code 255

My UnitTest

package com.openrewrite;

import org.openrewrite.test.RecipeSpec;

import org.junit.jupiter.api.Test;
import org.openrewrite.test.RewriteTest;

import static  org.openrewrite.maven.Assertions.pomXml;



class CompareDependenciesRecipeTest implements RewriteTest {
    @Override
    public void defaults(RecipeSpec spec) {

        spec.recipe(
                //new ChangeDependencyGroupIdAndArtifactId("org.yaml","snakeyaml","org.yaml","snakeyaml","2.3.0","")
                new MyRecipeChain() //see above, i gave the java code its calling RemoveRedundantDependencyVersions
        );
    }

    @Test
    void removeRedundantDependencyVersionsAndRemoveUnusedProperties() {
        String springParentVersion = "3.4.3";
        rewriteRun(
                pomXml(
                """
                     <?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
                                 <modelVersion>4.0.0</modelVersion>
                                 <parent>
                                     <groupId>org.springframework.boot</groupId>
                                     <artifactId>spring-boot-starter-parent</artifactId>
                                     <version>%s</version> <!-- lookup parent from repository -->
                                 </parent>
                                 <groupId>com.service</groupId>
                                 <artifactId>service</artifactId>
                                 <version>1.0-SNAPSHOT</version>
                                 <name>service</name>
                                 <description>Demo project for Spring Boot</description>
                             
                                 <properties>
                                     <java.version>20</java.version>
                                     <my-unused-prop>UNUSED</my-unused-prop>
                                     <netty.version>4.1.115.Final</netty.version><!-- should get bumped to 4.1.118.Final -->
                                     <snake.version>2.0</snake.version><!-- should get removed-->
                                     <spring-hateoas.version>2.3.0</spring-hateoas.version><!-- should bump to 2.4.1-->
                                 </properties>
                             
                                 <dependencies>
                                     <dependency>
                                         <groupId>org.springframework.boot</groupId>
                                         <artifactId>spring-boot-starter</artifactId>
                                     </dependency>
                                     <dependency>
                                         <groupId>org.yaml</groupId>
                                         <artifactId>snakeyaml</artifactId>
                                         <version>${snake.version}</version>
                                     </dependency>
                                     <dependency>
                                         <groupId>org.springframework.boot</groupId>
                                         <artifactId>spring-boot-starter-hateoas</artifactId>
                                     </dependency>
                                 </dependencies>
                               </project>
                """.formatted(springParentVersion),
                """
                    <?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
                                 <modelVersion>4.0.0</modelVersion>
                                 <parent>
                                     <groupId>org.springframework.boot</groupId>
                                     <artifactId>spring-boot-starter-parent</artifactId>
                                     <version>%s</version> <!-- lookup parent from repository -->
                                 </parent>
                                 <groupId>com.service</groupId>
                                 <artifactId>service</artifactId>
                                 <version>1.0-SNAPSHOT</version>
                                 <name>service</name>
                                 <description>Demo project for Spring Boot</description>
                             
                                 <properties>
                                     <java.version>20</java.version>
                                     <netty.version>4.1.118.Final</netty.version><!-- should get bumped to 4.1.118.Final -->
                                     <snake.version>2.0</snake.version><!-- should get removed-->
                                     <spring-hateoas.version>2.4.1</spring-hateoas.version><!-- should bump to 2.4.1-->
                                 </properties>
                             
                                 <dependencies>
                                     <dependency>
                                         <groupId>org.springframework.boot</groupId>
                                         <artifactId>spring-boot-starter</artifactId>
                                     </dependency>
                                     <dependency>
                                         <groupId>org.yaml</groupId>
                                         <artifactId>snakeyaml</artifactId>
                                     </dependency>
                                     <dependency>
                                         <groupId>org.springframework.boot</groupId>
                                         <artifactId>spring-boot-starter-hateoas</artifactId>
                                     </dependency>
                                 </dependencies>
                               </project>
                """.formatted(springParentVersion)
            )
        );
    }

}

Are you interested in contributing a fix to OpenRewrite?

Yes if I could get past the learn curve. Documentation has not been helpful in writing my own recipes

Metadata

Metadata

Assignees

No one assigned

    Labels

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

    Type

    No type

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions