diff --git a/build.gradle.kts b/build.gradle.kts index 7a5213e84e..81a5e8716e 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -51,5 +51,6 @@ dependencies { testRuntimeOnly("com.fasterxml.jackson.core:jackson-core") testRuntimeOnly("com.fasterxml.jackson.core:jackson-databind") testRuntimeOnly("org.codehaus.groovy:groovy:latest.release") + testRuntimeOnly("jakarta.annotation:jakarta.annotation-api:1.3.5") testRuntimeOnly(gradleApi()) } diff --git a/src/main/resources/META-INF/rewrite/add-common-annotations-dependencies.yml b/src/main/resources/META-INF/rewrite/add-common-annotations-dependencies.yml new file mode 100644 index 0000000000..40d42df6e9 --- /dev/null +++ b/src/main/resources/META-INF/rewrite/add-common-annotations-dependencies.yml @@ -0,0 +1,40 @@ +# +# 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. +# +--- +type: specs.openrewrite.org/v1beta/recipe +name: org.openrewrite.java.migrate.javax.AddCommonAnnotationsDependencies +displayName: Add explicit Common Annotations dependencies +description: > + Add the necessary `annotation-api` dependency from Jakarta EE 8 to maintain compatibility with Java version 11 or greater. +tags: + - javax + - java11 + - jsr250 + - jakarta +recipeList: + # Add or update the jakarta.annotation-api to a maven project. This artifact still uses the javax.annotation name space. + - org.openrewrite.java.dependencies.ChangeDependency: + oldGroupId: javax.annotation + oldArtifactId: javax.annotation-api + newGroupId: jakarta.annotation + newArtifactId: jakarta.annotation-api + newVersion: 1.3.x + - org.openrewrite.java.dependencies.AddDependency: + groupId: jakarta.annotation + artifactId: jakarta.annotation-api + version: 1.3.x + onlyIfUsing: javax.annotation..* + acceptTransitive: true diff --git a/src/main/resources/META-INF/rewrite/add-inject-dependencies.yml b/src/main/resources/META-INF/rewrite/add-inject-dependencies.yml index 183fdff99f..da3dd7edeb 100644 --- a/src/main/resources/META-INF/rewrite/add-inject-dependencies.yml +++ b/src/main/resources/META-INF/rewrite/add-inject-dependencies.yml @@ -18,8 +18,7 @@ type: specs.openrewrite.org/v1beta/recipe name: org.openrewrite.java.migrate.javax.AddInjectDependencies displayName: Add explicit Inject dependencies description: > - This recipe will add the necessary `inject-api` dependency from Jakarta EE 8 to maintain compatibility with Java - version 11 or greater. + Add the necessary `inject-api` dependency from Jakarta EE 8 to maintain compatibility with Java version 11 or greater. tags: - javax - java11 @@ -33,7 +32,6 @@ recipeList: version: 1.0.3 onlyIfUsing: javax.inject.* acceptTransitive: true - - org.openrewrite.java.dependencies.UpgradeDependencyVersion: groupId: jakarta.inject artifactId: jakarta.inject-api diff --git a/src/main/resources/META-INF/rewrite/java-version-11.yml b/src/main/resources/META-INF/rewrite/java-version-11.yml index bd4044c3b4..99360341cf 100644 --- a/src/main/resources/META-INF/rewrite/java-version-11.yml +++ b/src/main/resources/META-INF/rewrite/java-version-11.yml @@ -37,6 +37,7 @@ recipeList: - org.openrewrite.java.migrate.javax.AddJaxbDependencies - org.openrewrite.java.migrate.javax.AddJaxwsDependencies - org.openrewrite.java.migrate.javax.AddInjectDependencies + - org.openrewrite.java.migrate.javax.AddCommonAnnotationsDependencies # Remediate deprecations - org.openrewrite.staticanalysis.BigDecimalRoundingConstantsToEnums - org.openrewrite.staticanalysis.PrimitiveWrapperClassConstructorToValueOf diff --git a/src/test/java/org/openrewrite/java/migrate/javax/AddCommonAnnotationsDependenciesTest.java b/src/test/java/org/openrewrite/java/migrate/javax/AddCommonAnnotationsDependenciesTest.java new file mode 100644 index 0000000000..e6575b925d --- /dev/null +++ b/src/test/java/org/openrewrite/java/migrate/javax/AddCommonAnnotationsDependenciesTest.java @@ -0,0 +1,84 @@ +/* + * 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.java.migrate.javax; + +import org.junit.jupiter.api.Test; +import org.openrewrite.java.JavaParser; +import org.openrewrite.test.RecipeSpec; +import org.openrewrite.test.RewriteTest; + +import static org.openrewrite.java.Assertions.*; +import static org.openrewrite.maven.Assertions.pomXml; + +class AddCommonAnnotationsDependenciesTest implements RewriteTest { + + @Override + public void defaults(RecipeSpec spec) { + spec.recipeFromResource( + "/META-INF/rewrite/add-common-annotations-dependencies.yml", + "org.openrewrite.java.migrate.javax.AddCommonAnnotationsDependencies") + .allSources(src -> src.markers(javaVersion(8))); + } + + @Test + void addDependencyIfAnnotationJsr250Present() { + rewriteRun( + spec -> spec.parser(JavaParser.fromJavaVersion().classpath("jakarta.annotation-api")), + mavenProject("my-project", + //language=java + srcMainJava( + java( + """ + import javax.annotation.Generated; + + @Generated("Hello") + class A { + } + """ + ) + ), + //language=xml + pomXml( + """ + + + 4.0.0 + org.sample + sample + 1.0.0 + + """, + """ + + + 4.0.0 + org.sample + sample + 1.0.0 + + + jakarta.annotation + jakarta.annotation-api + 1.3.5 + + + + """ + ) + ) + ); + } +}