Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ sidebar_label: Running Rewrite on a Gradle project without modifying the build
description: How to run OpenRewrite on a Gradle project without modifying any of the build files.
---

import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';

# Running Rewrite on a Gradle project without modifying the build

In this tutorial, we will apply a Rewrite recipe to a repository built with Gradle without modifying the build itself. We will use a [Gradle init script](https://docs.gradle.org/current/userguide/init_scripts.html) to accomplish this.
Expand All @@ -21,10 +24,13 @@ Extract the zip file and open the project in your favorite editor.
### Step 2: Create a Gradle init script

:::warning
If you want to use an init script you **cannot** have a `rewrite` section in your `build.gradle` file. You must pick one or the other.
If you want to use an init script you **cannot** have a `rewrite` section in your `build.gradle` (or `build.gradle.kts`) file. You must pick one or the other.
:::

Create a `init.gradle` file. It does not need to be in the project directory itself (although it will make it easier for this guide). Copy the below init script to your file:
Create an init script file. It does not need to be in the project directory itself (although it will make it easier for this guide). Copy the below init script to your file:

<Tabs groupId="gradle-dsl">
<TabItem value="groovy" label="Groovy">

```groovy title="init.gradle"
initscript {
Expand Down Expand Up @@ -52,8 +58,39 @@ rootProject {
}
```

</TabItem>
<TabItem value="kotlin" label="Kotlin">

In the `rootProject` block, we specify a dependency that contains OpenRewrite Spring recipes (`rewrite-spring`). If you wanted, you could also define a `rewrite` section inside of the `rootProject` that has elements like `activeRecipe` or `activeStyle`. In general, though, it's better to [use the command line to specify the recipes or styles](#step-4-run-the-recipe) so that you can keep your init script fairly generic.
```kotlin title="init.gradle.kts"
initscript {
repositories {
maven { url = uri("https://plugins.gradle.org/m2") }
}
dependencies {
classpath("org.openrewrite:plugin:latest.release")
}
}

rootProject {
plugins.apply(org.openrewrite.gradle.RewritePlugin::class.java)
dependencies {
add("rewrite", "org.openrewrite.recipe:rewrite-spring:latest.release")
}

afterEvaluate {
if (repositories.isEmpty()) {
repositories {
mavenCentral()
}
}
}
}
```

</TabItem>
</Tabs>

In the `rootProject` block, we specify a dependency that contains OpenRewrite Spring recipes (`rewrite-spring`). If you wanted, you could also define a `rewrite` section inside of the `rootProject` that has elements like `activeRecipe` or `activeStyle`. In general, though, it's better to [use the command line to specify the recipes or styles](#step-4-run-the-recipe) so that you can keep your init script fairly generic.

For a full range of options available, please see our [Gradle plugin configuration doc](../reference/gradle-plugin-configuration.md)

Expand All @@ -77,12 +114,23 @@ recipeList:

At this point, you are able to run the Rewrite Gradle plugin as normal (with an additional `--init-gradle` argument). Note that we did not modify the project's build script. This same init script can then be used to apply recipes to a set of projects cloned locally without changing their contents.

Here is what the Gradle command would look like if you wanted to run the above declarative recipe with the init script you created earlier (assuming you're in the same location as the `init.gradle` file):
Here is what the Gradle command would look like if you wanted to run the above declarative recipe with the init script you created earlier (assuming you're in the same location as the init script file):

<Tabs groupId="gradle-dsl">
<TabItem value="groovy" label="Groovy">

```bash
gradle rewriteRun --init-script init.gradle -Drewrite.activeRecipe=org.openrewrite.FindSpringUses
```

</TabItem>
<TabItem value="kotlin" label="Kotlin">

```bash
gradle rewriteRun --init-script init.gradle.kts -Drewrite.activeRecipe=org.openrewrite.FindSpringUses
```

</TabItem>
</Tabs>

For a full list of options that you can use in the Gradle command line, please see our [Gradle plugin configuration doc](../reference/gradle-plugin-configuration.md#jvm-args-that-can-be-added-to-the-gradle-command-line)
42 changes: 42 additions & 0 deletions src/components/RunRecipe/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,30 @@ rootProject {
}
}`;

// Gradle init script snippet (Kotlin)
const gradleInitKts = `initscript {
repositories {
maven { url = uri("https://plugins.gradle.org/m2") }
}
dependencies { classpath("org.openrewrite:plugin:${gradlePluginVersion}") }
}
rootProject {
plugins.apply(org.openrewrite.gradle.RewritePlugin::class.java)
dependencies {
add("rewrite", "${needsDependency ? `${groupId}:${artifactId}:${version}` : 'org.openrewrite:rewrite-java'}")
}
extensions.configure<org.openrewrite.gradle.RewriteExtension> {
activeRecipe("${activeRecipeName}")${dataTableGradleConfig}
}
afterEvaluate {
if (repositories.isEmpty()) {
repositories {
mavenCentral()
}
}
}
}`;

// Maven POM snippet
const mavenPom = `<project>
<build>
Expand Down Expand Up @@ -221,6 +245,24 @@ rootProject {
</ol>
</TabItem>
)}
{showGradle && !requiresConfiguration && (
<TabItem value="gradle-init-script-kts" label="Gradle init script (Kotlin)">
<ol>
<li>
Create a file named <code>init.gradle.kts</code> in the root of your project.
<CodeBlock language="kotlin" title="init.gradle.kts">
{gradleInitKts}
</CodeBlock>
</li>
<li>
Run the recipe.
<CodeBlock language="shell" title="shell">
gradle --init-script init.gradle.kts rewriteRun
</CodeBlock>
</li>
</ol>
</TabItem>
)}
{showMaven && (
<TabItem value="maven" label="Maven POM">
<ol>
Expand Down