diff --git a/rewrite-gradle/src/main/java/org/openrewrite/gradle/UpdateGradleWrapper.java b/rewrite-gradle/src/main/java/org/openrewrite/gradle/UpdateGradleWrapper.java index 10118812886..ffa27201381 100755 --- a/rewrite-gradle/src/main/java/org/openrewrite/gradle/UpdateGradleWrapper.java +++ b/rewrite-gradle/src/main/java/org/openrewrite/gradle/UpdateGradleWrapper.java @@ -22,6 +22,7 @@ import lombok.experimental.FieldDefaults; import lombok.experimental.NonFinal; import org.openrewrite.*; +import org.openrewrite.gradle.search.FindGradleProject; import org.openrewrite.gradle.util.GradleWrapper; import org.openrewrite.internal.ListUtils; import org.openrewrite.internal.StringUtils; @@ -150,6 +151,7 @@ private GradleWrapper getGradleWrapper(ExecutionContext ctx) { } public static class GradleWrapperState { + boolean gradleProject = false; boolean needsWrapperUpdate = false; BuildTool updatedMarker; boolean addGradleWrapperProperties = true; @@ -225,6 +227,10 @@ public boolean isAcceptable(SourceFile sourceFile, ExecutionContext ctx) { return false; } + if (new FindGradleProject(FindGradleProject.SearchCriteria.Marker).getVisitor().visitNonNull(sourceFile, ctx) != sourceFile) { + acc.gradleProject = true; + } + if ((sourceFile instanceof Quark || sourceFile instanceof Remote) && equalIgnoringSeparators(sourceFile.getSourcePath(), WRAPPER_JAR_LOCATION)) { acc.addGradleWrapperJar = false; @@ -253,6 +259,10 @@ public Collection generate(GradleWrapperState acc, ExecutionContext return Collections.emptyList(); } + if (!acc.gradleProject) { + return Collections.emptyList(); + } + if (!(acc.addGradleWrapperJar || acc.addGradleWrapperProperties || acc.addGradleBatchScript || acc.addGradleShellScript)) { return Collections.emptyList(); } diff --git a/rewrite-gradle/src/test/java/org/openrewrite/gradle/UpdateGradleWrapperTest.java b/rewrite-gradle/src/test/java/org/openrewrite/gradle/UpdateGradleWrapperTest.java index 1537063e75c..92a271e7469 100755 --- a/rewrite-gradle/src/test/java/org/openrewrite/gradle/UpdateGradleWrapperTest.java +++ b/rewrite-gradle/src/test/java/org/openrewrite/gradle/UpdateGradleWrapperTest.java @@ -41,6 +41,8 @@ import java.util.regex.Pattern; import static org.assertj.core.api.Assertions.assertThat; +import static org.openrewrite.gradle.Assertions.buildGradle; +import static org.openrewrite.gradle.toolingapi.Assertions.withToolingApi; import static org.openrewrite.gradle.util.GradleWrapper.WRAPPER_BATCH_LOCATION; import static org.openrewrite.gradle.util.GradleWrapper.WRAPPER_JAR_LOCATION; import static org.openrewrite.gradle.util.GradleWrapper.WRAPPER_PROPERTIES_LOCATION; @@ -67,7 +69,8 @@ class UpdateGradleWrapperTest implements RewriteTest { @Override public void defaults(RecipeSpec spec) { - spec.recipe(new UpdateGradleWrapper("7.4.2", null, null, null)); + spec.recipe(new UpdateGradleWrapper("7.4.2", null, null, null)) + .beforeRecipe(withToolingApi()); } @Test @@ -95,7 +98,14 @@ void addGradleWrapper() { assertThat(gradleWrapperJar.getSourcePath()).isEqualTo(WRAPPER_JAR_LOCATION); assertThat(gradleWrapperJar.getUri()).isEqualTo(URI.create("https://services.gradle.org/distributions/gradle-7.4.2-bin.zip")); assertThat(isValidWrapperJar(gradleWrapperJar)).as("Wrapper jar is not valid").isTrue(); - }) + }), + buildGradle( + """ + plugins { + id "java" + } + """ + ) ); } @@ -218,7 +228,10 @@ void updateChecksumAlreadySet() { zipStorePath=wrapper/dists """, spec -> spec.path("gradle/wrapper/gradle-wrapper.properties") - ) + ), + gradlew, + gradlewBat, + gradleWrapperJarQuark ); } @@ -325,7 +338,10 @@ void olderThan6_6() { distributionSha256Sum=1f3067073041bc44554d0efe5d402a33bc3d3c93cc39ab684f308586d732a80d """, spec -> spec.path("gradle/wrapper/gradle-wrapper.properties") - ) + ), + gradlew, + gradlewBat, + gradleWrapperJarQuark ); } @@ -383,7 +399,10 @@ void allowUpdatingDistributionTypeWhenSameVersion() { distributionSha256Sum=1f3067073041bc44554d0efe5d402a33bc3d3c93cc39ab684f308586d732a80d """, spec -> spec.path("gradle/wrapper/gradle-wrapper.properties") - ) + ), + gradlew, + gradlewBat, + gradleWrapperJarQuark ); }