diff --git a/rewrite-gradle/src/main/java/org/openrewrite/gradle/RemoveRedundantDependencyVersions.java b/rewrite-gradle/src/main/java/org/openrewrite/gradle/RemoveRedundantDependencyVersions.java new file mode 100644 index 00000000000..5602d2d0226 --- /dev/null +++ b/rewrite-gradle/src/main/java/org/openrewrite/gradle/RemoveRedundantDependencyVersions.java @@ -0,0 +1,295 @@ +/* + * 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.gradle;
+
+import lombok.EqualsAndHashCode;
+import lombok.Value;
+import org.jspecify.annotations.Nullable;
+import org.openrewrite.*;
+import org.openrewrite.gradle.marker.GradleProject;
+import org.openrewrite.gradle.trait.GradleDependency;
+import org.openrewrite.gradle.util.ChangeStringLiteral;
+import org.openrewrite.gradle.util.Dependency;
+import org.openrewrite.gradle.util.DependencyStringNotationConverter;
+import org.openrewrite.groovy.GroovyIsoVisitor;
+import org.openrewrite.groovy.tree.G;
+import org.openrewrite.internal.ListUtils;
+import org.openrewrite.java.MethodMatcher;
+import org.openrewrite.java.tree.Expression;
+import org.openrewrite.java.tree.J;
+import org.openrewrite.java.tree.JavaType;
+import org.openrewrite.maven.MavenDownloadingException;
+import org.openrewrite.maven.internal.MavenPomDownloader;
+import org.openrewrite.maven.tree.GroupArtifactVersion;
+import org.openrewrite.maven.tree.ResolvedDependency;
+import org.openrewrite.maven.tree.ResolvedPom;
+import org.openrewrite.semver.*;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Optional;
+
+@Value
+@EqualsAndHashCode(callSuper = false)
+public class RemoveRedundantDependencyVersions extends ScanningRecipe
+ * 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.gradle;
+
+import org.junit.jupiter.api.Test;
+import org.openrewrite.DocumentExample;
+import org.openrewrite.test.RecipeSpec;
+import org.openrewrite.test.RewriteTest;
+
+import static org.openrewrite.gradle.Assertions.buildGradle;
+import static org.openrewrite.gradle.toolingapi.Assertions.withToolingApi;
+
+class RemoveRedundantDependencyVersionsTest implements RewriteTest {
+ @Override
+ public void defaults(RecipeSpec spec) {
+ spec.beforeRecipe(withToolingApi())
+ .recipe(new RemoveRedundantDependencyVersions(null, null, null, null));
+ }
+
+ @DocumentExample
+ @Test
+ void literal() {
+ rewriteRun(
+ buildGradle(
+ """
+ plugins {
+ id "java"
+ }
+
+ repositories {
+ mavenCentral()
+ }
+
+ dependencies {
+ implementation(platform("org.springframework.boot:spring-boot-dependencies:3.3.3"))
+ implementation("org.apache.commons:commons-lang3:3.14.0")
+ }
+ """,
+ """
+ plugins {
+ id "java"
+ }
+
+ repositories {
+ mavenCentral()
+ }
+
+ dependencies {
+ implementation(platform("org.springframework.boot:spring-boot-dependencies:3.3.3"))
+ implementation("org.apache.commons:commons-lang3")
+ }
+ """
+ )
+ );
+ }
+
+ @Test
+ void mapEntry() {
+ rewriteRun(
+ buildGradle(
+ """
+ plugins {
+ id "java"
+ }
+
+ repositories {
+ mavenCentral()
+ }
+
+ dependencies {
+ implementation(platform("org.springframework.boot:spring-boot-dependencies:3.3.3"))
+ implementation(group: "org.apache.commons", name: "commons-lang3", version: "3.14.0")
+ }
+ """,
+ """
+ plugins {
+ id "java"
+ }
+
+ repositories {
+ mavenCentral()
+ }
+
+ dependencies {
+ implementation(platform("org.springframework.boot:spring-boot-dependencies:3.3.3"))
+ implementation(group: "org.apache.commons", name: "commons-lang3")
+ }
+ """
+ )
+ );
+ }
+
+ @Test
+ void mapLiteral() {
+ rewriteRun(
+ buildGradle(
+ """
+ plugins {
+ id "java"
+ }
+
+ repositories {
+ mavenCentral()
+ }
+
+ dependencies {
+ implementation(platform("org.springframework.boot:spring-boot-dependencies:3.3.3"))
+ implementation([group: "org.apache.commons", name: "commons-lang3", version: "3.14.0"])
+ }
+ """,
+ """
+ plugins {
+ id "java"
+ }
+
+ repositories {
+ mavenCentral()
+ }
+
+ dependencies {
+ implementation(platform("org.springframework.boot:spring-boot-dependencies:3.3.3"))
+ implementation([group: "org.apache.commons", name: "commons-lang3"])
+ }
+ """
+ )
+ );
+ }
+
+ @Test
+ void enforcedPlatform() {
+ rewriteRun(
+ buildGradle(
+ """
+ plugins {
+ id "java"
+ }
+
+ repositories {
+ mavenCentral()
+ }
+
+ dependencies {
+ implementation(enforcedPlatform("org.springframework.boot:spring-boot-dependencies:3.3.3"))
+ implementation("org.apache.commons:commons-lang3:3.14.0")
+ }
+ """,
+ """
+ plugins {
+ id "java"
+ }
+
+ repositories {
+ mavenCentral()
+ }
+
+ dependencies {
+ implementation(enforcedPlatform("org.springframework.boot:spring-boot-dependencies:3.3.3"))
+ implementation("org.apache.commons:commons-lang3")
+ }
+ """
+ )
+ );
+ }
+
+ @Test
+ void platformUsingMapEntry() {
+ rewriteRun(
+ buildGradle(
+ """
+ plugins {
+ id "java"
+ }
+
+ repositories {
+ mavenCentral()
+ }
+
+ dependencies {
+ implementation(enforcedPlatform(group: "org.springframework.boot", name: "spring-boot-dependencies", version: "3.3.3"))
+ implementation("org.apache.commons:commons-lang3:3.14.0")
+ }
+ """,
+ """
+ plugins {
+ id "java"
+ }
+
+ repositories {
+ mavenCentral()
+ }
+
+ dependencies {
+ implementation(enforcedPlatform(group: "org.springframework.boot", name: "spring-boot-dependencies", version: "3.3.3"))
+ implementation("org.apache.commons:commons-lang3")
+ }
+ """
+ )
+ );
+ }
+
+ @Test
+ void freestandingScript() {
+ rewriteRun(
+ buildGradle(
+ """
+ repositories {
+ mavenCentral()
+ }
+
+ dependencies {
+ implementation(platform("org.springframework.boot:spring-boot-dependencies:3.3.3"))
+ implementation("org.apache.commons:commons-lang3:3.14.0")
+ }
+ """,
+ """
+ repositories {
+ mavenCentral()
+ }
+
+ dependencies {
+ implementation(platform("org.springframework.boot:spring-boot-dependencies:3.3.3"))
+ implementation("org.apache.commons:commons-lang3")
+ }
+ """,
+ spec -> spec.path("dependencies.gradle")
+ ),
+ buildGradle(
+ """
+ plugins {
+ id("java")
+ }
+
+ apply from: 'dependencies.gradle'
+ """
+ )
+ );
+ }
+}