Maven Plugin Configuration

rewrite-maven-plugin configuration options and goal descriptions

The Rewrite Maven Plugin is the fastest way to apply Rewrite recipes to your code as part of your Maven build. The Rewrite Maven Plugin is compatible with all versions of Maven since 3.6.

The Rewrite Maven plugin automatically supplies any recipes you configure to run with all source files in the project that are transformable. It will supply Java visitors with Java files, XML visitors with XML files, etc.

The Rewrite Maven plugin offers these goals:

  • mvn rewrite:run - Runs the configured recipes and applies the changes locally.

  • mvn rewrite:dryRun - Generates warnings in the console for any recipes that would suggest changes, but doesn't make any changes.

  • mvn rewrite:discover - Generate a report showing the available and applied recipes based on what Rewrite finds on your classpath.

  • mvn rewrite:cyclonedx - Generate a CycloneDx bill of materials outlining all of the project's dependencies, including transitive dependencies.

In some circumstances, depending on how your project pom.xml is configured, you may get a NoPluginFoundForPrefixException. The root cause for this varies from pom.xml to pom.xml. In any case, as a quick fix to get unstuck, try referencing the fully-qualified rewrite-maven-plugin coordinates instead of the shorthand prefix. That is, try using mvn org.openrewrite.maven:rewrite-maven-plugin:GOAL (such as mvn org.openrewrite.maven:rewrite-maven-plugin:run, etc.) rather than mvn rewrite:GOAL.

Plugin configuration

It generally makes sense to apply the plugin to the root pom.xml in a repository so that the configuration applies to each project in a multi-module project. The configuration block below contains an example of every configuration option.

  • activeRecipes - Explicitly turns on recipes by name (the name given in the resource). No recipe is run unless explicitly turned on with this setting.

  • activeStyles - Explicitly turns on a style by name (the name given in the resource). No style is applied unless explicitly turned on with this setting.

  • configLocation - Where to look for a Rewrite YML configuration file somewhere in the project directory (or really anywhere on disk). If you want to customize this, prefixing the file name with the Maven property ${maven.multiModuleProjectDirectory} is a handy way of ensuring that each module resolves the same configuration file relative to the root directory of the repository. This configLocation is (unless an absolute path is given) evaluated for each module relative to that module's project directory.

  • dependencies - To make pre-packaged Rewrite recipes available to the Maven plugin, add them as plugin dependencies.

Note. the plugin scans the compile, provided, and test scopes for visitors, recipes, and styles and will automatically discover recipes on the project classpath.

<!-- This style is made up for sake of example. It isn't packaged with Rewrite -->
<!-- These are the default values. It is not necessary to supply these value manually -->
<!-- This module is made up for sake of example. It isn't packaged with Rewrite -->

To find out what recipes a rewrite module provides, see its documentation and the output of the rewrite:discover goal.

The "Run" Goal

Executemvn rewrite:run to run the active recipes and apply the changes. This will write changes locally to your source files on disk. Afterwards, review the changes, and when you are comfortable with the changes, commit them. The run goal generates warnings in the build log wherever it makes changes to source files.

Warnings showing which files were changed and by what visitors

After the goal finishes executing, run git diff to see what changes were made, review, and commit them.

An example of changes made to spring-cloud/spring-cloud-sleuth the rewrite:run goal

The "DryRun" Goal

Executemvn rewrite:dryRun to 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 a recipe.

It could also be manually called in a continuous integration environment, and if you so choose, fail the continuous integration build if the build log contains any such warnings.

The "Discover" Goal

Execute mvn rewrite:discover to list the recipes that the Rewrite Maven plugin has found on your classpath and the recipes that you have activated in your plugin configuration.

The beginning of rewrite:discover output, showing activated and available Recipes

This same goal also lists all of the parameters that recipes can be or are already configured with.

Recipes showing their configurable parameters. "!" means a parameter is required.

The "CycloneDx" Goal

Execute rewrite:cyclonedx to generate a CycloneDx bill of materials (BOM) outlining all of the project's dependencies, including transitive dependencies. The BOM will be written to target/<module name>-<version>-cyclonedx.xml.

Excerpt from Rewrite-generated CycloneDx BOM