Skip to content

Bump plugin version for gradle in buildscript classpath dependency scope#5570

Closed
Jenson3210 wants to merge 2 commits intomainfrom
feature/plugins-buildscript
Closed

Bump plugin version for gradle in buildscript classpath dependency scope#5570
Jenson3210 wants to merge 2 commits intomainfrom
feature/plugins-buildscript

Conversation

@Jenson3210
Copy link
Contributor

What's changed?

Dependency Trait now allows for withVersion to easily bump the version of a Dependency J.MethodInvocation and return the resulting J.MethodInvocation
Plugins can now be updated by the buildscript dependencies also.

What's your motivation?

The plugin dependencies in buildscript are methodinvocations. Rather than copying/gathering stuff from elsewhere I think this approach would allow other recipes to bump a version rather easily. (later perhaps also groups/artifactids?

Checklist

  • I've added unit tests to cover both positive and negative cases
  • I've read and applied the recipe conventions and best practices
  • I've used the IntelliJ IDEA auto-formatter on affected files

@github-project-automation github-project-automation bot moved this to In Progress in OpenRewrite Jun 6, 2025
@Jenson3210 Jenson3210 self-assigned this Jun 6, 2025
@Jenson3210 Jenson3210 added the enhancement New feature or request label Jun 6, 2025
@Jenson3210 Jenson3210 requested a review from shanman190 June 6, 2025 14:33
assert m.getSelect() != null;
List<Expression> pluginArgs = ((J.MethodInvocation) m.getSelect()).getArguments();
if (!(pluginArgs.get(0) instanceof J.Literal)) {
Optional<GradleDependency> gradleDependency = Traits.gradleDependency().get(getCursor());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Optional<GradleDependency> gradleDependency = Traits.gradleDependency().get(getCursor());
Optional<GradleDependency> gradleDependency = Traits.gradleDependency().configuration("classpath").get(getCursor());

Comment on lines +318 to +332
private @Nullable String pluginIdOrNull(J.MethodInvocation m, Optional<GradleDependency> gradleDependency) {
String pluginId = null;
if (m.getSelect() != null) {
List<Expression> pluginArgs = ((J.MethodInvocation) m.getSelect()).getArguments();
if ((pluginArgs.get(0) instanceof J.Literal)) {
pluginId = literalValue(pluginArgs.get(0));
}
} else if (gradleDependency.isPresent()) {
pluginId = gradleDependency.get().getResolvedDependency().getGroupId();
}
if (pluginId == null || !StringUtils.matchesGlob(pluginId, pluginIdPattern)) {
return null;
}
return pluginId;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this only tracks when the imported Gradle dependency happens to be the actual plugin marker dependency and thus pulls in the actual dependency as a transitive.

In a majority of cases where the buildscript block is being utilized, the artifact being imported doesn't share in this quality...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just as an extension, it is for this reason why UpgradeDependencyVersion handles the dependencies section of both the current project and it's buildscript block rather than trying to make UpgradePluginVersion work when it only has the pluginId which may have nearly zero relationship to the classpath dependency declaration.

@Getter
org.openrewrite.gradle.internal.Dependency requestedDependency;

public J.MethodInvocation withVersion(String version) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably return the trait itself in order to continue updating this making the method invocation update to a new cursor which replaces the current one.

See YamlValue for details.

Comment on lines +172 to +175
currentVersion = dependency.getResolvedDependency().getVersion();
if (dependency.getRequestedDependency().getVersion() != null) {
acc.versionAssignmentToPluginId.put(dependency.getRequestedDependency().getVersion(), pluginId);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The resolved dependency will be out of the GradleProject marker, if present, or directly from source if it's not available for some reason. I'm not sure that the requested dependency as a secondary level is needed (read: simplification).

@Jenson3210
Copy link
Contributor Author

Going to close this one as @shanman190 correctly referred me to the UpgradeDependencyVersion for this purpose. Thanks 🙏

@Jenson3210 Jenson3210 closed this Jun 10, 2025
@github-project-automation github-project-automation bot moved this from In Progress to Done in OpenRewrite Jun 10, 2025
@Jenson3210 Jenson3210 deleted the feature/plugins-buildscript branch June 10, 2025 08:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

2 participants