openrewrite / rewrite

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

A way to get all dependency instead ResolvedDependency only #4212

Open abccbaandy opened 1 month ago

abccbaandy commented 1 month ago

What problem are you trying to solve?

I want to remove all dependency which already contain in others, especially for spring manage dependency.

Have you considered any alternatives or workarounds?

I just check the spring exist, but I don't think it's long-term solution.

Additional context

https://github.com/openrewrite/rewrite-migrate-java/issues/481

Are you interested in contributing this feature to OpenRewrite?

Yes, I already start to study the source of DependencyInsight. https://github.com/openrewrite/rewrite-migrate-java/issues/481#issuecomment-2126619941

timtebeek commented 1 month ago

Thanks for the suggestion @abccbaandy ! I think this could be a separate recipe that folks can run to remove direct dependencies that are also available transitively. We already have that dependency insight readily available. A recipe would then have to use that information to strip out any dependencies.

Some care should be taken of course with scopes and optional dependencies, and you could end up with a pom.xml that's more sensitive to changes in transitive dependencies when updating dependency versions. But folks running this recipe likely would know those limitations, and still prefer a minimal pom.xml.

Thanks for the offer to help implement such a recipe! Would you be looking to add this first and only for Maven?

abccbaandy commented 1 month ago

Yes, I already did it, you can see https://github.com/openrewrite/rewrite-migrate-java/issues/481#issuecomment-2126619941 I quote some relate text here.

  1. The DependencyInsight seems not support non-ResolvedDependency. Means when we have direct dependency, the transitive dependency with the same name is gone. ref https://github.com/openrewrite/rewrite/blob/main/rewrite-maven/src/main/java/org/openrewrite/maven/search/DependencyInsight.java#L119 So, I guess this issue cause DependencyInsight onlyTransitive feature not possible.
timtebeek commented 1 month ago

Ah so you're saying that our model currently only gives you the resolved dependency, but you're losing out on querying if that dependency is also available transitively? I could see how that would complicate this recipe if that's the case.