Skip to content

Commit

Permalink
Add only if it is not present transitively
Browse files Browse the repository at this point in the history
  • Loading branch information
BoykoAlex committed Jun 20, 2024
1 parent dc98a5e commit cd99cce
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
"""
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework.samples</groupId>
<artifactId>spring-petclinic</artifactId>
<version>2.7.3</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.3</version>
</parent>
<name>petclinic</name>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
</dependencies>
</project>
"""
)
);
}

}

0 comments on commit cd99cce

Please sign in to comment.