|
| 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 | +} |
0 commit comments