Maven plugin configuration | OpenRewrite Docs (original) (raw)

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

tip

The OpenRewrite 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 OpenRewrite Maven plugin offers these goals:

info

rewrite name-spaced properties can be used for substituting plugin properties:

info

The goals rewrite:run and rewrite:dryRun are configured to fork Maven's life cycle and are a better choice when running recipes via a stand-alone goal (mvn rewrite:run) because this will trigger all the necessary life-cycle goals prior to running rewrite's plugin. However, when using rewrite within the context of an integration build (mvn deploy rewrite:run) it may be more efficient to use the non-forking variants, as these will not cause duplicate life cycle phases to be called.

info

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. You may also find it beneficial to check out the auto-generated rewrite-maven-plugin documentation which contains type information among other things.

**/META-INF/services/**  
**/META-INF/spring.factories  
**/META-INF/spring/**  
**/*.bash  
**/*.bat  
**/CODEOWNERS  
**/*.config  
**/Dockerfile  
**/.gitattributes  
**/.gitignore  
**/.java-version  
**/*.jsp  
**/*.ksh  
**/*.md  
**/*.qute.java  
**/.sdkmanrc  
**/*.sh  
**/*.sql  
**/*.txt  

info

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

pom.xml

<project>
  <build>
    <plugins>
      <plugin>
        <groupId>org.openrewrite.maven</groupId>
        <artifactId>rewrite-maven-plugin</artifactId>
        <version>6.41.0</version>
        <configuration>
          <activeRecipes>
            <recipe>org.openrewrite.java.Spring</recipe>
          </activeRecipes>
          <activeStyles>
            <!-- This style is made up for sake of example. It isn't packaged with OpenRewrite -->
            <style>com.yourorg.SpringStyle</style>
          </activeStyles>
          <!-- These are default values, shown for example. It isn't necessary to supply these values manually: -->
          <configLocation>${maven.multiModuleProjectDirectory}/rewrite.yml</configLocation>
          <failOnDryRunResults>false</failOnDryRunResults>
          <exclusions>
            <exclusion>*/some/irrelevant/or/expensive/directory/**</exclusion>
          </exclusions>
          <additionalPlainTextMasks>
            <additionalPlainTextMask>**/.custom-ext</additionalPlainTextMask>
            <additionalPlainTextMask>**/.config-file</additionalPlainTextMask>
          </additionalPlainTextMasks>
          <plainTextMasks>
            <plainTextMask>**/.txt</plainTextMask>
          </plainTextMasks>
        </configuration>
        <dependencies>
          <!-- This module is made up for sake of example. It isn't packaged with OpenRewrite -->
          <dependency>
            <groupId>com.yourorg.recipes</groupId>
            <artifactId>your-recipes</artifactId>
            <version>1.1.0</version>
          </dependency>
        </dependencies>
      </plugin>
    </plugins>
  </build>
</project>

info

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

Execute mvn rewrite:typetable to generate a type table to support multiple versions of a single library.

Execute mvn rewrite:recipeCsvGenerate to generate a recipes.csv marketplace file from the recipes found in this project. This is the Maven equivalent of the Gradle recipeCsvGenerate task provided by the OpenRewrite Gradle build plugin.

The goal scans compiled classes and resources in target/classes/ for recipe definitions (both Java class-based and YAML declarative recipes) and writes the result to src/main/resources/META-INF/rewrite/recipes.csv. If an existing recipes.csv is present, generated data is merged into it, preserving any manually added entries such as custom category assignments.

The goal automatically forks the Maven lifecycle to process-classes, so classes and resources will be compiled before scanning.

Execute mvn rewrite:run to 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 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 goal

Execute mvn 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 at all. This goal can be used to preview the changes that would be made by a recipe.

rewrite:dryRun outputs a report in the form of a patch file, by default under target/site/rewrite/rewrite.patch, containing changes that would be made if you were to run rewrite:run. This file can be used with git or diff to view or apply the potential changes. For example, git diff . target/site/rewrite/rewrite.patch.

Example rewrite.patch file produced by dryRun

dryRun can be used as a "gate" in a continuous integration environment by failing the build if dryRun detects changes to be made and failOnDryRunResults is set to true:

<configuration>
  <failOnDryRunResults>true</failOnDryRunResults>
</configuration>

If desired, dryRun can be configured to bind dryRun to desired maven phases. For example, the following configuration is sufficient to have mvn verify also run rewrite:dryRun:

pom.xml

<!-- truncating the pom.xml for brevity -->
<project>
  <build>
    <plugins>
      <plugin>
        <artifactId>rewrite-maven-plugin</artifactId>
        <configuration>...</configuration>
        <!-- this 'executions' block is the important part of binding the dryRun goal to the verify phase -->
        <executions>
          <execution>
            <goals>
              <goal>dryRun</goal>
            </goals>
            <phase>verify</phase>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

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

The beginning of rewrite output, showing activated and available Recipes

The discover goal supports several parameters:

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

Rewrite's pom cache

Rewrite's maven parser has its own resolution logic for pom files. (It will download metadata and poms from maven repositories). To speed up this process, there is a caching abstraction with two implementations (one that uses RocksDB and one that uses an in-memory cache to reduce the number of times a pom/metadata is downloaded from a repository.

The Maven plugin uses the RocksDB cache by default and will store this cache in ~/.rewrite-cache . RocksDB is a high-performance, embedded database that is maintained by Facebook. The database engine is written in C++ and is used by the plugin via a JNI wrapper that has no transitive dependencies.

Cache troubleshooting

In the very rare cases where the Maven plugin is unable to write to its on-disk folder or you encounter a serialization issue when reading/writing to the cache, there are several options available: