openrewrite / rewrite

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

openrewrite maven recipes shoudn't try to resolve every single pom #4687

Open adastraperangusta opened 3 days ago

adastraperangusta commented 3 days ago

Hello,

I have projects with external dependencies that have been installed manually into a local maven repository. These artefacts don't have a pom in the repository. On these projects 'mvn clean install' succeeds and there is only a warning about the missing pom :

[WARNING] The POM for xxxxxxx is missing, no dependency information available

However when I try to run any openrewrite maven recipes on this kind of project, it fails with :

[ERROR] </project>org.openrewrite.maven.MavenDownloadingException: Unable to download POM:xxxxx Tried repositories:
[ERROR] https://repo.maven.apache.org/maven2: HTTP 404
[ERROR]   org.openrewrite.maven.internal.MavenPomDownloader.download(MavenPomDownloader.java:636)
[ERROR]   org.openrewrite.maven.tree.ResolvedPom.resolveDependencies(ResolvedPom.java:929)
[ERROR]   org.openrewrite.maven.tree.ResolvedPom.resolveDependencies(ResolvedPom.java:851)
[ERROR]   org.openrewrite.maven.tree.MavenResolutionResult.resolveDependencies(MavenResolutionResult.java:175)

Why openrewrite maven recipes would fail when a maven build succeeds with warnings ? How to fix this ?

You can reproduce this bug with this minimal project : project.zip

Just install any jar locally with :

mvn install:install-file -Dfile=<any jar file> -DgroupId=bug.report -DartifactId=somedep -Dversion=1.0 -Dpackaging=jar -DgeneratePom=false

Then 'mvn clean install' will give :

[INFO] Scanning for projects...
[INFO] 
[INFO] -------------------------< bug.report:somejar >-------------------------
[INFO] Building somejar 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[WARNING] The POM for bug.report:somedep:jar:1.0 is missing, no dependency information available
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ somejar ---
[INFO] Deleting /home/stef/repos_github/bugreport/target
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ somejar ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /home/stef/repos_github/bugreport/src/main/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ somejar ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file to /home/stef/repos_github/bugreport/target/classes
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ somejar ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /home/stef/repos_github/bugreport/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ somejar ---
[INFO] No sources to compile
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ somejar ---
[INFO] No tests to run.
[INFO] 
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ somejar ---
[INFO] Building jar: /home/stef/repos_github/bugreport/target/somejar-1.0-SNAPSHOT.jar
[INFO] 
[INFO] --- maven-install-plugin:2.4:install (default-install) @ somejar ---
[INFO] Installing /home/stef/repos_github/bugreport/target/somejar-1.0-SNAPSHOT.jar to /home/stef/.m2/repository/bug/report/somejar/1.0-SNAPSHOT/somejar-1.0-SNAPSHOT.jar
[INFO] Installing /home/stef/repos_github/bugreport/pom.xml to /home/stef/.m2/repository/bug/report/somejar/1.0-SNAPSHOT/somejar-1.0-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  1.105 s
[INFO] Finished at: 2024-11-19T21:36:09+01:00

Then running 'mvn org.openrewrite.maven:rewrite-maven-plugin:run -Drewrite.activeRecipes=org.openrewrite.maven.cleanup.PrefixlessExpressions`will fail with :

[ERROR] Failed to execute goal org.openrewrite.maven:rewrite-maven-plugin:5.45.0:run (default-cli) on project somejar: Failed to parse or resolve the Maven POM file or one of its dependencies; We can not reliably continue without this information.: Failed to parse or resolve the Maven POM file or one of its dependencies; We can not reliably continue without this information. 
[ERROR] <!--~~(Unable to download POM: bug.report:somedep:1.0. Tried repositories:
[ERROR] https://repo.maven.apache.org/maven2: HTTP 404)~~>--><project xmlns="http://maven.apache.org/POM/4.0.0"
[ERROR]          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
[ERROR]          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
[ERROR]     <modelVersion>4.0.0</modelVersion>
[ERROR] 
[ERROR]     <groupId>bug.report</groupId>
[ERROR]     <artifactId>somejar</artifactId>
[ERROR]     <version>1.0-SNAPSHOT</version>
[ERROR] 
[ERROR]     <properties>
[ERROR]         <maven.compiler.source>1.8</maven.compiler.source>
[ERROR]         <maven.compiler.target>1.8</maven.compiler.target>
[ERROR]     </properties>
[ERROR] 
[ERROR]     <dependencies>
[ERROR]         <!-- any jar installed with :
[ERROR]         mvn install:install-file -Dfile=<any jar file> -DgroupId=bug.report -DartifactId=somedep -Dversion=1.0 -Dpackaging=jar -DgeneratePom=false
[ERROR]         -->
[ERROR]         <dependency>
[ERROR]             <groupId>bug.report</groupId>
[ERROR]             <artifactId>somedep</artifactId>
[ERROR]             <version>1.0</version>
[ERROR]         </dependency>
[ERROR]     </dependencies>
[ERROR] 
[ERROR]     <build>
[ERROR]         <plugins>
[ERROR]             <plugin>
[ERROR]                 <groupId>org.apache.maven.plugins</groupId>
[ERROR]                 <artifactId>maven-compiler-plugin</artifactId>
[ERROR]                 <version>3.8.1</version>
[ERROR]             </plugin>
[ERROR]         </plugins>
[ERROR]     </build>
[ERROR] </project>org.openrewrite.maven.MavenDownloadingException: Unable to download POM: bug.report:somedep:1.0. Tried repositories:
[ERROR] https://repo.maven.apache.org/maven2: HTTP 404
[ERROR]   org.openrewrite.maven.internal.MavenPomDownloader.download(MavenPomDownloader.java:636)
[ERROR]   org.openrewrite.maven.tree.ResolvedPom.resolveDependencies(ResolvedPom.java:929)
[ERROR]   org.openrewrite.maven.tree.ResolvedPom.resolveDependencies(ResolvedPom.java:851)
[ERROR]   org.openrewrite.maven.tree.MavenResolutionResult.resolveDependencies(MavenResolutionResult.java:175)
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
timtebeek commented 2 days ago

Thanks for the clear steps to reproduce @adastraperangusta ! Indeed it seems Maven is more tolerant as compared to our own parser. We had added this check a while back for cases where there was neither a pom.xml nor a local .jar file, as that would lead to missing type information in our models that made it hard to troubleshoot the inevitable missed recipe changes. It seems we could relax that somewhat again provided the .jar is available. That would still not be ideal from our perspective, but would allow more projects to run. Any help towards that would be appreciated!