openrewrite / rewrite

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

Cannot update parent in pom.xml #4589

Open natalia-pokrovskaya opened 4 days ago

natalia-pokrovskaya commented 4 days ago

Hello,

I have an issue running custom recipie for org.openrewrite.maven.ChangeParentPom

Here is my configuration: A maven, multi-module project. I want to update the version of the parent of the root's pom.xml file.

Here is my configuration in rules.yml file:

type: specs.openrewrite.org/v1beta/recipe
name: my.test.UpgradeParent
displayName: Change Maven dependency example
recipeList:
  - org.openrewrite.maven.ChangeParentPom:
      oldGroupId: com.my.group
      oldArtifactId: my-artifact
      newVersion: 1.0.4

In my pom.xml I have:

<project>
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>com.my.group</groupId>
        <artifactId>my-artifact</artifactId>
        <version>1.107</version>
    </parent>

    <build>
        <plugins>
            <plugin>
                <groupId>org.openrewrite.maven</groupId>
                <artifactId>rewrite-maven-plugin</artifactId>
                <version>5.42.2</version>
                <configuration>
                    <configLocation>rules.yml</configLocation>
                    <activeRecipes>
                        <recipe>my.test.UpgradeParent</recipe>
                    </activeRecipes>
                    <failOnInvalidActiveRecipes>true</failOnInvalidActiveRecipes>
                </configuration>
            </plugin>
        </plugins>
    </build>
...

But when I run it with rewrite:run or rewrite:dryRun, it doesn't match my parent pom GAV and don't update it...

[INFO] --- rewrite:5.42.2:dryRun (default-cli) @ XXXXX-module ---
[INFO] Using active recipe(s) [com.bnppf.UpgradeToSf5]
[INFO] Using active styles(s) []
[INFO] Validating active recipes...
[INFO] Project [XXXXX] Resolving Poms...
[INFO] Project [XXXXX] Parsing source files
[INFO] Project [XXXXX-module1] Parsing source files
[INFO] Project [XXXXX-module2] Parsing source files
[INFO] Project [XXXXX-module3] Parsing source files
[INFO] Project [XXXXX-module4] Parsing source files
[INFO] Project [XXXXX-module5] Parsing source files
[INFO] Running recipe(s)...
[INFO] Applying recipes would make no changes. No patch file generated.

I tried to specify newArtifactId, newGroupId, but it changes nothing.... What am I doing wrong? Any help will be highly appreciated...

Thank you very much in advance!

I am using:

timtebeek commented 2 days ago

Hi @natalia-pokrovskaya ; thanks for the detailed report. Since it looks to be an internal parent that can not be upgraded I'm wondering if your repositories are configured correctly. Does it work when you try to change the parent pom from one that's available in Maven Central?

svaningelgem commented 1 day ago

I created a very simple project (don't blame me, blame the chatbot for the bad code :p):

structure:

my-parent-project/
├── pom.xml
├── my-module/
│   └── pom.xml
_pom.xml:_

```xml 4.0.0 org.apache.maven maven-parent 33 com.example my-parent-project 1.0-SNAPSHOT pom My Parent Project http://maven.apache.org UTF-8 my-module maven-clean-plugin 3.1.0 maven-resources-plugin 3.2.0 maven-compiler-plugin 3.8.0 maven-surefire-plugin 2.22.1 maven-jar-plugin 3.2.0 maven-install-plugin 2.5.2 maven-deploy-plugin 2.8.2 maven-site-plugin 3.7.1 maven-project-info-reports-plugin 3.0.0 ```

_my-module/pom.xml:_

```xml 4.0.0 com.example my-parent-project 1.0-SNAPSHOT ../pom.xml com.example my-module 1.0-SNAPSHOT jar My Module http://maven.apache.org junit junit 3.8.1 test ```

Step 1: see that it compiles (✅) Step 2: add rewrite to it:

_Adjusted pom.xml:_

(See bottom for build plugin -- literally taken from @natalia-pokrovskaya 's example above) ```xml 4.0.0 org.apache.maven maven-parent 33 com.example my-parent-project 1.0-SNAPSHOT pom My Parent Project http://maven.apache.org UTF-8 my-module maven-clean-plugin 3.1.0 maven-resources-plugin 3.2.0 maven-compiler-plugin 3.8.0 maven-surefire-plugin 2.22.1 maven-jar-plugin 3.2.0 maven-install-plugin 2.5.2 maven-deploy-plugin 2.8.2 maven-site-plugin 3.7.1 maven-project-info-reports-plugin 3.0.0 org.openrewrite.maven rewrite-maven-plugin 5.42.2 rules.yml my.test.UpgradeParent true ```

Step 2b: Add my-module/rules.yml [strange location -- not the root, but the last project executed!!!!]:

_rules.yml:_

```yaml type: specs.openrewrite.org/v1beta/recipe name: my.test.UpgradeParent displayName: Change Maven dependency example recipeList: - org.openrewrite.maven.ChangeParentPom: oldGroupId: org.apache.maven oldArtifactId: maven-parent newVersion: 34 ```

Step 3: run it (mvn rewrite:run -Drat.skip=true) Step 4: works! ‼ Result:

[INFO] --- rewrite:5.42.2:run (default-cli) @ my-module ---
[INFO] Using active recipe(s) [my.test.UpgradeParent]
[INFO] Using active styles(s) []
[INFO] Validating active recipes...
[INFO] Project [My Parent Project] Resolving Poms...
[INFO] Project [My Parent Project] Parsing source files
[INFO] Project [My Module] Parsing source files
[INFO] Running recipe(s)...
[WARNING] Changes have been made to pom.xml by:
[WARNING]     org.openrewrite.maven.ChangeParentPom: {oldGroupId=org.apache.maven, oldArtifactId=maven-parent, newVersion=34}
[WARNING] Please review and commit the results.

But that would mean that the openrewrite plugin is not very useful for any project which is not on the central maven repository? Even though we have a Nexus repository where all the calls are redirected to, openrewrite still tries somehow to go to central as I understand it?

I verified that it doesn't work against an internal parent pom.

timtebeek commented 1 day ago

hi @svaningelgem ; thanks for reporducing. I imagine you're a colleague of @natalia-pokrovskaya and as such are using much of the same settings? OpenRewrite definitely does work for internal projects as well, but perhaps there's an issue with the specific configuration that you're using. Whenever possible we try to use <repositories> and ~/.m2/settings.xml to discover which internal repositories to reach out to, and what credentials to use for those. Sometimes we find those might need to be tweaked or made a little more explicit when there's internal alternative authentication schemes for instance. Are you using any Artifactory that allows you to download & provide a custom settings.xml for instance?

svaningelgem commented 1 day ago

You've guessed correct @timtebeek 👍 .

I have an ~/.m2/settings.xml in which I have 1 repository set, with mirrorOf set to *. Authentication scheme is via username/password, also defined in this settings file.

So, nothing out of the ordinary there (I think)...