Skip to content

Commit a6a509e

Browse files
Improve "Add explicit Common Annotations dependencies" in "Migrate to Java 11" (#816)
* Proper AddCommonAnnotationsDependencies implementation * Add test * Polish * Polish * Polish * Add a new separate test instead of expanding the documentation example * Only add the classpath dependency to the new test that needs it * Shorted comment * Inline the now dependency only `UpdateJakartaAnnotations2` --------- Co-authored-by: Tim te Beek <tim@moderne.io>
1 parent cbe5ca5 commit a6a509e

File tree

7 files changed

+146
-74
lines changed

7 files changed

+146
-74
lines changed

build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ recipeDependencies {
1212
parserClasspath("jakarta.servlet:jakarta.servlet-api:6.0.0")
1313
parserClasspath("javax.persistence:javax.persistence-api:2.2")
1414
parserClasspath("org.glassfish:javax.servlet:3.0")
15+
parserClasspath("javax.annotation:javax.annotation-api:1.3.2")
1516
}
1617

1718
val rewriteVersion = rewriteRecipe.rewriteVersion.get()

src/main/resources/META-INF/rewrite/add-common-annotations-dependencies.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,16 @@ tags:
2626
- jsr250
2727
- jakarta
2828
recipeList:
29-
# Add or update the jakarta.annotation-api to a maven project. This artifact still uses the javax.annotation name space.
29+
# Add or update the jakarta.annotation-api. Use version 1.3.x, as this version still kept the old javax namespace (starting from 2.x it's changed to jakarta).
3030
- org.openrewrite.java.dependencies.ChangeDependency:
3131
oldGroupId: javax.annotation
3232
oldArtifactId: javax.annotation-api
3333
newGroupId: jakarta.annotation
3434
newArtifactId: jakarta.annotation-api
3535
newVersion: 1.3.x
36+
# set explicitly even if managed, as higher versions do not have the javax namespace
37+
overrideManagedVersion: true
38+
changeManagedDependency: false
3639
# Jakarta EE recommends `provided` scope
3740
- org.openrewrite.maven.ChangeDependencyScope:
3841
groupId: jakarta.annotation
509 Bytes
Binary file not shown.

src/main/resources/META-INF/rewrite/jakarta-ee-10.yml

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,15 @@ tags:
5757
- jakarta
5858
# NOTE: The spec versions in this section comes from https://github.com/jakartaee/jakartaee-api/blob/10.0.0/pom.xml
5959
recipeList:
60-
- org.openrewrite.java.migrate.jakarta.UpdateJakartaAnnotations2
6160
- org.openrewrite.java.migrate.jakarta.UpdateJakartaXmlWsEE10
6261
- org.openrewrite.java.dependencies.UpgradeDependencyVersion:
6362
groupId: jakarta.activation
6463
artifactId: jakarta.activation-api
6564
newVersion: 2.1.x
65+
- org.openrewrite.java.dependencies.UpgradeDependencyVersion:
66+
groupId: jakarta.annotation
67+
artifactId: jakarta.annotation-api
68+
newVersion: 2.1.x
6669
- org.openrewrite.java.dependencies.UpgradeDependencyVersion:
6770
groupId: jakarta.authentication
6871
artifactId: jakarta.authentication-api
@@ -386,22 +389,6 @@ recipeList:
386389
newVersion: 10.0.0
387390
---
388391
type: specs.openrewrite.org/v1beta/recipe
389-
name: org.openrewrite.java.migrate.jakarta.UpdateJakartaAnnotations2
390-
displayName: Update Jakarta EE annotation Dependencies to 2.1.x.
391-
description: Update Jakarta EE annotation Dependencies to 2.1.x.
392-
recipeList:
393-
- org.openrewrite.java.dependencies.UpgradeDependencyVersion:
394-
groupId: jakarta.annotation
395-
artifactId: jakarta.annotation-api
396-
newVersion: 2.1.x
397-
- org.openrewrite.java.ChangeType:
398-
oldFullyQualifiedTypeName: javax.annotation.Nullable
399-
newFullyQualifiedTypeName: jakarta.annotation.Nullable
400-
- org.openrewrite.java.ChangeType:
401-
oldFullyQualifiedTypeName: javax.annotation.Nonnull
402-
newFullyQualifiedTypeName: jakarta.annotation.Nonnull
403-
---
404-
type: specs.openrewrite.org/v1beta/recipe
405392
name: org.openrewrite.java.migrate.jakarta.UpdateJakartaXmlWsEE10
406393
displayName: Update Jakarta EE XML Web Services Dependencies for EE 10.
407394
description: Update Jakarta EE XML Web Services Dependencies for EE 10.

src/main/resources/META-INF/rewrite/jakarta-ee-9.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ recipeList:
6464
- org.openrewrite.java.migrate.jakarta.ApplicationPathWildcardNoLongerAccepted
6565
- org.openrewrite.java.migrate.jakarta.UpdateApacheWSSecurityPackages
6666
- org.openrewrite.java.migrate.jakarta.JavaxEEApiToJakarta
67-
- org.openrewrite.java.migrate.jakarta.RemoveJakartaAnnotationDependency
67+
- org.openrewrite.java.migrate.jakarta.RemoveJakartaAnnotationDependencyWhenManagedBySpringBoot
6868

6969
---
7070
type: specs.openrewrite.org/v1beta/recipe
@@ -1079,10 +1079,11 @@ recipeList:
10791079
newVersion: 9.1.0
10801080
---
10811081
type: specs.openrewrite.org/v1beta/recipe
1082-
name: org.openrewrite.java.migrate.jakarta.RemoveJakartaAnnotationDependency
1082+
name: org.openrewrite.java.migrate.jakarta.RemoveJakartaAnnotationDependencyWhenManagedBySpringBoot
10831083
displayName: Remove `jakarta.annotation-api` dependency when managed by Spring Boot
1084-
description: Counteract the `jakarta.annotation-api` added by `org.openrewrite.java.migrate.javax.AddCommonAnnotationsDependencies` for Spring Boot applications.
1084+
description: Best practice recipe to cleanup a direct dependency which also comes transitively for Spring Boot applications.
10851085
preconditions:
1086+
# TODO: This recipe can be broadened by checking if `jakarta.annotation-api` is transitively on the classpath OR move this recipe to be part of org.openrewrite.java.spring.boot3.UpgradeSpringBoot_3_0 recipe.
10861087
- org.openrewrite.java.migrate.jakarta.HasNoJakartaAnnotations
10871088
- org.openrewrite.java.dependencies.DependencyInsight:
10881089
groupIdPattern: org.springframework.boot

src/main/resources/META-INF/rewrite/java-version-17.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ recipeList:
6262
- org.openrewrite.java.migrate.lang.SwitchCaseAssignmentsToSwitchExpression
6363
- org.openrewrite.java.migrate.lang.SwitchCaseReturnsToSwitchExpression
6464
- org.openrewrite.java.migrate.lang.SwitchExpressionYieldToArrow
65+
- org.openrewrite.java.migrate.UpdateJakartaAnnotationsIfForJavax
66+
- org.openrewrite.java.migrate.UpdateJakartaAnnotationsIfExistsForJakarta
6567

6668
---
6769
type: specs.openrewrite.org/v1beta/recipe
@@ -326,3 +328,29 @@ recipeList:
326328
groupId: org.projectlombok
327329
artifactId: lombok-mapstruct-binding
328330
version: 0.2.0
331+
332+
---
333+
# The scanning phase comes before the editing phase, therefor we check if a javax.annotation dependency is on the classpath (with whatever version)
334+
type: specs.openrewrite.org/v1beta/recipe
335+
name: org.openrewrite.java.migrate.UpdateJakartaAnnotationsIfForJavax
336+
displayName: Update `jakarta.annotation-api` dependency if it exists
337+
description: Counteract the `jakarta.annotation-api` by updating to `jakarta` namespace
338+
preconditions:
339+
- org.openrewrite.java.dependencies.search.ModuleHasDependency:
340+
groupIdPattern: javax.annotation
341+
artifactIdPattern: javax.annotation-api
342+
recipeList:
343+
- org.openrewrite.java.migrate.jakarta.JavaxAnnotationMigrationToJakartaAnnotation
344+
345+
---
346+
type: specs.openrewrite.org/v1beta/recipe
347+
name: org.openrewrite.java.migrate.UpdateJakartaAnnotationsIfExistsForJakarta
348+
displayName: Update `jakarta.annotation-api` dependency if it exists
349+
description: Counteract the `jakarta.annotation-api` by updating to `jakarta` namespace
350+
preconditions:
351+
- org.openrewrite.java.dependencies.search.ModuleHasDependency:
352+
groupIdPattern: jakarta.annotation
353+
artifactIdPattern: jakarta.annotation-api
354+
version: 1.3.x
355+
recipeList:
356+
- org.openrewrite.java.migrate.jakarta.JavaxAnnotationMigrationToJakartaAnnotation

0 commit comments

Comments
 (0)