From 0e3ff9f3c72032247e1de88aed8c782cd55b1479 Mon Sep 17 00:00:00 2001 From: Knut Wannheden Date: Mon, 4 Dec 2023 13:06:24 +0100 Subject: [PATCH] `AddJaxwsRuntime` should not always add `jaxws-rt` (#363) The Gradle recipe should only add the runtime dependency if the API dependency `jakarta.xml.ws:jakarta.xml.ws-api` is already present. This is already how the Maven recipe operates. --- .../java/migrate/javax/AddJaxwsRuntime.java | 58 ++++++++++--------- 1 file changed, 31 insertions(+), 27 deletions(-) diff --git a/src/main/java/org/openrewrite/java/migrate/javax/AddJaxwsRuntime.java b/src/main/java/org/openrewrite/java/migrate/javax/AddJaxwsRuntime.java index abe87e4c53..eecc3101fc 100644 --- a/src/main/java/org/openrewrite/java/migrate/javax/AddJaxwsRuntime.java +++ b/src/main/java/org/openrewrite/java/migrate/javax/AddJaxwsRuntime.java @@ -111,27 +111,29 @@ public G.CompilationUnit visitCompilationUnit(G.CompilationUnit cu, ExecutionCon .orElseThrow(() -> new RuntimeException("Gradle build scripts must have a GradleProject marker")); Set apiConfigurations = getTransitiveDependencyConfiguration(gp, JAKARTA_JAXWS_API_GROUP, JAKARTA_JAXWS_API_ARTIFACT); - Set runtimeConfigurations = getTransitiveDependencyConfiguration(gp, SUN_JAXWS_RUNTIME_GROUP, SUN_JAXWS_RUNTIME_ARTIFACT); - if (runtimeConfigurations.isEmpty()) { - if (gp.getConfiguration("compileOnly") != null) { - g = (G.CompilationUnit) new org.openrewrite.gradle.AddDependencyVisitor(SUN_JAXWS_RUNTIME_GROUP, SUN_JAXWS_RUNTIME_ARTIFACT, "2.3.x", null, "compileOnly", null, null, null, null) - .visitNonNull(g, ctx); - } - if (gp.getConfiguration("testImplementation") != null) { - g = (G.CompilationUnit) new org.openrewrite.gradle.AddDependencyVisitor(SUN_JAXWS_RUNTIME_GROUP, SUN_JAXWS_RUNTIME_ARTIFACT, "2.3.x", null, "testImplementation", null, null, null, null) - .visitNonNull(g, ctx); - } - } else { - for (String apiConfiguration : apiConfigurations) { - GradleDependencyConfiguration apiGdc = gp.getConfiguration(apiConfiguration); - List apiTransitives = gp.configurationsExtendingFrom(apiGdc, true); - for (String runtimeConfiguration : runtimeConfigurations) { - GradleDependencyConfiguration runtimeGdc = gp.getConfiguration(runtimeConfiguration); - List runtimeTransitives = gp.configurationsExtendingFrom(runtimeGdc, true); - if (apiTransitives.stream().noneMatch(runtimeTransitives::contains)) { - g = (G.CompilationUnit) new org.openrewrite.gradle.AddDependencyVisitor(SUN_JAXWS_RUNTIME_GROUP, SUN_JAXWS_RUNTIME_ARTIFACT, "2.3.x", null, apiConfiguration, null, null, null, null) - .visitNonNull(g, ctx); + if (!apiConfigurations.isEmpty()) { + Set runtimeConfigurations = getTransitiveDependencyConfiguration(gp, SUN_JAXWS_RUNTIME_GROUP, SUN_JAXWS_RUNTIME_ARTIFACT); + if (runtimeConfigurations.isEmpty()) { + if (gp.getConfiguration("compileOnly") != null) { + g = (G.CompilationUnit) new org.openrewrite.gradle.AddDependencyVisitor(SUN_JAXWS_RUNTIME_GROUP, SUN_JAXWS_RUNTIME_ARTIFACT, "2.3.x", null, "compileOnly", null, null, null, null) + .visitNonNull(g, ctx); + } + if (gp.getConfiguration("testImplementation") != null) { + g = (G.CompilationUnit) new org.openrewrite.gradle.AddDependencyVisitor(SUN_JAXWS_RUNTIME_GROUP, SUN_JAXWS_RUNTIME_ARTIFACT, "2.3.x", null, "testImplementation", null, null, null, null) + .visitNonNull(g, ctx); + } + } else { + for (String apiConfiguration : apiConfigurations) { + GradleDependencyConfiguration apiGdc = gp.getConfiguration(apiConfiguration); + List apiTransitives = gp.configurationsExtendingFrom(apiGdc, true); + for (String runtimeConfiguration : runtimeConfigurations) { + GradleDependencyConfiguration runtimeGdc = gp.getConfiguration(runtimeConfiguration); + List runtimeTransitives = gp.configurationsExtendingFrom(runtimeGdc, true); + if (apiTransitives.stream().noneMatch(runtimeTransitives::contains)) { + g = (G.CompilationUnit) new org.openrewrite.gradle.AddDependencyVisitor(SUN_JAXWS_RUNTIME_GROUP, SUN_JAXWS_RUNTIME_ARTIFACT, "2.3.x", null, apiConfiguration, null, null, null, null) + .visitNonNull(g, ctx); + } } } } @@ -199,13 +201,15 @@ public Xml.Document visitDocument(Xml.Document document, ExecutionContext ctx) { //Find the highest scope of a transitive dependency on the JAX-WS API (if it exists at all) Scope apiScope = getTransitiveDependencyScope(mavenModel, JAKARTA_JAXWS_API_GROUP, JAKARTA_JAXWS_API_ARTIFACT); - //Find the highest scope of a transitive dependency on the JAX-WS runtime (if it exists at all) - Scope runtimeScope = getTransitiveDependencyScope(mavenModel, SUN_JAXWS_RUNTIME_GROUP, SUN_JAXWS_RUNTIME_ARTIFACT); - - if (apiScope != null && (runtimeScope == null || !apiScope.isInClasspathOf(runtimeScope))) { - String resolvedScope = apiScope == Scope.Test ? "test" : "provided"; - d = (Xml.Document) new AddDependencyVisitor(SUN_JAXWS_RUNTIME_GROUP, SUN_JAXWS_RUNTIME_ARTIFACT, - "2.3.x", null, resolvedScope, null, null, null, null, null).visit(d, ctx); + if (apiScope != null) { + //Find the highest scope of a transitive dependency on the JAX-WS runtime (if it exists at all) + Scope runtimeScope = getTransitiveDependencyScope(mavenModel, SUN_JAXWS_RUNTIME_GROUP, SUN_JAXWS_RUNTIME_ARTIFACT); + + if (runtimeScope == null || !apiScope.isInClasspathOf(runtimeScope)) { + String resolvedScope = apiScope == Scope.Test ? "test" : "provided"; + d = (Xml.Document) new AddDependencyVisitor(SUN_JAXWS_RUNTIME_GROUP, SUN_JAXWS_RUNTIME_ARTIFACT, + "2.3.x", null, resolvedScope, null, null, null, null, null).visit(d, ctx); + } } return d;