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 015d3e5641..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; @@ -190,11 +194,18 @@ private Xml.Document maybeAddRuntimeDependency(Xml.Document d, ExecutionContext return d; } + String groupId = GLASSFISH_JAXB_RUNTIME_GROUP; + String artifactId = GLASSFISH_JAXB_RUNTIME_ARTIFACT; + String version = "2.3.x"; if ("sun".equals(runtime)) { - d = (Xml.Document) new org.openrewrite.maven.AddDependencyVisitor(SUN_JAXB_RUNTIME_GROUP, SUN_JAXB_RUNTIME_ARTIFACT, "2.3.x", null, Scope.Runtime.name().toLowerCase(), null, null, null, null, null) + groupId = SUN_JAXB_RUNTIME_GROUP; + artifactId = SUN_JAXB_RUNTIME_ARTIFACT; + } + if (getResolutionResult().findDependencies(groupId, artifactId, Scope.Runtime).isEmpty()) { + 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.AddDependencyVisitor(GLASSFISH_JAXB_RUNTIME_GROUP, GLASSFISH_JAXB_RUNTIME_ARTIFACT, "2.3.x", null, Scope.Runtime.name().toLowerCase(), null, null, null, null, null) + 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..a40a42c4b9 100644 --- a/src/test/java/org/openrewrite/java/migrate/javax/AddJaxbDependenciesTest.java +++ b/src/test/java/org/openrewrite/java/migrate/javax/AddJaxbDependenciesTest.java @@ -94,7 +94,7 @@ void addJaxbRuntimeOnce() { dependencies { implementation "jakarta.xml.bind:jakarta.xml.bind-api:%s" - + runtimeOnly "org.glassfish.jaxb:jaxb-runtime:%s" } """.formatted(bindApiVersion, runtimeVersion); @@ -480,4 +480,56 @@ 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 + + + + """ + ) + ); + } }