Skip to content

Commit b9f0360

Browse files
evie-lautimtebeek
andauthored
Upgrade maven-ejb-plugin alongside jakarta.ejb-api (#1155)
* Upgrade maven-ejb-plugin alongside jakarta.ejb-api * Change XPath to be less rigid Co-authored-by: Tim te Beek <tim@moderne.io> * Handle case where ejb.api is coupled with maven-ejb-plugin's ejbVersion * More specific version assertions in ejb-api test * regenerate recipes.csv * Return assertThat(pom)...actual() in ejb-api test assertions --------- Co-authored-by: Tim te Beek <tim@moderne.io>
1 parent 83f0e68 commit b9f0360

4 files changed

Lines changed: 244 additions & 5 deletions

File tree

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Copyright 2025 the original author or authors.
3+
* <p>
4+
* Licensed under the Moderne Source Available License (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* https://docs.moderne.io/licensing/moderne-source-available-license
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.openrewrite.java.migrate.jakarta;
17+
18+
import lombok.EqualsAndHashCode;
19+
import lombok.Value;
20+
import org.openrewrite.ExecutionContext;
21+
import org.openrewrite.Recipe;
22+
import org.openrewrite.TreeVisitor;
23+
import org.openrewrite.maven.MavenIsoVisitor;
24+
import org.openrewrite.xml.ChangeTagValueVisitor;
25+
import org.openrewrite.xml.tree.Xml;
26+
27+
/**
28+
* Sets the {@code <ejbVersion>} configuration of the {@code maven-ejb-plugin} to {@code 4.0},
29+
* handling both literal values (e.g. {@code 3.2}) and Maven property references
30+
* (e.g. {@code ${jee.ejb.api}}) whose resolved value matches the EJB 3.x range.
31+
* <p>
32+
* This complements {@code UpgradePluginVersion} (which already handles property-referenced
33+
* plugin versions via {@code ChangePropertyValue}) and replaces the YAML-only
34+
* {@code ChangeTagValue} step that could not resolve property expressions.
35+
*/
36+
@Value
37+
@EqualsAndHashCode(callSuper = false)
38+
public class UpgradeMavenEjbPluginConfiguration extends Recipe {
39+
40+
41+
@Override
42+
public String getDisplayName() {
43+
return "Set `maven-ejb-plugin` ejbVersion to 4.0";
44+
}
45+
46+
@Override
47+
public String getDescription() {
48+
return "Updates the `<ejbVersion>` configuration of `maven-ejb-plugin` to `4.0` when the current value " +
49+
"(or its resolved Maven property) indicates EJB 3.x. Handles the common pattern where `<ejbVersion>` " +
50+
"is coupled to the `javax.ejb-api` dependency version via a shared property, decoupling them after migration.";
51+
}
52+
53+
@Override
54+
public TreeVisitor<?, ExecutionContext> getVisitor() {
55+
return new MavenIsoVisitor<ExecutionContext>() {
56+
@Override
57+
public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) {
58+
// isPluginTag() relies on cursor state — must be checked before super.visitTag()
59+
if (!isPluginTag("org.apache.maven.plugins", "maven-ejb-plugin")) {
60+
return super.visitTag(tag, ctx);
61+
}
62+
Xml.Tag ejbVersionTag = tag.getChild("configuration")
63+
.flatMap(config -> config.getChild("ejbVersion"))
64+
.orElse(null);
65+
if (ejbVersionTag != null) {
66+
String rawValue = ejbVersionTag.getValue().orElse(null);
67+
if (rawValue != null && !"4.0".equals(rawValue.trim())) {
68+
// Resolve property references (e.g. "${jee.ejb.api}") to their actual value.
69+
// Note: by the time this recipe runs the property may already have been bumped to 4.x
70+
// by an earlier step (ChangeDependency / UpgradeDependencyVersion), so we resolve
71+
// against the *original* pom values by checking the rawValue pattern too.
72+
String rawTrimmed = rawValue.trim();
73+
boolean isPropertyRef = rawTrimmed.startsWith("${");
74+
String resolvedValue = isPropertyRef ?
75+
getResolutionResult().getPom().getValue(rawTrimmed) : rawTrimmed;
76+
boolean currentlyEjb3 = resolvedValue != null && resolvedValue.startsWith("3.");
77+
// A property reference means the ejbVersion was coupled to the javax.ejb-api version.
78+
// Even if the property was already updated to 4.x by an earlier recipe step, the
79+
// ejbVersion tag still holds a property reference that must be replaced with "4.0".
80+
if (isPropertyRef || currentlyEjb3) {
81+
doAfterVisit(new ChangeTagValueVisitor<>(ejbVersionTag, "4.0"));
82+
}
83+
}
84+
}
85+
return super.visitTag(tag, ctx);
86+
}
87+
};
88+
}
89+
}

src/main/resources/META-INF/rewrite/jakarta-ee-9.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,11 @@ recipeList:
364364
version: 4.0.x
365365
onlyIfUsing: javax.ejb..*
366366
acceptTransitive: true
367+
- org.openrewrite.maven.UpgradePluginVersion:
368+
groupId: org.apache.maven.plugins
369+
artifactId: maven-ejb-plugin
370+
newVersion: 3.2.x
371+
- org.openrewrite.java.migrate.jakarta.UpgradeMavenEjbPluginConfiguration
367372
- org.openrewrite.java.ChangePackage:
368373
oldPackageName: javax.ejb
369374
newPackageName: jakarta.ejb

0 commit comments

Comments
 (0)