### Run SetupJavaCaching Recipe with Moderne CLI Source: https://docs.openrewrite.org/recipes/github/setupjavacaching These shell commands demonstrate how to execute the `SetupJavaCaching` recipe using the Moderne CLI. The first command shows a direct run, while the second illustrates how to install the recipe artifact if it's not locally available, ensuring dependency caching setup. ```shell mod run . --recipe SetupJavaCaching ``` ```shell mod config recipes jar install org.openrewrite.recipe:rewrite-github-actions:3.11.0 ``` -------------------------------- ### Clone OpenRewrite Sample Project using Git Source: https://docs.openrewrite.org/running-recipes/getting-started Clones the openrewrite/spring-petclinic-migration repository to provide a sample Java project for testing OpenRewrite recipes. This is the initial step before adding OpenRewrite plugins. ```bash git clone https://github.com/openrewrite/spring-petclinic-migration.git ``` -------------------------------- ### Add OpenRewrite Maven Plugin Source: https://docs.openrewrite.org/running-recipes/getting-started Integrates the OpenRewrite Maven plugin into a Maven project by adding a `` configuration to the `pom.xml` file. This allows running OpenRewrite recipes via Maven goals. ```xml org.openrewrite.maven rewrite-maven-plugin 6.18.0 ``` -------------------------------- ### Activate OpenRewrite Recipes in Maven Source: https://docs.openrewrite.org/running-recipes/getting-started Configures active recipes for an OpenRewrite project within a Maven `pom.xml` file. It specifies the 'rewrite-maven-plugin' and lists 'org.openrewrite.java.OrderImports' and 'com.yourorg.VetToVeterinary' within the `` configuration. ```xml org.openrewrite.maven rewrite-maven-plugin 6.18.0 org.openrewrite.java.OrderImports com.yourorg.VetToVeterinary ``` -------------------------------- ### Add OpenRewrite Gradle Plugin (Groovy) Source: https://docs.openrewrite.org/running-recipes/getting-started Configures a Gradle project to use the OpenRewrite plugin by adding it to the `plugins` block and ensuring `mavenCentral()` is in the `repositories` block. A placeholder `rewrite` block is also included. ```gradle plugins { id 'java' id 'maven-publish' id 'org.openrewrite.rewrite' version '7.16.0' } repositories { // The root project doesn't have to be a Java project, but this is necessary // to resolve recipe artifacts. mavenCentral() } rewrite { // Will configure in subsequent steps } // ... ``` -------------------------------- ### Activate OrderImports Recipe in Gradle (Groovy) Source: https://docs.openrewrite.org/running-recipes/getting-started Configures the OpenRewrite plugin in a Gradle Groovy build script to activate the 'org.openrewrite.java.OrderImports' recipe. Ensure the 'org.openrewrite.rewrite' plugin is applied with a compatible version. ```groovy plugins { id 'java' id 'maven-publish' id 'org.openrewrite.rewrite' version '7.16.0' } rewrite { activeRecipe( 'org.openrewrite.java.OrderImports', ) } ``` -------------------------------- ### Activate OrderImports Recipe in Gradle (Kotlin) Source: https://docs.openrewrite.org/running-recipes/getting-started Configures the OpenRewrite plugin in a Gradle Kotlin build script to activate the 'org.openrewrite.java.OrderImports' recipe. Ensure the 'org.openrewrite.rewrite' plugin is applied with a compatible version. ```kotlin plugins { `java-library` `maven-publish` id("org.openrewrite.rewrite") version "7.16.0" } rewrite { activeRecipe( "org.openrewrite.java.OrderImports", ) } ``` -------------------------------- ### Discover OpenRewrite Recipes (Maven/Gradle) Source: https://docs.openrewrite.org/running-recipes/getting-started Executes a command to list all available OpenRewrite recipes. This command can be run using either the Maven wrapper (`mvn rewrite:discover`) or the Gradle wrapper (`gradle rewriteDiscover`). Initially, only built-in recipes are shown. ```bash mvn rewrite:discover ``` ```bash gradle rewriteDiscover ``` -------------------------------- ### Activate OpenRewrite Recipes in Gradle (Kotlin) Source: https://docs.openrewrite.org/running-recipes/getting-started Configures active recipes for an OpenRewrite project using Gradle build scripts written in Kotlin. It activates both 'org.openrewrite.java.OrderImports' and the custom 'com.yourorg.VetToVeterinary' recipe. Ensure the 'org.openrewrite.rewrite' plugin is applied. ```kotlin plugins { `java-library` `maven-publish` id("org.openrewrite.rewrite") version "7.16.0" } rewrite { activeRecipe( "org.openrewrite.java.OrderImports", "com.yourorg.VetToVeterinary" ) } ``` -------------------------------- ### Add OpenRewrite Gradle Plugin (Kotlin) Source: https://docs.openrewrite.org/running-recipes/getting-started Configures a Gradle Kotlin DSL project to use the OpenRewrite plugin. It adds the plugin to the `plugins` block, includes `mavenCentral()` in `repositories`, and sets up an empty `rewrite` block for future configuration. ```kotlin plugins { `java-library` `maven-publish` id("org.openrewrite.rewrite") version "7.16.0" } repositories { // The root project doesn't have to be a Java project, but this is necessary // to resolve recipe artifacts. mavenCentral() } rewrite { // Will configure in subsequent steps } ``` -------------------------------- ### Configure Gradle for OpenRewrite Recipes (Groovy) Source: https://docs.openrewrite.org/running-recipes/getting-started This snippet demonstrates how to configure the OpenRewrite plugin in a Gradle build file using Groovy syntax. It shows how to define active recipes and manage OpenRewrite dependencies, recommending the use of 'rewrite-recipe-bom' for version management. ```groovy plugins { id 'java' id 'maven-publish' id 'org.openrewrite.rewrite' version '7.16.0' } rewrite { activeRecipe( 'org.openrewrite.java.OrderImports', 'com.yourorg.VetToVeterinary', 'org.openrewrite.java.spring.boot2.SpringBoot2JUnit4to5Migration' ) } dependencies { rewrite platform('org.openrewrite.recipe:rewrite-recipe-bom:latest.release') rewrite('org.openrewrite.recipe:rewrite-spring') // Other project dependencies } ``` -------------------------------- ### Configure Gradle for OpenRewrite Recipes (Kotlin) Source: https://docs.openrewrite.org/running-recipes/getting-started This snippet shows the configuration for the OpenRewrite plugin in a Gradle build file using Kotlin syntax. It outlines how to specify active recipes and include OpenRewrite dependencies, with a recommendation to use 'rewrite-recipe-bom' for simplified version handling. ```kotlin plugins { `java-library` `maven-publish` id("org.openrewrite.rewrite") version "7.16.0" } rewrite { activeRecipe( "org.openrewrite.java.OrderImports", "com.yourorg.VetToVeterinary", "org.openrewrite.java.spring.boot2.SpringBoot2JUnit4to5Migration" ) } dependencies { rewrite(platform("org.openrewrite.recipe:rewrite-recipe-bom:latest.release")) rewrite("org.openrewrite.recipe:rewrite-spring") // Other project dependencies } ``` -------------------------------- ### Activate OpenRewrite Recipes in Gradle (Groovy) Source: https://docs.openrewrite.org/running-recipes/getting-started Configures active recipes for an OpenRewrite project using Gradle build scripts written in Groovy. It activates both the built-in 'org.openrewrite.java.OrderImports' recipe and the custom 'com.yourorg.VetToVeterinary' recipe. The 'org.openrewrite.rewrite' plugin must be applied. ```groovy plugins { id 'java' id 'maven-publish' id 'org.openrewrite.rewrite' version '7.16.0' } rewrite { activeRecipe( 'org.openrewrite.java.OrderImports', 'com.yourorg.VetToVeterinary' ) } ``` -------------------------------- ### Activate OrderImports Recipe in Maven Source: https://docs.openrewrite.org/running-recipes/getting-started Configures the OpenRewrite Maven plugin in the `pom.xml` file to activate the 'org.openrewrite.java.OrderImports' recipe. This involves specifying the plugin group, artifact ID, version, and the desired active recipes within the configuration. ```xml org.openrewrite.maven rewrite-maven-plugin 6.18.0 org.openrewrite.java.OrderImports ``` -------------------------------- ### Configure Maven Plugin for OpenRewrite Recipes Source: https://docs.openrewrite.org/running-recipes/getting-started This snippet shows how to configure the OpenRewrite Maven plugin in a pom.xml file. It includes specifying active recipes and adding external module dependencies like 'rewrite-spring'. Ensure the plugin version and dependency versions are set appropriately. ```xml org.openrewrite.maven rewrite-maven-plugin 6.18.0 org.openrewrite.java.OrderImports com.yourorg.VetToVeterinary org.openrewrite.java.spring.boot2.SpringBoot2JUnit4to5Migration org.openrewrite.recipe rewrite-spring 6.15.0 ``` -------------------------------- ### Refaster Template Example for StringIsEmpty Recipe - Java Source: https://docs.openrewrite.org/authoring-recipes/refaster-recipes This Java code defines a Refaster template for the StringIsEmpty recipe. It uses annotations to specify before and after template conditions for refactoring string comparisons. This template refactors checks like string.equals("") or string.length() == 0 to use string.isEmpty(). ```java import com.google.errorprone.refaster.annotation.AfterTemplate; import com.google.errorprone.refaster.annotation.BeforeTemplate; @RecipeDescriptor( name = "Prefer StringIsEmpty", description = "Prefer String#isEmpty() over alternatives that check the string's length." ) public class StringIsEmpty { @BeforeTemplate boolean equalsEmptyString(String string) { return string.equals(""); } @BeforeTemplate boolean lengthEquals0(String string) { return string.length() == 0; } @AfterTemplate boolean optimizedMethod(String string) { return string.isEmpty(); } } ``` -------------------------------- ### Install GitHub Actions Recipe for Moderne CLI Source: https://docs.openrewrite.org/recipes/github/setupjavacaching Installs the rewrite-github-actions recipe artifact for use with the Moderne CLI. ```Shell mod config recipes jar install org.openrewrite.recipe:rewrite-github-actions:3.10.0 ``` -------------------------------- ### IndexOf Checks Use Start Position Source: https://docs.openrewrite.org/changelog/earlier-releases/8-1-2-Release Replaces String.indexOf(String) in binary operations with a start position check when the compared value is an int and not less than 1. ```java import org.openrewrite.java.cleanup.IndexOfChecksShouldUseAStartPosition; // Example usage within a recipe: rewrite.apply(new IndexOfChecksShouldUseAStartPosition()); ``` -------------------------------- ### Install OpenRewrite AI Search JAR Source: https://docs.openrewrite.org/recipes/ai/research/getcodeembedding This command provides instructions to install the 'rewrite-ai-search' JAR file from Maven, which is necessary if the 'GetCodeEmbedding' recipe is not locally available. ```Shell mod config recipes jar install org.openrewrite.recipe:rewrite-ai-search:0.30.0 ``` -------------------------------- ### Install OpenRewrite AI Search recipe JAR Source: https://docs.openrewrite.org/recipes/ai/research/getrecommendations Command to install the OpenRewrite AI Search recipe JAR if it's not available locally, specifying the group ID, artifact ID, and version. ```Shell mod config recipes jar install org.openrewrite.recipe:rewrite-ai-search:0.30.0 ``` -------------------------------- ### Install and Run OpenRewrite Recipe with Moderne CLI Source: https://docs.openrewrite.org/recipes/staticanalysis/noprimitivewrappersfortostringorcompareto This example shows how to install and run an OpenRewrite static analysis recipe using the Moderne CLI. It first provides the command to install the recipe artifact and then the command to execute it. Requires the Moderne CLI to be configured. ```bash mod run . --recipe NoPrimitiveWrappersForToStringOrCompareTo ``` ```bash mod config recipes jar install org.openrewrite.recipe:rewrite-static-analysis:2.18.0 ``` -------------------------------- ### Run OpenRewrite Recipe via Moderne CLI Source: https://docs.openrewrite.org/recipes/org/apache/camel/upgrade/updatepropertiesandyamlkeys This command executes the 'UpdatePropertiesAndYamlKeys' recipe using the Moderne CLI. It assumes the CLI is installed and configured. If the recipe is not found locally, it provides a command to install it. ```shell mod run . --recipe UpdatePropertiesAndYamlKeys ``` ```shell mod config recipes jar install org.openrewrite.recipe:rewrite-third-party:0.28.0 ``` -------------------------------- ### Installing OpenRewrite Codemods with Moderne CLI Source: https://docs.openrewrite.org/recipes/codemods/cleanup/svelte/requirestorereactiveaccess Command to install a specific version of the rewrite-codemods artifact using the Moderne CLI. This is necessary if the recipe is not found locally. ```Shell mod config recipes jar install org.openrewrite.recipe:rewrite-codemods:0.17.0 ``` -------------------------------- ### Install Kubernetes Recipe Jar Source: https://docs.openrewrite.org/recipes/kubernetes/imagepullpolicyalways This command installs the `rewrite-kubernetes` recipe jar, version `3.10.2`. This is necessary if the `ImagePullPolicyAlways` recipe is not found locally in your Moderne CLI setup. It makes the Kubernetes-related recipes available for use. ```shell mod config recipes jar install org.openrewrite.recipe:rewrite-kubernetes:3.10.2 ``` -------------------------------- ### Install OpenRewrite Recipe Jar via Moderne CLI Source: https://docs.openrewrite.org/recipes/ai/timefold/solver/migration/fromoptaplannertotimefoldsolver This command shows how to install a specific OpenRewrite recipe artifact using the Moderne CLI. This is useful when the recipe is not available locally or needs to be updated to a specific version before execution. ```shell mod config recipes jar install org.openrewrite.recipe:rewrite-third-party:0.28.0 ``` -------------------------------- ### Initialize New Project with Maven Source: https://docs.openrewrite.org/authoring-recipes/recipe-development-environment This command initializes a new Maven project using the maven-archetype-quickstart archetype. It allows specifying group ID, artifact ID, and archetype version for project generation. ```bash mvn -B archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.5 ``` -------------------------------- ### Java Cleanup: Atomic Primitive Equals Uses Get Source: https://docs.openrewrite.org/changelog/earlier-releases/8-1-2-Release Converts `AtomicBoolean#equals(Object)`, `AtomicInteger#equals(Object)`, and `AtomicLong#equals(Object)` comparisons from instance equality to value equality using the `get()` method. ```Java org.openrewrite.java.cleanup.AtomicPrimitiveEqualsUsesGet ``` -------------------------------- ### Declarative Recipe Example (YAML) Source: https://docs.openrewrite.org/authoring-recipes/recipe-testing An example of a declarative recipe definition in YAML format. This specifies the recipe's type, name, display name, description, and a list of actions to be performed, such as adding a dependency and changing a type. This file should be placed in src/main/resources/META-INF/rewrite. ```yaml --- type: specs.openrewrite.org/v1beta/recipe name: com.yourorg.UseApacheStringUtils displayName: Use Apache `StringUtils` description: Replace Spring string utilities with Apache string utilities. recipeList: - org.openrewrite.java.dependencies.AddDependency: groupId: org.apache.commons artifactId: commons-lang3 version: latest.release onlyIfUsing: org.springframework.util.StringUtils configuration: implementation - org.openrewrite.java.ChangeType: oldFullyQualifiedTypeName: org.springframework.util.StringUtils newFullyQualifiedTypeName: org.apache.commons.lang3.StringUtils ``` -------------------------------- ### Run OpenRewrite Recipe via Moderne CLI Source: https://docs.openrewrite.org/recipes/io/quarkus/updates/core/quarkus30/renamejavaxservicefiles This command executes an OpenRewrite recipe using the Moderne CLI. The '.' indicates the current directory, and the recipe name is specified. The Moderne CLI must be installed and configured to use this command. An additional command is provided to install the recipe if it's not available locally. ```shell mod run . --recipe RenameJavaxServiceFiles ``` ```shell mod config recipes jar install org.openrewrite.recipe:rewrite-third-party:0.28.0 ``` -------------------------------- ### Run OpenRewrite Dropwizard CoreSetup via Maven Command Line Source: https://docs.openrewrite.org/recipes/java/dropwizard/coresetup Execute the `CoreSetup` recipe using the `rewrite-maven-plugin` directly from the command line. This command downloads necessary dependencies and applies the recipe. Ensure Maven is installed and configured on your machine. ```shell mvn -U org.openrewrite.maven:rewrite-maven-plugin:run -Drewrite.recipeArtifactCoordinates=org.openrewrite.recipe:rewrite-dropwizard:RELEASE -Drewrite.activeRecipes=org.openrewrite.java.dropwizard.CoreSetup -Drewrite.exportDatatables=true ``` -------------------------------- ### Fix Typecast Parenthesis Padding Source: https://docs.openrewrite.org/changelog/earlier-releases/8-1-2-Release Adjusts whitespace padding within typecast parentheses, for example, changing `( int ) 0L;` to `(int) 0L;`. ```Java org.openrewrite.staticanalysis.TypecastParenPad ``` -------------------------------- ### Run OpenRewrite with Moderne CLI Source: https://docs.openrewrite.org/recipes/java/migrate/sunnetsslpackageunavailable This command runs OpenRewrite recipes using the Moderne CLI. The CLI must be installed and configured. This example targets the 'SunNetSslPackageUnavailable' recipe. ```shell mod run . --recipe SunNetSslPackageUnavailable ``` -------------------------------- ### Install OpenRewrite GitHub Actions Recipe JAR Source: https://docs.openrewrite.org/recipes/github/setupjavaadoptopenj9tosemeru This command provides instructions on how to install the OpenRewrite GitHub Actions recipe JAR using the Moderne CLI if it's not available locally. This ensures the recipe can be accessed and executed. ```Shell mod config recipes jar install org.openrewrite.recipe:rewrite-github-actions:3.10.0 ``` -------------------------------- ### Run SetupJavaCaching with Gradle Init Script Source: https://docs.openrewrite.org/recipes/github/setupjavacaching Runs the OpenRewrite recipe using a Gradle init script. ```Shell gradle --init-script init.gradle rewriteRun ``` -------------------------------- ### Moderne CLI Command to Apply Spaces Recipe Source: https://docs.openrewrite.org/recipes/hcl/format/spaces An example command to run the 'Spaces' recipe using the Moderne CLI. Assumes the recipe is available locally or installed. ```shell mod run . --recipe Spaces ``` -------------------------------- ### Run OpenRewrite Recipe with Moderne CLI Source: https://docs.openrewrite.org/recipes/org/apache/camel/upgrade/camel49/debeziumchangetypes This command runs an OpenRewrite recipe using the Moderne CLI. Ensure the CLI is installed and configured. The `--recipe` flag specifies the recipe to execute. ```shell mod run . --recipe DebeziumChangeTypes ``` -------------------------------- ### Gradle Configuration for OpenRewrite Source: https://docs.openrewrite.org/recipes/github/setuppythontouv This snippet shows how to configure the OpenRewrite plugin and add the necessary GitHub Actions recipe dependency in a `build.gradle` file. It activates the `SetupPythonToUv` recipe and enables data table export. Ensure Gradle is installed to use this configuration. ```gradle plugins { id("org.openrewrite.rewrite") version("latest.release") } rewrite { activeRecipe("org.openrewrite.github.SetupPythonToUv") setExportDatatables(true) } repositories { mavenCentral() } dependencies { rewrite("org.openrewrite.recipe:rewrite-github-actions:3.11.0") } ``` -------------------------------- ### Replace indexOf(String) with aStartPosition Source: https://docs.openrewrite.org/changelog/earlier-releases/8-1-2-Release Replaces `indexOf(String)` in binary operations when the compared value is an int and not less than 1. This ensures more accurate comparisons by considering the starting position. ```Java org.openrewrite.java.cleanup.IndexOfChecksShouldUseAStartPosition ``` -------------------------------- ### Configure Gradle Init Script for OpenRewrite Recipe Source: https://docs.openrewrite.org/recipes/github/setupjavaadoptopenj9tosemeru This Gradle init script configures the OpenRewrite plugin and registers the GitHub Actions recipe. It includes repository setup, dependency declaration for the plugin, and recipe activation. The 'afterEvaluate' block ensures repositories are configured if not already present. ```gradle initscript { repositories { maven { url "https://plugins.gradle.org/m2" } } dependencies { classpath("org.openrewrite:plugin:7.16.0") } } rootProject { plugins.apply(org.openrewrite.gradle.RewritePlugin) dependencies { rewrite("org.openrewrite.recipe:rewrite-github-actions:3.11.0") } rewrite { activeRecipe("org.openrewrite.github.SetupJavaAdoptOpenj9ToSemeru") setExportDatatables(true) } afterEvaluate { if (repositories.isEmpty()) { repositories { mavenCentral() } } } } ``` -------------------------------- ### Running OpenRewrite with Moderne CLI Source: https://docs.openrewrite.org/recipes/java/recipes/selectrecipeexamples Executes OpenRewrite recipes using the Moderne command-line interface. This command applies the specified recipe to the current directory. It assumes the Moderne CLI is installed and configured. ```shell mod run . --recipe SelectRecipeExamples ``` -------------------------------- ### Moderne CLI Command for OpenRewrite Recipe Source: https://docs.openrewrite.org/recipes/io/quarkus/updates/core/quarkus30/applicationyml Utilizes the Moderne CLI to run the 'ApplicationYml' recipe. This command is concise and targets the current project directory. It also provides an example of how to install the recipe if it's not available locally. ```shell mod run . --recipe ApplicationYml ``` ```shell mod config recipes jar install org.openrewrite.recipe:rewrite-third-party:0.28.0 ``` -------------------------------- ### Gradle Build Configuration for OpenRewrite Source: https://docs.openrewrite.org/recipes/tech/picnic/errorprone/refasterrules/assertjcharsequencerulesrecipes Configures the OpenRewrite plugin and specifies the AssertJCharSequenceRulesRecipes to be activated in a Gradle project. It also includes adding the necessary OpenRewrite dependency. This setup requires Gradle to be installed. ```gradle plugins { id("org.openrewrite.rewrite") version("latest.release") } rewrite { activeRecipe("tech.picnic.errorprone.refasterrules.AssertJCharSequenceRulesRecipes") setExportDatatables(true) } repositories { mavenCentral() } dependencies { rewrite("org.openrewrite.recipe:rewrite-third-party:0.28.0") } ``` -------------------------------- ### Configure Gradle for ExamplesExtractor Recipe Source: https://docs.openrewrite.org/recipes/java/recipes/examplesextractor This snippet shows how to configure a Gradle build file to activate the ExamplesExtractor recipe. It involves adding the OpenRewrite plugin and specifying the recipe and dependency. The recipe is then run using the `gradle rewriteRun` command. ```gradle plugins { id("org.openrewrite.rewrite") version("latest.release") } rewrite { activeRecipe("org.openrewrite.java.recipes.ExamplesExtractor") setExportDatatables(true) } repositories { mavenCentral() } dependencies { rewrite("org.openrewrite.recipe:rewrite-rewrite:0.13.0") } ``` -------------------------------- ### Update Micronaut Validation Dependencies (Java) Source: https://docs.openrewrite.org/changelog/earlier-releases/8-1-2-Release Adds the Jakarta validation dependency if required, migrates from `javax.validation`, and updates Micronaut validation dependencies to ensure a consistent and current validation setup. ```java org.openrewrite.java.micronaut.UpdateMicronautValidation ``` -------------------------------- ### Run SecurityStarter Recipe using Maven Command Line Source: https://docs.openrewrite.org/recipes/devcenter/securitystarter This command executes the SecurityStarter recipe using the Maven plugin. It includes arguments for updating dependencies, specifying the recipe artifact coordinates, activating the recipe, and enabling data export. ```shell mvn -U org.openrewrite.maven:rewrite-maven-plugin:run -Drewrite.recipeArtifactCoordinates=io.moderne.recipe:rewrite-devcenter:RELEASE -Drewrite.activeRecipes=io.moderne.devcenter.SecurityStarter -Drewrite.exportDatatables=true ``` -------------------------------- ### Setup Spring PetClinic for WebLogic 14.1.2 Source: https://docs.openrewrite.org/changelog/8-50-2-Release Sets up the Spring Framework 5.3.x PetClinic example for WebLogic 14.1.2. This recipe is valuable for developers testing or migrating Spring applications to this WebLogic version. ```Java com.oracle.weblogic.rewrite.examples.spring.SetupSpringFrameworkPetClinicFor1412 ``` -------------------------------- ### Run ExamplesExtractor Recipe via Moderne CLI Source: https://docs.openrewrite.org/recipes/java/recipes/examplesextractor This command line snippet demonstrates running the ExamplesExtractor recipe using the Moderne CLI. It assumes the CLI is installed and configured. The command specifies the target directory and the recipe name. An additional command is provided to install the recipe if it's not available locally. ```shell mod run . --recipe ExamplesExtractor ``` ```shell mod config recipes jar install org.openrewrite.recipe:rewrite-rewrite:0.13.0 ``` -------------------------------- ### Run SetupJavaCaching with Maven Command Line Source: https://docs.openrewrite.org/recipes/github/setupjavacaching Executes the OpenRewrite recipe via the Maven command line, specifying the recipe artifact, active recipes, and data table export. ```Shell mvn -U org.openrewrite.maven:rewrite-maven-plugin:run -Drewrite.recipeArtifactCoordinates=org.openrewrite.recipe:rewrite-github-actions:RELEASE -Drewrite.activeRecipes=org.openrewrite.github.SetupJavaCaching -Drewrite.exportDatatables=true ``` -------------------------------- ### Gradle Init Script for OpenRewrite Source: https://docs.openrewrite.org/recipes/github/setuppythontouv This Gradle init script configures the OpenRewrite plugin, adds the GitHub Actions recipe dependency, and applies the recipe. It's useful for running recipes without modifying the main build file. The script ensures Maven Central is available for dependencies. Requires Gradle installed. ```gradle initscript { repositories { maven { url "https://plugins.gradle.org/m2" } } dependencies { classpath("org.openrewrite:plugin:7.16.0") } } rootProject { plugins.apply(org.openrewrite.gradle.RewritePlugin) dependencies { rewrite("org.openrewrite.recipe:rewrite-github-actions:3.11.0") } rewrite { activeRecipe("org.openrewrite.github.SetupPythonToUv") setExportDatatables(true) } afterEvaluate { if (repositories.isEmpty()) { repositories { mavenCentral() } } } } ``` -------------------------------- ### Gradle Init Script for OpenRewrite Source: https://docs.openrewrite.org/recipes/codemods/migrate/mui/all Sets up the OpenRewrite plugin using an init script for Gradle, enabling the 'Combination of all deprecations' recipe and managing dependencies. ```gradle initscript { repositories { maven { url "https://plugins.gradle.org/m2"} } dependencies {classpath("org.openrewrite:plugin:7.13.0")} } rootProject { plugins.apply(org.openrewrite.gradle.RewritePlugin) dependencies { rewrite("org.openrewrite.recipe:rewrite-codemods:0.17.0") } rewrite { activeRecipe("org.openrewrite.codemods.migrate.mui.All") setExportDatatables(true) } afterEvaluate { if(repositories.isEmpty()){ repositories { mavenCentral() } } } } ``` -------------------------------- ### Configure Gradle Init Script for ExamplesExtractor Recipe Source: https://docs.openrewrite.org/recipes/java/recipes/examplesextractor This snippet demonstrates how to use a Gradle init script to run the ExamplesExtractor recipe. It applies the RewritePlugin and configures the recipe and its dependencies within the script. The recipe is executed using the `gradle --init-script init.gradle rewriteRun` command. ```gradle initscript { repositories { maven { url "https://plugins.gradle.org/m2" } } dependencies { classpath("org.openrewrite:plugin:7.16.0") } } rootProject { plugins.apply(org.openrewrite.gradle.RewritePlugin) dependencies { rewrite("org.openrewrite.recipe:rewrite-rewrite:0.13.0") } rewrite { activeRecipe("org.openrewrite.java.recipes.ExamplesExtractor") setExportDatatables(true) } afterEvaluate { if (repositories.isEmpty()) { repositories { mavenCentral() } } } } ``` -------------------------------- ### Maven POM Configuration for OpenRewrite Source: https://docs.openrewrite.org/recipes/java/migrate/jakarta/javaxbeanvalidationxmltojakartabeanvalidationxml Configures the OpenRewrite Maven plugin in a `pom.xml` file, specifying the active recipe and necessary dependencies. This setup requires Maven to be installed. ```xml org.openrewrite.maven rewrite-maven-plugin 6.18.0 true org.openrewrite.java.migrate.jakarta.JavaxBeanValidationXmlToJakartaBeanValidationXml org.openrewrite.recipe rewrite-migrate-java 3.18.0 ``` -------------------------------- ### Migrate actions/setup-java from adopt-openj9 to semeru (YAML) Source: https://docs.openrewrite.org/recipes/github/setupjavaadoptopenj9tosemeru This snippet demonstrates how to update GitHub Actions workflows to use the 'semeru' distribution for `actions/setup-java` instead of the deprecated 'adopt-openj9'. It targets the `distribution` setting within the `steps` of a `jobs` block. ```yaml jobs: build: steps: - uses: actions/checkout@v2 with: fetch-depth: 0 - name: set-up-jdk-0 uses: actions/setup-java@v2.3.0 with: distribution: "adopt" java-version: "11" - name: set-up-jdk-1 uses: actions/setup-java@v2.3.0 with: distribution: "adopt-hotspot" java-version: "11" - name: set-up-jdk-2 uses: actions/setup-java@v2.3.0 with: distribution: "semeru" java-version: "11" - name: build run: ./gradlew build test ``` ```diff @@ -20,1 +20,1 @@ uses: actions/setup-java@v2.3.0 with: - distribution: "adopt-openj9" + distribution: "semeru" java-version: "11" ``` -------------------------------- ### Run DevCenterStarter Recipe via Maven Command Line (Direct) Source: https://docs.openrewrite.org/recipes/devcenter/devcenterstarter Executes the DevCenterStarter recipe directly using the Maven command line, specifying recipe coordinates and active recipes. Requires Maven to be installed. ```shell mvn -U org.openrewrite.maven:rewrite-maven-plugin:run -Drewrite.recipeArtifactCoordinates=io.moderne.recipe:rewrite-devcenter:RELEASE -Drewrite.activeRecipes=io.moderne.devcenter.DevCenterStarter -Drewrite.exportDatatables=true ``` -------------------------------- ### Local Maven Publishing Command Source: https://docs.openrewrite.org/authoring-recipes/recipe-development-environment This command installs the current Maven project to the local Maven repository. This is a key step for local testing of OpenRewrite recipes before publishing them to a remote artifact repository. ```bash mvn install ``` -------------------------------- ### Replace actions/setup-python with astral-sh/setup-uv Action Source: https://docs.openrewrite.org/recipes/github/setuppythontouv This recipe automatically replaces the 'actions/setup-python' GitHub action with 'astral-sh/setup-uv'. It also transforms associated pip commands to their uv equivalents and enables caching if configured. This aims to leverage UV's speed and integrated features. ```java /* * Copyright 2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * https://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.openrewrite.github; import org.openrewrite.Option; import org.openrewrite.Recipe; import org.openrewrite.YAML; import org.openrewrite.yaml.tools.YamlProcessor; import java.time.Duration; /** * Replace `actions/setup-python` action with `astral-sh/setup-uv` action for faster Python environment setup and dependency management. * * ## Benefits of UV: * - Significantly faster package installation and environment setup * - Built-in dependency resolution and locking * - Integrated caching for improved CI performance * - Drop-in replacement for pip workflows * * ## Transformations applied: * - `actions/setup-python@v5` → `astral-sh/setup-uv@v6` * - `cache: 'pip'` → `enable-cache: 'true'` * - `pip install -r requirements.txt` → `uv sync` (configurable strategy) * - `python -m ` → `uv run ` * - Removes unnecessary `pip install --upgrade pip` steps * * ## Sync strategies: * - `basic`: Basic synchronization (`uv sync`) * - `locked`: Use locked dependencies (`uv sync --locked`) * - `full`: Install all extras and dev dependencies (`uv sync --all-extras --dev`) * * See the [UV GitHub integration guide](https://astral-sh.github.io/uv/configuration#github-actions) for more details. */ public class SetupPythonToUv extends Recipe { @Override public String getDisplayName() { return "Replace `actions/setup-python` with `astral-sh/setup-uv`"; } @Override public String getDescription() { return "Replace `actions/setup-python` action with `astral-sh/setup-uv` action for faster Python environment setup and dependency management."; } @Override public Duration getMinJavaVersion() { return Duration.ofMinutes(1); } /** * The version of the `astral-sh/setup-uv` action to use. */ @Option(displayName = "UV version", description = "Optional. The version of the `astral-sh/setup-uv` action to use. Defaults to `v6`.", valid = {"v6", "v7", "v8"}, // Add future versions here example = "v6") final String uvVersion; /** * Strategy for the `uv sync` command replacement. */ @Option(displayName = "Sync strategy", description = "Optional. Strategy for the `uv sync` command replacement. Valid options: `basic`, `locked`, `full`. Defaults to `basic`.", valid = {"basic", "locked", "full"}, example = "locked") final String syncStrategy; /** * Whether to transform `pip install` commands to `uv` equivalents. */ @Option(displayName = "Transform pip commands", description = "Optional. Whether to transform `pip install` commands to `uv` equivalents. When disabled, only the action itself is replaced. Defaults to `true`.", example = "true") final Boolean transformPipCommands; /** * Whether to automatically convert `cache: 'pip'` to `enable-cache: 'true'` for UV's built-in caching. */ @Option(displayName = "Enable cache", description = "Optional. Whether to automatically convert `cache: 'pip'` to `enable-cache: 'true'` for UV's built-in caching. When disabled, cache settings are left unchanged. Defaults to `true`.", example = "true") final Boolean enableCache; public SetupPythonToUv() { this.uvVersion = "v6"; this.syncStrategy = "basic"; this.transformPipCommands = true; this.enableCache = true; } public SetupPythonToUv(String uvVersion, String syncStrategy, Boolean transformPipCommands, Boolean enableCache) { this.uvVersion = uvVersion; this.syncStrategy = syncStrategy; this.transformPipCommands = transformPipCommands; this.enableCache = enableCache; } @Override protected YAML visitYaml(YAML yaml) { // Check if the file is a GitHub Actions workflow file if (!yaml.getSource().contains("uses: actions/setup-python")) { return yaml; } // Process the YAML file to replace the action and commands return YamlProcessor.visit(yaml, this::processYaml); } private YAML processYaml(YAML yml) { // Replace the setup-python action yml = yml.map(mapping -> { if (mapping.getKey() != null && mapping.getKey().getValue().equals("uses") && mapping.getValue() != null && mapping.getValue().getValue().startsWith("actions/setup-python")) { return mapping.withValue(YAML.string("astral-sh/setup-uv@" + uvVersion)); } return mapping; }); // Optionally transform pip commands and enable cache if (transformPipCommands || enableCache) { yml = yml.map(mapping -> { // Replace cache: 'pip' with enable-cache: 'true' if (enableCache && mapping.getKey() != null && mapping.getKey().getValue().equals("cache") && mapping.getValue() != null && mapping.getValue().getValue().equals("pip")) { return mapping.withKey(YAML.string("enable-cache")).withValue(YAML.string("true")); } // Transform pip install commands if (transformPipCommands && mapping.getKey() != null && mapping.getKey().getValue().equals("run") && mapping.getValue() != null) { String runCommand = mapping.getValue().getValue(); if (runCommand.startsWith("pip install -r requirements.txt") || runCommand.startsWith("pip install .")) { String uvSyncCommand = "uv sync"; if ("locked".equals(syncStrategy)) { uvSyncCommand += " --locked"; } else if ("full".equals(syncStrategy)) { uvSyncCommand += " --all-extras --dev"; } return mapping.withValue(YAML.string(uvSyncCommand)); } else if (runCommand.startsWith("python -m pytest")) { return mapping.withValue(YAML.string("uv run pytest")); } } return mapping; }); } return yml; } } ``` -------------------------------- ### Change SLF4J log level (Java Example) Source: https://docs.openrewrite.org/recipes/java/logging/slf4j/changeloglevel This snippet demonstrates how to change the log level of SLF4J log statements from INFO to DEBUG, specifically for messages starting with 'LaunchDarkly'. It requires the `rewrite-logging-frameworks` dependency. ```java import org.slf4j.Logger; import org.slf4j.LoggerFactory; class Test { private static final Logger log = LoggerFactory.getLogger(Test.class); void test() { log.info("LaunchDarkly Hello"); } } ``` ```java import org.slf4j.Logger; import org.slf4j.LoggerFactory; class Test { private static final Logger log = LoggerFactory.getLogger(Test.class); void test() { log.debug("LaunchDarkly Hello"); } } ``` ```diff @@ -8,1 +8,1 @@ void test() { - log.info("LaunchDarkly Hello"); + log.debug("LaunchDarkly Hello"); } ``` -------------------------------- ### Configure Gradle Init Script for OpenRewrite Source: https://docs.openrewrite.org/recipes/codemods/cleanup/javascript/preferkeyboardeventkey Sets up an init.gradle script to apply the OpenRewrite plugin and configure the PreferKeyboardEventKey recipe for a project. ```Groovy initscript { repositories { maven { url "https://plugins.gradle.org/m2"} } dependencies {classpath("org.openrewrite:plugin:7.13.0")} } rootProject { plugins.apply(org.openrewrite.gradle.RewritePlugin) dependencies { rewrite("org.openrewrite.recipe:rewrite-codemods:0.17.0") } rewrite { activeRecipe("org.openrewrite.codemods.cleanup.javascript.PreferKeyboardEventKey") setExportDatatables(true) } afterEvaluate { if(repositories.isEmpty()){ repositories { mavenCentral() } } } } ``` -------------------------------- ### Maven Configuration for OpenRewrite Source: https://docs.openrewrite.org/recipes/github/setuppythontouv This snippet configures the OpenRewrite Maven plugin in `pom.xml` to activate the `SetupPythonToUv` recipe and export data tables. It also specifies the necessary dependency for the GitHub Actions recipe. Requires Maven installed. ```xml org.openrewrite.maven rewrite-maven-plugin 6.18.0 true org.openrewrite.github.SetupPythonToUv org.openrewrite.recipe rewrite-github-actions 3.11.0 ``` -------------------------------- ### Run C# SA1104/SA1105 Formatting Recipe Source: https://docs.openrewrite.org/recipes/csharp/recipes/stylecop/analyzers/sa1104sa1105sa1104 This command executes the OpenRewrite C# recipe SA1104SA1105SA1104, which formats query clauses to start on new lines when the preceding clause spans multiple lines. It requires the Moderne CLI to be installed. ```Shell mod run .--recipe SA1104SA1105SA1104 ``` -------------------------------- ### Run SetupJavaCaching with Moderne CLI Source: https://docs.openrewrite.org/recipes/github/setupjavacaching Executes the SetupJavaCaching recipe using the Moderne CLI. ```Shell mod run .--recipe SetupJavaCaching ``` -------------------------------- ### Configure Java 9+ Compilation with Maven Source: https://docs.openrewrite.org/authoring-recipes/refaster-recipes This Maven snippet shows how to add the `jakarta.annotation:jakarta.annotation-api` dependency and configure the `maven-compiler-plugin` to pass the `-Arewrite.generatedAnnotation=jakarta.annotation.Generated` argument for projects using Java 9 or higher. ```xml jakarta.annotation jakarta.annotation-api 3.0.0 org.apache.maven.plugins maven-compiler-plugin -Arewrite.generatedAnnotation=jakarta.annotation.Generated ``` -------------------------------- ### Run RecommendedESLintStyling with Gradle Init Script Source: https://docs.openrewrite.org/recipes/codemods/format/recommendedeslintstyling Executes the OpenRewrite recipe using a Gradle init script, applying the RecommendedESLintStyling. ```Shell gradle --init-script init.gradle rewriteRun ``` -------------------------------- ### Configure Java 9+ Compilation with Gradle Source: https://docs.openrewrite.org/authoring-recipes/refaster-recipes This Gradle snippet demonstrates how to add the `jakarta.annotation:jakarta.annotation-api` dependency and configure the Java compiler to use `-Arewrite.generatedAnnotation=jakarta.annotation.Generated` for projects using Java 9 or higher. ```gradle dependencies { implementation("jakarta.annotation:jakarta.annotation-api:3.0.0") } tasks.named("compileJava", JavaCompile).configure { options.compilerArgs.add("-Arewrite.generatedAnnotation=jakarta.annotation.Generated") } ``` -------------------------------- ### Configure Gradle Init Script for OpenRewrite Dropwizard CoreSetup Source: https://docs.openrewrite.org/recipes/java/dropwizard/coresetup Create an `init.gradle` script to apply the OpenRewrite plugin and configure the `CoreSetup` recipe. This approach allows running recipes without modifying the main build file. It includes repository configuration and applies the `RewritePlugin` to the root project. ```gradle initscript { repositories { maven { url "https://plugins.gradle.org/m2" } } dependencies { classpath("org.openrewrite:plugin:7.16.0") } } rootProject { plugins.apply(org.openrewrite.gradle.RewritePlugin) dependencies { rewrite("org.openrewrite.recipe:rewrite-dropwizard:0.6.2") } rewrite { activeRecipe("org.openrewrite.java.dropwizard.CoreSetup") setExportDatatables(true) } afterEvaluate { if (repositories.isEmpty()) { repositories { mavenCentral() } } } } ``` -------------------------------- ### Fix Typecast Paren Pad Whitespace in Java Source: https://docs.openrewrite.org/changelog/earlier-releases/8-1-2-Release Corrects whitespace padding between a typecast type identifier and its enclosing parentheses. For example, it changes `( int ) 0L;` to `(int) 0L;` when configured to remove spacing. ```Java org.openrewrite.java.cleanup.TypecastParenPad ``` -------------------------------- ### Fix Method Parameter Whitespace Padding Source: https://docs.openrewrite.org/changelog/earlier-releases/8-1-2-Release Corrects whitespace padding between a method definition or invocation identifier and its parameter list's opening parenthesis. For example, `someMethodInvocation (x);` becomes `someMethodInvocation(x)`. ```Java org.openrewrite.java.cleanup.MethodParamPad ```