From 110db88757c2d25a4c6a0b0bd8c9df02ca45656a Mon Sep 17 00:00:00 2001 From: min Date: Fri, 24 May 2024 16:57:06 +0800 Subject: [PATCH] Remove Jakarta annotation dependency when moving to Jakarta packages (#487) * draft for RemoveJakartaAnnotationDependency * Apply suggestions from code review * add negative test * find spring boot only This is a workaround, reason:https://github.com/openrewrite/rewrite-migrate-java/issues/481#issuecomment-2126619941 * Minor polish --------- Co-authored-by: Tim te Beek Co-authored-by: Tim te Beek --- .../META-INF/rewrite/jakarta-ee-9.yml | 15 ++ .../migrate/jakarta/JavaxToJakartaTest.java | 184 ++++++++++++++---- 2 files changed, 163 insertions(+), 36 deletions(-) diff --git a/src/main/resources/META-INF/rewrite/jakarta-ee-9.yml b/src/main/resources/META-INF/rewrite/jakarta-ee-9.yml index f08d7048ff..c10daacdeb 100644 --- a/src/main/resources/META-INF/rewrite/jakarta-ee-9.yml +++ b/src/main/resources/META-INF/rewrite/jakarta-ee-9.yml @@ -61,6 +61,8 @@ recipeList: - org.openrewrite.java.migrate.jakarta.UpdateApacheWSSecurityPackages - org.openrewrite.java.migrate.javaee8 - org.openrewrite.java.migrate.jakarta.JavaxEEApiToJakarta + - org.openrewrite.java.migrate.jakarta.RemoveJakartaAnnotationDependency + --- type: specs.openrewrite.org/v1beta/recipe name: org.openrewrite.java.migrate.jakarta.JavaxActivationMigrationToJakartaActivation @@ -988,3 +990,16 @@ recipeList: groupId: jakarta.platform artifactId: "*" newVersion: 9.0.0 +--- +type: specs.openrewrite.org/v1beta/recipe +name: org.openrewrite.java.migrate.jakarta.RemoveJakartaAnnotationDependency +displayName: Remove `jakarta.annotation-api` dependency when managed by Spring Boot +description: Counteract the `jakarta.annotation-api` added by `org.openrewrite.java.migrate.javax.AddCommonAnnotationsDependencies` for Spring Boot applications. +preconditions: + - org.openrewrite.java.dependencies.DependencyInsight: + groupIdPattern: org.springframework.boot + artifactIdPattern: spring-boot-starter +recipeList: + - org.openrewrite.java.dependencies.RemoveDependency: + groupId: jakarta.annotation + artifactId: jakarta.annotation-api \ No newline at end of file diff --git a/src/test/java/org/openrewrite/java/migrate/jakarta/JavaxToJakartaTest.java b/src/test/java/org/openrewrite/java/migrate/jakarta/JavaxToJakartaTest.java index d9cc086456..9afbaa2e58 100644 --- a/src/test/java/org/openrewrite/java/migrate/jakarta/JavaxToJakartaTest.java +++ b/src/test/java/org/openrewrite/java/migrate/jakarta/JavaxToJakartaTest.java @@ -437,47 +437,48 @@ void projectWithSpringBootStarterWeb() { spec -> spec.parser(JavaParser.fromJavaVersion().dependsOn(javaxServlet)), mavenProject( "Sample", + //language=xml pomXml( """ - - - 4.0.0 - + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.7.6 + + + com.example + demo + 0.0.1-SNAPSHOT + demo + Demo project for Spring Boot + + 17 + + + + jakarta.servlet + jakarta.servlet-api + + org.springframework.boot - spring-boot-starter-parent - 2.7.6 - - - com.example - demo - 0.0.1-SNAPSHOT - demo - Demo project for Spring Boot - - 17 - - - - jakarta.servlet - jakarta.servlet-api - - - org.springframework.boot - spring-boot-starter-web - - + spring-boot-starter-web + + - - - - org.springframework.boot - spring-boot-maven-plugin - - - + + + + org.springframework.boot + spring-boot-maven-plugin + + + - + """ ), srcMainJava( @@ -498,4 +499,115 @@ public class TestApplication { ) ); } + + @Test + void projectWithSpringBoot3StarterWebShouldRemoveJakartaDependency() { + rewriteRun( + spec -> spec.parser(JavaParser.fromJavaVersion().dependsOn(javaxServlet)), + mavenProject( + "Sample", + //language=xml + pomXml( + """ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 3.2.5 + + + com.example + demo + 0.0.1-SNAPSHOT + + + jakarta.annotation + jakarta.annotation-api + 1.3.5 + + + org.springframework.boot + spring-boot-starter-web + + + + """, + """ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 3.2.5 + + + com.example + demo + 0.0.1-SNAPSHOT + + + org.springframework.boot + spring-boot-starter-web + + + + """ + ), + srcMainJava( + //language=java + java( + """ + import jakarta.servlet.A; + public class TestApplication { + } + """ + ) + ) + ) + ); + } + + @Test + void doNothingIfNotFoundTransitiveDependency() { + rewriteRun( + spec -> spec.parser(JavaParser.fromJavaVersion().dependsOn(javaxServlet)), + mavenProject( + "Sample", + //language=java + srcMainJava( + java( + """ + import jakarta.servlet.A; + public class TestApplication { + } + """ + ) + ), + //language=xml + pomXml( + """ + + + 4.0.0 + org.sample + sample + 1.0.0 + + + jakarta.annotation + jakarta.annotation-api + 1.3.5 + + + + """ + ) + ) + ); + } }