Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update RewritePlugin to load tasks lazily #326

Merged

Conversation

mattbertolini
Copy link
Contributor

@mattbertolini mattbertolini commented Aug 21, 2024

What's changed?

The RewritePlugin was changed to call the register() method instead of the create() method for the three tasks. This method takes a configuration lambda where the existing configuration can be done. Next, the tasks' usages were replaced with the equivalent TaskProvider reference. Finally, fetching the appropriate JavaCompile tasks was updated to use a lazy load method (named()), so those tasks are not eagerly created. The plugin's Gradle version was unchanged and should still support Gradle 4.10.

What's your motivation?

The current structure of RewritePlugin eagerly loads the RewriteRun, RewriteDryRun, and RewriteDiscover tasks. This is a problem in Gradle builds with a large number of modules, as it has to create hundreds or even thousands of tasks during configuration. This will happen even if the tasks are not used in that run. This commit updates the plugin by lazily loading these tasks to avoid this issue.

Motivation
----------

The current structure of RewritePlugin eagerly loads the RewriteRun, RewriteDryRun, and RewriteDiscover tasks. This is a problem in Gradle builds with a large number of modules, as it has to create hundreds or even thousands of tasks during configuration. This will happen even if the tasks are not used in that run. This commit updates the plugin by lazily loading these tasks to avoid this issue.

Modifications
-------------

The RewritePlugin was changed to call the register() method instead of the create() method for the three tasks. This method takes a configuration lambda where the existing configuration can be done. Next, the tasks' usages were replaced with the equivalent TaskProvider reference. Finally, fetching the appropriate JavaCompile tasks was updated to use a lazy load method (named()), so those tasks are not eagerly created. The plugin's Gradle version was unchanged and should still support Gradle 4.10.

Result
------

The result is a RewritePlugin that no longer eagerly loads its tasks. It is now safe to use in very large builds.
Copy link
Collaborator

@shanman190 shanman190 left a comment

Choose a reason for hiding this comment

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

Thanks for the contribution, @mattbertolini!

Confirmed that all methods are available in Gradle 4.10.

@timtebeek timtebeek self-requested a review August 21, 2024 07:58
@timtebeek timtebeek added the enhancement New feature or request label Aug 21, 2024
@timtebeek timtebeek merged commit 048ab77 into openrewrite:main Aug 21, 2024
1 check passed
@timtebeek
Copy link
Contributor

Thanks a lot @mattbertolini, not only for the code change, but also for the detailed background here and in #288, as well as your patience waiting for #228 to land before coming back here with the fix.

@mattbertolini mattbertolini deleted the mbertolini/lazy-load-tasks branch August 21, 2024 11:46
@mattbertolini
Copy link
Contributor Author

Happy to help!

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.

Plugin Creates Tasks Immediately Rather Than Lazily in Configuration or Execution Phases
3 participants