From cd99cced0474eeb02a7a8105fa14487a6d25a0e8 Mon Sep 17 00:00:00 2001 From: aboyko Date: Thu, 20 Jun 2024 13:05:36 -0400 Subject: [PATCH] Add only if it is not present transitively --- .../java/migrate/javax/AddJaxbRuntime.java | 16 ++++-- .../javax/AddJaxbDependenciesTest.java | 56 +++++++++++++++++++ 2 files changed, 66 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/openrewrite/java/migrate/javax/AddJaxbRuntime.java b/src/main/java/org/openrewrite/java/migrate/javax/AddJaxbRuntime.java index 829e6d31f7..60a09e5755 100644 --- a/src/main/java/org/openrewrite/java/migrate/javax/AddJaxbRuntime.java +++ b/src/main/java/org/openrewrite/java/migrate/javax/AddJaxbRuntime.java @@ -152,11 +152,15 @@ public G.CompilationUnit visitCompilationUnit(G.CompilationUnit cu, ExecutionCon return g; } + String groupId = GLASSFISH_JAXB_RUNTIME_GROUP; + String artifactId = GLASSFISH_JAXB_RUNTIME_ARTIFACT; + String version = "2.3.x"; if ("sun".equals(runtime)) { - g = (G.CompilationUnit) new org.openrewrite.gradle.AddDependencyVisitor(SUN_JAXB_RUNTIME_GROUP, SUN_JAXB_RUNTIME_ARTIFACT, "2.3.x", null, "runtimeOnly", null, null, null, null) - .visitNonNull(g, ctx); - } else { - g = (G.CompilationUnit) new org.openrewrite.gradle.AddDependencyVisitor(GLASSFISH_JAXB_RUNTIME_GROUP, GLASSFISH_JAXB_RUNTIME_ARTIFACT, "2.3.x", null, "runtimeOnly", null, null, null, null) + groupId = SUN_JAXB_RUNTIME_GROUP; + artifactId = SUN_JAXB_RUNTIME_ARTIFACT; + } + if (rc.findResolvedDependency(groupId, artifactId) == null) { + g = (G.CompilationUnit) new org.openrewrite.gradle.AddDependencyVisitor(groupId, artifactId, version, null, "runtimeOnly", null, null, null, null) .visitNonNull(g, ctx); } return g; @@ -198,10 +202,10 @@ private Xml.Document maybeAddRuntimeDependency(Xml.Document d, ExecutionContext artifactId = SUN_JAXB_RUNTIME_ARTIFACT; } if (getResolutionResult().findDependencies(groupId, artifactId, Scope.Runtime).isEmpty()) { - d = (Xml.Document) new org.openrewrite.maven.AddDependencyVisitor(groupId, artifactId, "2.3.x", null, Scope.Runtime.name().toLowerCase(), null, null, null, null, null) + d = (Xml.Document) new org.openrewrite.maven.AddDependencyVisitor(groupId, artifactId, version, null, Scope.Runtime.name().toLowerCase(), null, null, null, null, null) .visitNonNull(d, ctx); } else { - d = (Xml.Document) new org.openrewrite.maven.UpgradeDependencyVersion(groupId, artifactId, "2.3.x", null, false, null).getVisitor() + d = (Xml.Document) new org.openrewrite.maven.UpgradeDependencyVersion(groupId, artifactId, version, null, false, null).getVisitor() .visitNonNull(d, ctx); } return d; diff --git a/src/test/java/org/openrewrite/java/migrate/javax/AddJaxbDependenciesTest.java b/src/test/java/org/openrewrite/java/migrate/javax/AddJaxbDependenciesTest.java index 5641dc00de..bb2a026dc7 100644 --- a/src/test/java/org/openrewrite/java/migrate/javax/AddJaxbDependenciesTest.java +++ b/src/test/java/org/openrewrite/java/migrate/javax/AddJaxbDependenciesTest.java @@ -480,4 +480,60 @@ void dontAddWhenJacksonPresent() { ) ); } + + @Test + void dontAddWhenTransientPresent() { + rewriteRun( + spec -> spec.beforeRecipe(withToolingApi()), + java(XML_ELEMENT_STUB), + java(CLASS_USING_XML_BIND), + buildGradle( + //language=gradle + """ + plugins { + id "java-library" + } + + repositories { + mavenCentral() + } + + dependencies { + implementation("jakarta.xml.bind:jakarta.xml.bind-api:2.3.3") + implementation 'org.springframework.boot:spring-boot-starter-data-jpa:2.7.3' + } + """ + ), + pomXml( + //language=xml + """ + + 4.0.0 + org.springframework.samples + spring-petclinic + 2.7.3 + + + org.springframework.boot + spring-boot-starter-parent + 2.7.3 + + petclinic + + + 11 + + + + + org.springframework.boot + spring-boot-starter-data-jpa + + + + """ + ) + ); + } + }