Declarative YAML Format
Declaring and configuring Recipes and Styles in YAML
There are two places OpenRewrite YAML files may appear:
In either case, these YAML files share the same format and purpose: making styles and recipes available to rewrite. A single OpenRewrite YAML file may contain any number of recipes and styles, separated by ---.
Within a YAML document recipe and style names are always fully qualified.
Don't place your custom recipes into the org.openrewrite namespace. We recommend using the same "reverse domain name" naming scheme used in Java packages.

Recipes

The basic format of a recipe defined in YAML is:
1
---
2
type: specs.openrewrite.org/v1beta/recipe
3
name: com.yourorg.RecipeA # A fully qualified, unique name for this recipe
4
displayName: Recipe A # A human-readable name for this recipe
5
description: Applies Recipe B # A human-readable description for this recipe
6
recipeList: # the list of recipes which comprise this recipe
7
- com.yourorg.RecipeB: # A fully qualified name for a recipe defined elsewhere
8
exampleConfig: foo # A configurable parameter of RecipeB
Copied!
Consider this example declarative recipe which uses the built-in ChangeMethodName and ChangePackage recipes to rename a method and a package.
rewrite.yml
1
---
2
type: specs.openrewrite.org/v1beta/recipe
3
name: com.yourorg.SayHello
4
recipeList:
5
- org.openrewrite.java.ChangeMethodName:
6
methodPattern: com.yourorg.HelloWorld sayGoodbye()
7
newMethodName: sayHello
8
- org.openrewrite.java.ChangePackage:
9
oldPackageName: com.yourorg.goodbye
10
newPackageName: com.yourorg.hello
Copied!
To run this recipe:
    1.
    Put the above into a rewrite.yml file at the project root
    2.
    Configure the gradle plugin or maven plugin with com.yourorg.SayHello listed as an active recipe
    3.
    Run the "rewrite:run" maven goal or "rewriteRun" gradle task
Order of recipe declaration is not important. A declarative recipe may include another declarative recipe declared later in the same file in its recipeList.

Styles

The basic format of a Style defined in YAML is:
1
---
2
type: specs.openrewrite.org/v1beta/style
3
name: com.yourorg.StyleA # A fully qualified, unique name for this style
4
displayName: Style A # A human-readable name for this style
5
description: Applies B # A human-readable description for this style
6
styleConfigs: # The list of styles which comprise this style
7
- com.yourorg.StyleB: # The fully qualified name for a style defined elsewhere
8
exampleConfig: foo # A configurable parameter of StyleB
Copied!
While you can define whatever sort of styles you may want, the formatting recipes built into OpenRewrite itself know how to act upon styles built into OpenRewrite. A full reference for such styles will be added to this documentation soon.
Consider this example declarative style, which specifies that tabs should be used for indentation and that at least 9999 imports from a given package should be required before collapsing them into a single star import:
1
---
2
type: specs.openrewrite.org/v1beta/style
3
name: com.yourorg.YesTabsNoStarImports
4
styleConfigs:
5
- org.openrewrite.java.style.TabsAndIndentsStyle:
6
useTabCharacter: true
7
- org.openrewrite.java.style.ImportLayoutStyle:
8
classCountToUseStarImport: 9999
Copied!
To put this style in effect for any formatting performed by OpenRewrite within the current project:
    1.
    Put the above into a rewrite.yml file at the project root
    2.
    Configure the gradle plugin or maven plugin with com.yourorg.YesTabsNoStarImports listed as the active style
The next time any OpenRewrite recipe is run any formatting it performs will take these styles into account.
Last modified 1mo ago
Export as PDF
Copy link