openrewrite / rewrite

Automated mass refactoring of source code.
https://docs.openrewrite.org
Apache License 2.0
2.03k stars 300 forks source link

ChangeDependencyGroupIdAndArtifactId: No version provided for dependency #1750

Closed josemariavillar closed 2 years ago

josemariavillar commented 2 years ago

Good morning,

I am having problems with the recipe 2ChangeDependencyGroupIdAndArtifactId". This problem occurs when I want to change several dependencies that are no longer defined in the new parent. For example, in the attached example, as you can see I first update the POM parent to the latest version of Spring Boot. In this version, the dependency com.oracle.ojdbc:ojdbc8 no longer exists and so I run the recipe "ChangeDependencyGroupIdAndArtifactId" to replace it with com.oracle.database.jdbc:ojdbc8.

<!-- Use starter parent -->
<parent>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-parent</artifactId>
   <version>2.2.6.RELEASE</version>
</parent>

...

<dependencies>
  <dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
  </dependency>
  <dependency>
    <groupId>com.oracle.ojdbc</groupId>
    <artifactId>ojdbc8</artifactId>
  </dependency>
</dependencies>

When executing the :

type: specs.openrewrite.org/v1beta/recipe
name: com.yourorg.UpgradePom
displayName: Upgrade POM
recipeList:
  - org.openrewrite.maven.ChangeParentPom:
      oldGroupId: org.springframework.boot
      oldArtifactId: spring-boot-starter-parent
      newVersion: 2.6.6

  - org.openrewrite.maven.ChangeDependencyGroupIdAndArtifactId:
        oldGroupId: org.apache.httpcomponents
        oldArtifactId: httpclient
        newGroupId: org.apache.httpcomponents.client5
        newArtifactId: httpclient5

  - org.openrewrite.maven.ChangeDependencyGroupIdAndArtifactId:
      oldGroupId: com.oracle.ojdbc
      oldArtifactId: ojdbc8
      newGroupId: com.oracle.database.jdbc
      newArtifactId: ojdbc8

The result obtained is:

[INFO] --- rewrite-maven-plugin:4.23.0:dryRun (default-cli) @ test-project ---
[INFO] Using active recipe(s) [com.yourorg.UpgradePom]
[INFO] Using active styles(s) []
[INFO] Validating active recipes...
[INFO] Parsing Java main files...
[INFO] Parsing Java test files...
[INFO] Running recipe(s)...
[WARNING] No version provided for dependency com.oracle.ojdbc:ojdbc8
[WARNING] These recipes would make changes to pom.xml:
[WARNING]     com.yourorg.UpgradePom
[WARNING]         org.openrewrite.maven.ChangeParentPom: {oldGroupId=org.springframework.boot, oldArtifactId=spring-boot-starter-parent, newVersion=2.6.6, allowVersionDowngrades=false}
[WARNING]         org.openrewrite.maven.ChangeDependencyGroupIdAndArtifactId: {oldGroupId=com.oracle.ojdbc, oldArtifactId=ojdbc8, newGroupId=com.oracle.database.jdbc, newArtifactId=ojdbc8}
[WARNING] Patch file available:
[WARNING]     C:\test_project\target\site\rewrite\rewrite.patch
[WARNING] Run 'mvn rewrite:run' to apply the recipes.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  01:00 min
[INFO] Finished at: 2022-05-03T13:19:10+02:00
[INFO] ------------------------------------------------------------------------
diff --git a/pom.xml b/pom.xml
index 8e5b31f..02c7ed7 100644
--- a/pom.xml
+++ b/pom.xml
@@ -14,7 +14,7 @@ org.openrewrite.maven.ChangeDependencyGroupIdAndArtifactId, org.openrewrite.maven.ChangeParentPom
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
-       <version>2.2.6.RELEASE</version>
+       <version>2.6.6</version>
    </parent>

    <properties>
@@ -27,7 +27,7 @@
            <artifactId>httpclient</artifactId>
        </dependency>
        <dependency>
-           <groupId>com.oracle.ojdbc</groupId>
+           <groupId>com.oracle.database.jdbc</groupId>
            <artifactId>ojdbc8</artifactId>
        </dependency>
    </dependencies>

And, as you can see, the recipe is not executed.

  - org.openrewrite.maven.ChangeDependencyGroupIdAndArtifactId:
        oldGroupId: org.apache.httpcomponents
        oldArtifactId: httpclient
        newGroupId: org.apache.httpcomponents.client5
        newArtifactId: httpclient5

This problem occurs in version 4.22.0, as well as in version 4.23.0 of the plugin.

You can reproduce the problem in the following attached project: https://github.com/josemariavillar/test_project/tree/no-version-provided

Thank you very much for the excellent support you provide

Thanks & regards

tkvangorder commented 2 years ago

Thanks for reporting! We recently made some changes (in 7.23.x) to this recipe that may help with this issue:

See: https://github.com/openrewrite/rewrite/issues/1723

I think if you were to add newVersion to your recipes:

  - org.openrewrite.maven.ChangeDependencyGroupIdAndArtifactId:
        oldGroupId: org.apache.httpcomponents
        oldArtifactId: httpclient
        newGroupId: org.apache.httpcomponents.client5
        newArtifactId: httpclient5
        newVersion: <desired version here>

  - org.openrewrite.maven.ChangeDependencyGroupIdAndArtifactId:
      oldGroupId: com.oracle.ojdbc
      oldArtifactId: ojdbc8
      newGroupId: com.oracle.database.jdbc
      newArtifactId: ojdbc8
      newVersion: <desired version here>

This recipe now uses the Maven Resolution to be a little smarter with its modifications:

@josemariavillar Can you confirm that this works as you would expect it with the latest snapshot? I also realize that we need to add the ability to use the semver selector for the newVersion. (that is a really good suggestion)

josemariavillar commented 2 years ago

Good morning, I will check it and let you know. Thank you very much.

tkvangorder commented 2 years ago

Hi @josemariavillar ,

I am going to close this issue but if, for some reason, you find it is not working please feel free to re-open it.