rewrite icon indicating copy to clipboard operation
rewrite copied to clipboard

rewrite:run (warns about) failing to download SNAPSHOT parent from local repository

Open DidierLoiseau opened this issue 1 year ago • 2 comments

What version of OpenRewrite are you using?

I am using

  • Maven plugin v5.39.2
  • rewrite-migrate-java v2.23.0 (shouldn’t be relevant?)

How are you running OpenRewrite?

I am using the Maven plugin, and my project is a single module project with a parent in SHAPSHOT version.

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

Parent pom Just install it in your local repository with mvn install.

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>parent-pom</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>

</project>

Child pom

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.example</groupId>
        <artifactId>parent-pom</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>child-pom</artifactId>

    <build>
        <plugins>
            <plugin>
                <groupId>org.openrewrite.maven</groupId>
                <artifactId>rewrite-maven-plugin</artifactId>
                <version>5.39.2</version>
                <configuration>
                    <activeRecipes>
                        <recipe>org.openrewrite.java.migrate.UpgradeBuildToJava17</recipe>
                    </activeRecipes>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.openrewrite.recipe</groupId>
                        <artifactId>rewrite-migrate-java</artifactId>
                        <version>2.23.0</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>
</project>

I’m running rewrite:run on the child pom.

What did you expect to see?

No warning from the maven plugin.

What did you see instead?

Multiple warnings about failing to download the parent from the local repository. I’m not sure about the impact – parent gets ignored in recipes?

[INFO] --- rewrite:5.39.2:run (default-cli) @ child-pom ---
[INFO] Using active recipe(s) [org.openrewrite.java.migrate.UpgradeBuildToJava17]
[INFO] Using active styles(s) []
[INFO] Validating active recipes...
[INFO] Project [child-pom] Resolving Poms...
[WARNING] Failed to download com.example:parent-pom:1.0-SNAPSHOT. Attempted URIs:
  - file:///C:/Users/loiseadi/.m2/repository/
[WARNING] Failed to download com.example:parent-pom:1.0-SNAPSHOT. Attempted URIs:
  - file:///C:/Users/loiseadi/.m2/repository/
[WARNING] Failed to download com.example:parent-pom:1.0-SNAPSHOT. Attempted URIs:
  - file:///C:/Users/loiseadi/.m2/repository/
[INFO] Project [child-pom] Parsing source files
[INFO] Running recipe(s)...
[WARNING] Failed to download com.example:parent-pom:1.0-SNAPSHOT. Attempted URIs:
  - file:///C:/Users/loiseadi/.m2/repository/
[WARNING] Failed to download com.example:parent-pom:1.0-SNAPSHOT. Attempted URIs:
  - file:///C:/Users/loiseadi/.m2/repository/
[WARNING] Failed to download com.example:parent-pom:1.0-SNAPSHOT. Attempted URIs:
  - file:///C:/Users/loiseadi/.m2/repository/

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

With debug output:

[DEBUG] -- end configuration --
[INFO] Using active recipe(s) [org.openrewrite.java.migrate.UpgradeBuildToJava17]
[INFO] Using active styles(s) []
[DEBUG] No rewrite configuration found at C:\projects\maven-tests\child-pom\rewrite.yml
[INFO] Validating active recipes...
[INFO] Project [child-pom] Resolving Poms...
[WARNING] Failed to download com.example:parent-pom:1.0-SNAPSHOT. Attempted URIs:
  - file:///C:/Users/loiseadi/.m2/repository/
[DEBUG] Downloaded com.example:parent-pom:1.0-SNAPSHOT from com.example:child-pom:1.0-SNAPSHOT
[WARNING] Failed to download com.example:parent-pom:1.0-SNAPSHOT. Attempted URIs:
  - file:///C:/Users/loiseadi/.m2/repository/
[WARNING] Failed to download com.example:parent-pom:1.0-SNAPSHOT. Attempted URIs:
  - file:///C:/Users/loiseadi/.m2/repository/
[DEBUG] Project [child-pom] Base directory : 'C:\projects\maven-tests\child-pom'
[DEBUG] Project [child-pom]   Collected Maven POM : 'C:\projects\maven-tests\child-pom\pom.xml'
[DEBUG] Project [child-pom]   Maven Source : 'C:\projects\maven-tests\child-pom\pom.xml'
[INFO] Project [child-pom] Parsing source files
[DEBUG] Project [child-pom] Scanned 0 resource files in main scope.
[DEBUG] Project [child-pom] Scanned 0 resource files in test scope.
[DEBUG] Project [child-pom] Parsed 0 additional files found within the project.
[INFO] Running recipe(s)...
[DEBUG] Using SLF4J as the default logging framework
[WARNING] Failed to download com.example:parent-pom:1.0-SNAPSHOT. Attempted URIs:
  - file:///C:/Users/loiseadi/.m2/repository/
[WARNING] Failed to download com.example:parent-pom:1.0-SNAPSHOT. Attempted URIs:
  - file:///C:/Users/loiseadi/.m2/repository/
[WARNING] Failed to download com.example:parent-pom:1.0-SNAPSHOT. Attempted URIs:
  - file:///C:/Users/loiseadi/.m2/repository/

Note the Downloaded com.example:parent-pom:1.0-SNAPSHOT from com.example:child-pom:1.0-SNAPSHOT – so it failed successfully?

Also note that there is no warning when parent is not -SNAPSHOT.

DidierLoiseau avatar Sep 12 '24 18:09 DidierLoiseau

I decided to try debugging it. Despite the log messages coming from rewrite-maven-plugin, I think the issue comes from rewrite-maven:

                    String uri = repo.getUri() + (repo.getUri().endsWith("/") ? "" : "/") +
                                 requireNonNull(gav.getGroupId()).replace('.', '/') + '/' +
                                 gav.getArtifactId() + '/' +
                                 (gav.getVersion() == null ? "" : gav.getVersion() + '/') +
                                 "maven-metadata.xml";

Indeed, in local, the metadata files are called maven-metadata-<repo-id>.xml – so maven-metadata-local.xml for the local repository itself. See Maven Repository Metadata Model.

I don’t know if this ticket should be moved to rewrite then? Or should I recreate it myself maybe?

DidierLoiseau avatar Sep 13 '24 09:09 DidierLoiseau

Hi! Thanks for logging your findings & the patience before a reply. We've pushed a change in 5.40.2 that might change the behavior that you're seeing here:

  • https://github.com/openrewrite/rewrite-maven-plugin/releases/tag/v5.40.2
  • https://github.com/openrewrite/rewrite/pull/4486

Although in this case it seems a child module is still looking for the parent in ~/.m2/repository as opposed to looking up the <relativePath>../</relativePath>? Indeed seems odd that you're seeing warnings on that. Appreciate you calling it out and doing some initial debugging, and the helpful link to the meta data model. Indeed would lean towards an issue with openrewrite/rewrite then. I'll move the issue accordingly.

timtebeek avatar Sep 20 '24 18:09 timtebeek

This will also happen when using non-snapshot versions not available on configured maven repositories.

I think I found a possible solution for this while writing the unit test for

  • #4607.

There, I'm adding the snapshot versions manually to the maven pom cache.

If this is a viable solution, I can submit a PR. Any thoughts, @timtebeek?

ammachado avatar Oct 24 '24 13:10 ammachado

This will also happen when using non-snapshot versions not available on configured maven repositories.

The issue here was about a warning emitted when depending on snapshot, which turned out to be because it was trying to retrieve the Maven Metadata with the wrong file name. I think that issue is solved now but I didn’t recheck.

@timtebeek indeed noticed a strange behavior, to which I forgot to reply:

Although in this case it seems a child module is still looking for the parent in ~/.m2/repository as opposed to looking up the <relativePath>../</relativePath>?

In this particular case, it was two separate folders and the parent was not actually located there when I tried this. I should have put <relativePath /> in the child pom (which is the case in our actual projects). I think this behavior is correct, as it matches Maven’s: if the parent pom can’t be found at the (possibly default) relative path, it checks in the repositories (and emits a warning).

DidierLoiseau avatar Oct 24 '24 14:10 DidierLoiseau