Skip to content

Commit

Permalink
jaxb-runtime added only if not present already (#499)
Browse files Browse the repository at this point in the history
* Add `jaxb-runtime` only if not present already via transitive dependency

Add  only if it is not present transitively

* Cleanup - Follow bot proposal

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* Add extra closing tags on input pom.xml

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Tim te Beek <tim@moderne.io>
  • Loading branch information
3 people authored Jun 20, 2024
1 parent f1f1bb5 commit 86bc2b5
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 7 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 @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
"""
<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 86bc2b5

Please sign in to comment.