Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,8 @@ public class AddJaxwsRuntime extends Recipe {
private static final String SUN_JAXWS_RUNTIME_GROUP = "com.sun.xml.ws";
private static final String SUN_JAXWS_RUNTIME_ARTIFACT = "jaxws-rt";

private final AddJaxwsRuntimeGradle addJaxwsRuntimeGradle;
private final AddJaxwsRuntimeMaven addJaxwsRuntimeMaven;

public AddJaxwsRuntime() {
this.addJaxwsRuntimeGradle = new AddJaxwsRuntimeGradle();
this.addJaxwsRuntimeMaven = new AddJaxwsRuntimeMaven();
}
private final AddJaxwsRuntimeGradle addJaxwsRuntimeGradle = new AddJaxwsRuntimeGradle();
private final AddJaxwsRuntimeMaven addJaxwsRuntimeMaven = new AddJaxwsRuntimeMaven();

@Override
public String getDisplayName() {
Expand Down Expand Up @@ -110,29 +105,24 @@ public G.CompilationUnit visitCompilationUnit(G.CompilationUnit cu, ExecutionCon
GradleProject gp = g.getMarkers().findFirst(GradleProject.class)
.orElseThrow(() -> new RuntimeException("Gradle build scripts must have a GradleProject marker"));

Set<String> apiConfigurations = getTransitiveDependencyConfiguration(gp, JAKARTA_JAXWS_API_GROUP, JAKARTA_JAXWS_API_ARTIFACT);
Set<GradleDependencyConfiguration> apiConfigurations = getTransitiveDependencyConfiguration(gp, JAKARTA_JAXWS_API_GROUP, JAKARTA_JAXWS_API_ARTIFACT);

if (!apiConfigurations.isEmpty()) {
Set<String> runtimeConfigurations = getTransitiveDependencyConfiguration(gp, SUN_JAXWS_RUNTIME_GROUP, SUN_JAXWS_RUNTIME_ARTIFACT);
Set<GradleDependencyConfiguration> 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, null)
.visitNonNull(g, ctx);
g = addJaxWsRuntimeDependency("compileOnly", 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, null)
.visitNonNull(g, ctx);
g = addJaxWsRuntimeDependency("testImplementation", g, ctx);
}
} else {
for (String apiConfiguration : apiConfigurations) {
GradleDependencyConfiguration apiGdc = gp.getConfiguration(apiConfiguration);
List<GradleDependencyConfiguration> apiTransitives = gp.configurationsExtendingFrom(apiGdc, true);
for (String runtimeConfiguration : runtimeConfigurations) {
GradleDependencyConfiguration runtimeGdc = gp.getConfiguration(runtimeConfiguration);
List<GradleDependencyConfiguration> 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, null)
.visitNonNull(g, ctx);
for (GradleDependencyConfiguration apiConfiguration : apiConfigurations) {
List<GradleDependencyConfiguration> apiTransitives = gp.configurationsExtendingFrom(apiConfiguration, true);
for (GradleDependencyConfiguration runtimeConfiguration : runtimeConfigurations) {
List<GradleDependencyConfiguration> runtimeTransitives = gp.configurationsExtendingFrom(runtimeConfiguration, true);
if (apiTransitives.stream().noneMatch(runtimeTransitives::contains) && apiConfiguration.isCanBeDeclared()) {
g = addJaxWsRuntimeDependency(apiConfiguration.getName(), g, ctx);
}
}
}
Expand All @@ -142,34 +132,39 @@ public G.CompilationUnit visitCompilationUnit(G.CompilationUnit cu, ExecutionCon
return g;
}

private Set<String> getTransitiveDependencyConfiguration(GradleProject gp, String groupId, String artifactId) {
Set<String> configurations = new HashSet<>();
private Set<GradleDependencyConfiguration> getTransitiveDependencyConfiguration(GradleProject gp, String groupId, String artifactId) {
Set<GradleDependencyConfiguration> configurations = new HashSet<>();
for (GradleDependencyConfiguration gdc : gp.getConfigurations()) {
if (gdc.findRequestedDependency(groupId, artifactId) != null || gdc.findResolvedDependency(groupId, artifactId) != null) {
configurations.add(gdc.getName());
configurations.add(gdc);
}
}

Set<String> tmpConfigurations = new HashSet<>(configurations);
for (String tmpConfiguration : tmpConfigurations) {
GradleDependencyConfiguration gdc = gp.getConfiguration(tmpConfiguration);
Set<GradleDependencyConfiguration> tmpConfigurations = new HashSet<>(configurations);
for (GradleDependencyConfiguration tmpConfiguration : tmpConfigurations) {
GradleDependencyConfiguration gdc = gp.getConfiguration(tmpConfiguration.getName());
for (GradleDependencyConfiguration transitive : gp.configurationsExtendingFrom(gdc, true)) {
configurations.remove(transitive.getName());
configurations.remove(transitive);
}
}

tmpConfigurations = new HashSet<>(configurations);
for (String configuration : tmpConfigurations) {
GradleDependencyConfiguration gdc = gp.getConfiguration(configuration);
for (GradleDependencyConfiguration configuration : tmpConfigurations) {
GradleDependencyConfiguration gdc = gp.getConfiguration(configuration.getName());
for (GradleDependencyConfiguration extendsFrom : gdc.allExtendsFrom()) {
if (configurations.contains(extendsFrom.getName())) {
if (configurations.contains(extendsFrom)) {
configurations.remove(configuration);
}
}
}

return configurations;
}

private G.CompilationUnit addJaxWsRuntimeDependency(String apiConfiguration, G.CompilationUnit g, ExecutionContext ctx) {
return (G.CompilationUnit) new org.openrewrite.gradle.AddDependencyVisitor(SUN_JAXWS_RUNTIME_GROUP, SUN_JAXWS_RUNTIME_ARTIFACT, "2.3.x", null, apiConfiguration, null, null, null, null, null)
.visitNonNull(g, ctx);
}
});
}
}
Expand Down Expand Up @@ -221,7 +216,7 @@ public Xml.Document visitDocument(Xml.Document document, ExecutionContext ctx) {
* Finds the highest scope for a given group/artifact.
*
* @param mavenModel The maven model to search for a dependency.
* @param groupId The group ID of the dependency
* @param groupId The group ID of the dependency
* @param artifactId The artifact ID of the dependency
* @return The highest scope of the given dependency or null if the dependency does not exist.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/META-INF/rewrite/java-version-11.yml
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ recipeList:
groupId: jakarta.xml.ws
artifactId: jakarta.xml.ws-api
newVersion: 2.3.x
# Add the jaxb runtime to any projects that have a transitive dependency on the api
# Add the jax-ws runtime to any projects that have a transitive dependency on the api
- org.openrewrite.java.migrate.javax.AddJaxwsRuntime
# Remove the version from added dependencies when a managed version exists.
- org.openrewrite.maven.RemoveRedundantDependencyVersions:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.openrewrite.java.migrate.javax;

import org.junit.jupiter.api.Test;
import org.openrewrite.Issue;
import org.openrewrite.config.Environment;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;
Expand Down Expand Up @@ -131,6 +132,32 @@ void addJaxwsRuntimeOnce() {
);
}

@Test
@Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/760")
void dontAddGradleResolutionOnlyDependencies() {
rewriteRun(
spec -> spec.beforeRecipe(withToolingApi()),
buildGradle(
//language=gradle
"""
plugins {
id 'java'
}
repositories {
mavenCentral()
}
dependencies {
compileOnly "com.sun.xml.ws:jaxws-rt:2.3.7"

implementation "org.springframework.boot:spring-boot-starter-web-services:2.5.15"

testImplementation "com.sun.xml.ws:jaxws-rt:2.3.7"
}
"""
)
);
}

@Test
void removeReferenceImplementationRuntime() {
rewriteRun(
Expand Down