Gradle plugin configuration
rewrite-gradle-plugin configuration options and task descriptions
The OpenRewrite Gradle Plugin is the fastest way to apply OpenRewrite recipes to your code as part of your Gradle build. The OpenRewrite Gradle Plugin is compatible with all versions of Gradle since 4.0.
Apply the org.openrewrite.rewrite plugin to your build.
// OpenRewrite Extension Configuration
// Ensure a repository is declared that the rewrite core libraries can be resolved from
With the plugin applied, the
rewriteDSL is available for configuration.
When applied to a multi-project build, plugin behavior differs depending on whether the plugin is applied to the root project or to a sub-project. Applied to the root project, the plugin will parse and refactor all sources from all projects. Applied to any project other than the root project, the plugin will parse and refactor only sources from that project.
The rewrite gradle plugin resolves the rewrite core libraries and any recipe modules added to the
rewriteconfiguration at runtime. It will attempt to resolve them from whatever repositories are available to the project. This is accomplished by adding Maven Central, or a mirror of it, to your project's repositories:
rewriteDSL exposes a few configuration options:
activeRecipe- Explicitly turns on recipes by name (the name given in the
specs.openrewrite.org/v1beta/reciperesource). No recipe is run unless explicitly turned on with this setting.
activeStyle- Explicitly turns on a style by name (the name given in the
specs.openrewrite.org/v1beta/styleresource). No style is applied unless explicitly turned on with this setting.
configFile- Where to look for a OpenRewrite YML configuration file somewhere in the project directory (or really anywhere on disk). This file is not required to exist. If not specified otherwise, the default value is
failOnDryRunResults- Boolean flag toggling whether
rewriteDryRunshould throw an exception and non-zero exit code if changes are detected. Default is
sizeThresholdMb- Threshold over which non-Java sources are ignored during parsing. Default threshold is 10Mb.
exclusion- One or more paths, relative to the project the plugin is applied to, where non-Java sources are ignored during parsing. Supports glob patterns.
plainTextMasks- A set of file masks to denote which files should be parsed as plain text. Evaluated as a PathMatcher glob pattern, where "**" matches any number of directories and "*" matches a single directory or filename. Exclusions take precedence over any plain text masks. If this configuration is not explicitly defined the default masks are**/META-INF/services/****/.gitignore**/.gitattributes**/.java-version**/.sdkmanrc**/gradlew**/*.sh**/*.bash**/*.bat**/*.ksh**/*.txt**/*.jsp
// Excludes a particular yaml file
// Exclude all json files
// These are default values, shown for example. It isn't necessary to supply these values manually:
configFile = project.getRootProject().file("rewrite.yml")
failOnDryRunResults = false
sizeThresholdMb = 10
All OpenRewrite libraries and modules are published to MavenCentral. Use the
repositoriesGradle DSL to ensure that your build can resolve dependencies from there or one of its mirrors.
No recipe is ever run on your codebase without being explicitly activated in the plugin's configuration. To make pre-packaged OpenRewrite recipes available for activation, add Rewrite's bill of materials along with the specific
Once a pre-packaged recipe has been added to the
rewritedependency configuration, you can tell the Gradle plugin to activate it the
rewriteDSL. For example, here is how you would activate the
org.openrewrite.java.testing.junit5.JUnit5BestPracticesrecipe that comes with
rewrite-testing-frameworksin a single-project Gradle build:
gradle rewriteRunto run the active recipes and apply the changes. This will write changes locally to your source files on disk. Afterward, review the changes, and when you are comfortable with the changes, commit them. The
rungoal generates warnings in the build log wherever it makes changes to source files.
Showing which files were changed and by what visitors
After the goal finishes executing, run
git diff(or your VCS system's equivalent) to see what changes were made, review, and commit them.
Example of changes made to netflix conductor by the rewriteRun task
gradle rewriteDryRunto dry-run the active recipes and print which visitors would make changes to which files to the build log. This does not alter your source files on disk at all. This goal can be used to preview the changes that would be made by the active recipes.
rewriteDryRunoutputs a report in the form of a
patchfile, by default under
build/reports/rewrite/rewrite.patch, containing changes that would be made if you were to run
patchfile can be used with
diffto view or apply the potential changes. For example,
git diff . build/reports/rewrite/rewrite.patch.
Listing of source files that would be changed if rewriteRun were run
rewriteDryRuncan be used as a "gate" in a continuous integration environment by failing the build if
rewriteDryRundetects changes to be made and
failOnDryRunResultsis set to
failOnDryRunResults = true
rewriteDryRuncan be configured so that when
gradle rewriteDiscoverto list the recipes available on your classpath.