gnodet-bot commented on code in PR #14169: URL: https://github.com/apache/maven/pull/14169#discussion_r4730430087
########## impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvnup/goals/JakartaEeApiMigrationStrategy.java: ########## @@ -0,0 +1,321 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.maven.cling.invoker.mvnup.goals; + +import java.nio.file.Path; +import java.util.HashSet; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Set; +import java.util.stream.Stream; + +import eu.maveniverse.domtrip.Document; +import eu.maveniverse.domtrip.Element; +import org.apache.maven.api.cli.mvnup.UpgradeOptions; +import org.apache.maven.api.di.Named; +import org.apache.maven.api.di.Priority; +import org.apache.maven.api.di.Singleton; +import org.apache.maven.cling.invoker.mvnup.UpgradeContext; + +import static eu.maveniverse.domtrip.maven.MavenPomElements.Elements.ARTIFACT_ID; +import static eu.maveniverse.domtrip.maven.MavenPomElements.Elements.BUILD; +import static eu.maveniverse.domtrip.maven.MavenPomElements.Elements.DEPENDENCIES; +import static eu.maveniverse.domtrip.maven.MavenPomElements.Elements.DEPENDENCY; +import static eu.maveniverse.domtrip.maven.MavenPomElements.Elements.DEPENDENCY_MANAGEMENT; +import static eu.maveniverse.domtrip.maven.MavenPomElements.Elements.GROUP_ID; +import static eu.maveniverse.domtrip.maven.MavenPomElements.Elements.PLUGIN; +import static eu.maveniverse.domtrip.maven.MavenPomElements.Elements.PLUGINS; +import static eu.maveniverse.domtrip.maven.MavenPomElements.Elements.PLUGIN_MANAGEMENT; +import static eu.maveniverse.domtrip.maven.MavenPomElements.Elements.PROFILE; +import static eu.maveniverse.domtrip.maven.MavenPomElements.Elements.PROFILES; +import static eu.maveniverse.domtrip.maven.MavenPomElements.Elements.VERSION; + +/** + * Strategy for migrating deprecated Java EE API dependencies to their Jakarta EE successors. + * + * <p>Java 11 (JEP 320) removed several Java EE and CORBA modules from the JDK that were + * previously bundled in {@code rt.jar}. Projects that declared these APIs as explicit Maven + * dependencies using their legacy {@code javax.*} groupIds will fail to compile on JDK 11+ + * unless they migrate to the standalone Jakarta EE equivalents. + * + * <p>This strategy detects legacy Java EE API artifacts in {@code <dependencies>}, + * {@code <dependencyManagement>}, profile dependencies, and plugin dependencies, and + * replaces them with their Jakarta EE equivalents: + * + * <table> + * <caption>Legacy to Jakarta EE migration map</caption> + * <tr><th>Legacy (removed in JDK 11)</th><th>Jakarta EE replacement</th></tr> + * <tr><td>{@code javax.xml.bind:jaxb-api}</td> + * <td>{@code jakarta.xml.bind:jakarta.xml.bind-api:2.3.3}</td></tr> + * <tr><td>{@code com.sun.xml.bind:jaxb-core}</td> + * <td>{@code org.glassfish.jaxb:jaxb-core:2.3.0.1}</td></tr> + * <tr><td>{@code com.sun.xml.bind:jaxb-impl}</td> + * <td>{@code org.glassfish.jaxb:jaxb-runtime:2.3.9}</td></tr> + * <tr><td>{@code javax.xml.ws:jaxws-api}</td> + * <td>{@code jakarta.xml.ws:jakarta.xml.ws-api:2.3.3}</td></tr> + * <tr><td>{@code javax.jws:javax.jws-api}</td> + * <td>{@code jakarta.jws:jakarta.jws-api:1.1.1}</td></tr> + * <tr><td>{@code javax.annotation:javax.annotation-api}</td> + * <td>{@code jakarta.annotation:jakarta.annotation-api:1.3.5}</td></tr> + * <tr><td>{@code javax.activation:activation}</td> + * <td>{@code jakarta.activation:jakarta.activation-api:1.2.2}</td></tr> + * <tr><td>{@code javax.activation:javax.activation-api}</td> + * <td>{@code jakarta.activation:jakarta.activation-api:1.2.2}</td></tr> + * </table> + * + * <p>The {@code javax.*} API versions listed above are the last Jakarta EE releases that preserve + * the {@code javax.*} package namespace (i.e. they are source-compatible with code written for + * the removed JDK modules), making them a drop-in replacement without requiring import changes. + * The JAXB RI ({@code com.sun.xml.bind:*}) replacements use the {@code org.glassfish.jaxb} + * groupId and retain binary compatibility. + * + * @see <a href="https://openjdk.org/jeps/320">JEP 320: Remove the Java EE and CORBA Modules</a> + * @see <a href="https://github.com/apache/maven/issues/14163">#14163</a> + */ +@Named +@Singleton +@Priority(16) +public class JakartaEeApiMigrationStrategy extends AbstractUpgradeStrategy { + + /** + * Maps legacy groupId:artifactId to the Jakarta EE replacement coordinates. + * Values are {@code groupId:artifactId:version} triples for the last compatible + * Jakarta EE release that still uses the {@code javax.*} package namespace. + */ + static final Map<String, Replacement> LEGACY_TO_JAKARTA = buildMigrationMap(); + + private static Map<String, Replacement> buildMigrationMap() { + Map<String, Replacement> map = new LinkedHashMap<>(); + // JAXB — javax.xml.bind + map.put("javax.xml.bind:jaxb-api", new Replacement("jakarta.xml.bind", "jakarta.xml.bind-api", "2.3.3")); + map.put("com.sun.xml.bind:jaxb-core", new Replacement("org.glassfish.jaxb", "jaxb-core", "2.3.0.1")); + map.put("com.sun.xml.bind:jaxb-impl", new Replacement("org.glassfish.jaxb", "jaxb-runtime", "2.3.9")); + // JAX-WS — javax.xml.ws + map.put("javax.xml.ws:jaxws-api", new Replacement("jakarta.xml.ws", "jakarta.xml.ws-api", "2.3.3")); + // JAX-WS annotations — javax.jws + map.put("javax.jws:javax.jws-api", new Replacement("jakarta.jws", "jakarta.jws-api", "1.1.1")); + // javax.annotation — from Common Annotations (JSR-250) + map.put( + "javax.annotation:javax.annotation-api", + new Replacement("jakarta.annotation", "jakarta.annotation-api", "1.3.5")); + // JavaBeans Activation Framework — javax.activation + map.put( + "javax.activation:activation", + new Replacement("jakarta.activation", "jakarta.activation-api", "1.2.2")); + map.put( + "javax.activation:javax.activation-api", + new Replacement("jakarta.activation", "jakarta.activation-api", "1.2.2")); + return Map.copyOf(map); + } + + @Override + public boolean isApplicable(UpgradeContext context) { + UpgradeOptions options = getOptions(context); + + if (options.all().orElse(false)) { + return true; + } + + boolean noOptionsSpecified = options.all().isEmpty() + && options.infer().isEmpty() + && options.model().isEmpty() + && options.plugins().isEmpty() + && options.modelVersion().isEmpty(); + + boolean allOptionsDisabled = options.all().map(v -> !v).orElse(false) + && options.infer().map(v -> !v).orElse(false) + && options.model().map(v -> !v).orElse(false) + && options.plugins().map(v -> !v).orElse(false) + && options.modelVersion().isEmpty(); + + if (noOptionsSpecified || allOptionsDisabled) { + return true; + } + + if (options.model().isPresent()) { + return options.model().get(); + } + + return false; + } + + @Override + public String getDescription() { + return "Migrating legacy Java EE API dependencies to Jakarta EE equivalents (JEP 320)"; + } + + @Override + protected UpgradeResult doApply(UpgradeContext context, Map<Path, Document> pomMap) { + Set<Path> processedPoms = new HashSet<>(); + Set<Path> modifiedPoms = new HashSet<>(); + Set<Path> errorPoms = new HashSet<>(); + + for (Map.Entry<Path, Document> entry : pomMap.entrySet()) { + Path pomPath = entry.getKey(); + Document pomDocument = entry.getValue(); + processedPoms.add(pomPath); + + context.info(pomPath + " (checking for legacy Java EE API dependencies)"); + context.indent(); + + try { + boolean modified = migrateLegacyJavaEeApis(pomDocument, context); + + if (modified) { + logChange(context, "Legacy Java EE API migration to Jakarta EE equivalents"); + modifiedPoms.add(pomPath); + } else { + context.success("No legacy Java EE API dependencies found"); + } + } catch (Exception e) { + context.failure("Failed to migrate Java EE API dependencies: " + e.getMessage()); + errorPoms.add(pomPath); + } finally { + context.unindent(); + } + } + + return new UpgradeResult(processedPoms, modifiedPoms, errorPoms); + } + + /** + * Scans all dependency sections in the POM and replaces legacy Java EE artifacts + * with their Jakarta EE equivalents. + * + * @param pomDocument the POM document to modify + * @param context the upgrade context for logging + * @return {@code true} if any dependencies were migrated + */ + boolean migrateLegacyJavaEeApis(Document pomDocument, UpgradeContext context) { + Element root = pomDocument.root(); + boolean modified = false; + + // <dependencies> + modified |= migrateDependenciesSection(root.childElement(DEPENDENCIES).orElse(null), context); + + // <dependencyManagement><dependencies> + modified |= root.childElement(DEPENDENCY_MANAGEMENT).stream() + .flatMap(dm -> dm.childElement(DEPENDENCIES).stream()) + .reduce(false, (acc, deps) -> acc | migrateDependenciesSection(deps, context), Boolean::logicalOr); + + // profiles + Element profiles = root.childElement(PROFILES).orElse(null); + if (profiles != null) { + for (Element profile : profiles.childElements(PROFILE).toList()) { + modified |= migrateDependenciesSection( + profile.childElement(DEPENDENCIES).orElse(null), context); + modified |= profile.childElement(DEPENDENCY_MANAGEMENT).stream() + .flatMap(dm -> dm.childElement(DEPENDENCIES).stream()) + .reduce( + false, + (acc, deps) -> acc | migrateDependenciesSection(deps, context), + Boolean::logicalOr); + } + } + + // plugin-level dependencies (e.g. maven-antrun-plugin dependencies) + Stream<Element> pluginContainers = Stream.concat( + root.childElement(BUILD).stream() + .flatMap(build -> Stream.concat( + build.childElement(PLUGINS).stream(), + build.childElement(PLUGIN_MANAGEMENT).stream() + .flatMap(pm -> pm.childElement(PLUGINS).stream()))), + root.childElement(PROFILES).stream() + .flatMap(profs -> profs.childElements(PROFILE)) + .flatMap(profile -> profile.childElement(BUILD).stream()) + .flatMap(build -> Stream.concat( + build.childElement(PLUGINS).stream(), + build.childElement(PLUGIN_MANAGEMENT).stream() + .flatMap(pm -> pm.childElement(PLUGINS).stream())))); + + for (Element pluginsElem : pluginContainers.toList()) { + for (Element plugin : pluginsElem.childElements(PLUGIN).toList()) { + modified |= migrateDependenciesSection( + plugin.childElement(DEPENDENCIES).orElse(null), context); + } + } + + return modified; + } + + private boolean migrateDependenciesSection(Element dependenciesElement, UpgradeContext context) { + if (dependenciesElement == null) { + return false; + } + + boolean modified = false; + for (Element dependency : dependenciesElement.childElements(DEPENDENCY).toList()) { + modified |= migrateDependency(dependency, context); + } + return modified; + } + + /** + * Checks whether a dependency element matches a legacy Java EE artifact and, + * if so, updates its groupId and artifactId in-place. + * + * <p>The version element is updated only when it is explicitly declared in this + * dependency. If the version is managed via {@code <dependencyManagement>} and + * absent here, it is left untouched. + * + * @param dependency the dependency element to inspect and potentially modify + * @param context the upgrade context for logging + * @return {@code true} if the dependency was migrated + */ + boolean migrateDependency(Element dependency, UpgradeContext context) { + String groupId = dependency.childTextTrimmed(GROUP_ID); + String artifactId = dependency.childTextTrimmed(ARTIFACT_ID); + + if (groupId == null || artifactId == null) { + return false; + } + + String key = groupId + ":" + artifactId; + Replacement replacement = LEGACY_TO_JAKARTA.get(key); + if (replacement == null) { + return false; + } + + // Update groupId + dependency.childElement(GROUP_ID).ifPresent(e -> e.textContent(replacement.groupId())); + + // Update artifactId + dependency.childElement(ARTIFACT_ID).ifPresent(e -> e.textContent(replacement.artifactId())); + + // Update version only if it is explicitly declared in this element + Element versionElement = dependency.childElement(VERSION).orElse(null); + if (versionElement != null) { + versionElement.textContent(replacement.version()); + } Review Comment: ⚠️ **Medium: Version property expressions silently clobbered** If the POM declares `<version>${jaxb.version}</version>`, this code replaces the property expression with a literal version string — silently destroying property-managed version coordination across the reactor. `PluginUpgradeStrategy` already handles this by detecting `${...}` patterns and updating `<properties>` instead, or skipping with a warning. This strategy should do the same: ```suggestion Element versionElement = dependency.childElement(VERSION).orElse(null); if (versionElement != null) { String currentVersion = versionElement.textContentTrimmed(); if (currentVersion != null && currentVersion.startsWith("${") && currentVersion.endsWith("}")) { context.warning("Version of " + key + " is controlled by property " + currentVersion + " — update the property value manually to " + replacement.version()); } else { versionElement.textContent(replacement.version()); } } ``` ########## impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvnup/goals/JakartaEeApiMigrationStrategy.java: ########## @@ -0,0 +1,321 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.maven.cling.invoker.mvnup.goals; + +import java.nio.file.Path; +import java.util.HashSet; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Set; +import java.util.stream.Stream; + +import eu.maveniverse.domtrip.Document; +import eu.maveniverse.domtrip.Element; +import org.apache.maven.api.cli.mvnup.UpgradeOptions; +import org.apache.maven.api.di.Named; +import org.apache.maven.api.di.Priority; +import org.apache.maven.api.di.Singleton; +import org.apache.maven.cling.invoker.mvnup.UpgradeContext; + +import static eu.maveniverse.domtrip.maven.MavenPomElements.Elements.ARTIFACT_ID; +import static eu.maveniverse.domtrip.maven.MavenPomElements.Elements.BUILD; +import static eu.maveniverse.domtrip.maven.MavenPomElements.Elements.DEPENDENCIES; +import static eu.maveniverse.domtrip.maven.MavenPomElements.Elements.DEPENDENCY; +import static eu.maveniverse.domtrip.maven.MavenPomElements.Elements.DEPENDENCY_MANAGEMENT; +import static eu.maveniverse.domtrip.maven.MavenPomElements.Elements.GROUP_ID; +import static eu.maveniverse.domtrip.maven.MavenPomElements.Elements.PLUGIN; +import static eu.maveniverse.domtrip.maven.MavenPomElements.Elements.PLUGINS; +import static eu.maveniverse.domtrip.maven.MavenPomElements.Elements.PLUGIN_MANAGEMENT; +import static eu.maveniverse.domtrip.maven.MavenPomElements.Elements.PROFILE; +import static eu.maveniverse.domtrip.maven.MavenPomElements.Elements.PROFILES; +import static eu.maveniverse.domtrip.maven.MavenPomElements.Elements.VERSION; + +/** + * Strategy for migrating deprecated Java EE API dependencies to their Jakarta EE successors. + * + * <p>Java 11 (JEP 320) removed several Java EE and CORBA modules from the JDK that were + * previously bundled in {@code rt.jar}. Projects that declared these APIs as explicit Maven + * dependencies using their legacy {@code javax.*} groupIds will fail to compile on JDK 11+ + * unless they migrate to the standalone Jakarta EE equivalents. + * + * <p>This strategy detects legacy Java EE API artifacts in {@code <dependencies>}, + * {@code <dependencyManagement>}, profile dependencies, and plugin dependencies, and + * replaces them with their Jakarta EE equivalents: + * + * <table> + * <caption>Legacy to Jakarta EE migration map</caption> + * <tr><th>Legacy (removed in JDK 11)</th><th>Jakarta EE replacement</th></tr> + * <tr><td>{@code javax.xml.bind:jaxb-api}</td> + * <td>{@code jakarta.xml.bind:jakarta.xml.bind-api:2.3.3}</td></tr> + * <tr><td>{@code com.sun.xml.bind:jaxb-core}</td> + * <td>{@code org.glassfish.jaxb:jaxb-core:2.3.0.1}</td></tr> + * <tr><td>{@code com.sun.xml.bind:jaxb-impl}</td> + * <td>{@code org.glassfish.jaxb:jaxb-runtime:2.3.9}</td></tr> + * <tr><td>{@code javax.xml.ws:jaxws-api}</td> + * <td>{@code jakarta.xml.ws:jakarta.xml.ws-api:2.3.3}</td></tr> + * <tr><td>{@code javax.jws:javax.jws-api}</td> + * <td>{@code jakarta.jws:jakarta.jws-api:1.1.1}</td></tr> + * <tr><td>{@code javax.annotation:javax.annotation-api}</td> + * <td>{@code jakarta.annotation:jakarta.annotation-api:1.3.5}</td></tr> + * <tr><td>{@code javax.activation:activation}</td> + * <td>{@code jakarta.activation:jakarta.activation-api:1.2.2}</td></tr> + * <tr><td>{@code javax.activation:javax.activation-api}</td> + * <td>{@code jakarta.activation:jakarta.activation-api:1.2.2}</td></tr> + * </table> + * + * <p>The {@code javax.*} API versions listed above are the last Jakarta EE releases that preserve + * the {@code javax.*} package namespace (i.e. they are source-compatible with code written for + * the removed JDK modules), making them a drop-in replacement without requiring import changes. + * The JAXB RI ({@code com.sun.xml.bind:*}) replacements use the {@code org.glassfish.jaxb} + * groupId and retain binary compatibility. + * + * @see <a href="https://openjdk.org/jeps/320">JEP 320: Remove the Java EE and CORBA Modules</a> + * @see <a href="https://github.com/apache/maven/issues/14163">#14163</a> + */ +@Named +@Singleton +@Priority(16) +public class JakartaEeApiMigrationStrategy extends AbstractUpgradeStrategy { + + /** + * Maps legacy groupId:artifactId to the Jakarta EE replacement coordinates. + * Values are {@code groupId:artifactId:version} triples for the last compatible + * Jakarta EE release that still uses the {@code javax.*} package namespace. + */ + static final Map<String, Replacement> LEGACY_TO_JAKARTA = buildMigrationMap(); + + private static Map<String, Replacement> buildMigrationMap() { + Map<String, Replacement> map = new LinkedHashMap<>(); + // JAXB — javax.xml.bind + map.put("javax.xml.bind:jaxb-api", new Replacement("jakarta.xml.bind", "jakarta.xml.bind-api", "2.3.3")); + map.put("com.sun.xml.bind:jaxb-core", new Replacement("org.glassfish.jaxb", "jaxb-core", "2.3.0.1")); + map.put("com.sun.xml.bind:jaxb-impl", new Replacement("org.glassfish.jaxb", "jaxb-runtime", "2.3.9")); + // JAX-WS — javax.xml.ws + map.put("javax.xml.ws:jaxws-api", new Replacement("jakarta.xml.ws", "jakarta.xml.ws-api", "2.3.3")); + // JAX-WS annotations — javax.jws + map.put("javax.jws:javax.jws-api", new Replacement("jakarta.jws", "jakarta.jws-api", "1.1.1")); + // javax.annotation — from Common Annotations (JSR-250) + map.put( + "javax.annotation:javax.annotation-api", + new Replacement("jakarta.annotation", "jakarta.annotation-api", "1.3.5")); + // JavaBeans Activation Framework — javax.activation + map.put( + "javax.activation:activation", + new Replacement("jakarta.activation", "jakarta.activation-api", "1.2.2")); + map.put( + "javax.activation:javax.activation-api", + new Replacement("jakarta.activation", "jakarta.activation-api", "1.2.2")); + return Map.copyOf(map); + } + + @Override + public boolean isApplicable(UpgradeContext context) { + UpgradeOptions options = getOptions(context); + + if (options.all().orElse(false)) { + return true; + } + + boolean noOptionsSpecified = options.all().isEmpty() + && options.infer().isEmpty() + && options.model().isEmpty() + && options.plugins().isEmpty() + && options.modelVersion().isEmpty(); + + boolean allOptionsDisabled = options.all().map(v -> !v).orElse(false) + && options.infer().map(v -> !v).orElse(false) + && options.model().map(v -> !v).orElse(false) + && options.plugins().map(v -> !v).orElse(false) + && options.modelVersion().isEmpty(); + + if (noOptionsSpecified || allOptionsDisabled) { + return true; + } + + if (options.model().isPresent()) { + return options.model().get(); + } + + return false; + } + + @Override + public String getDescription() { + return "Migrating legacy Java EE API dependencies to Jakarta EE equivalents (JEP 320)"; + } + + @Override + protected UpgradeResult doApply(UpgradeContext context, Map<Path, Document> pomMap) { + Set<Path> processedPoms = new HashSet<>(); + Set<Path> modifiedPoms = new HashSet<>(); + Set<Path> errorPoms = new HashSet<>(); + + for (Map.Entry<Path, Document> entry : pomMap.entrySet()) { + Path pomPath = entry.getKey(); + Document pomDocument = entry.getValue(); + processedPoms.add(pomPath); + + context.info(pomPath + " (checking for legacy Java EE API dependencies)"); + context.indent(); + + try { + boolean modified = migrateLegacyJavaEeApis(pomDocument, context); + + if (modified) { + logChange(context, "Legacy Java EE API migration to Jakarta EE equivalents"); + modifiedPoms.add(pomPath); + } else { + context.success("No legacy Java EE API dependencies found"); + } + } catch (Exception e) { + context.failure("Failed to migrate Java EE API dependencies: " + e.getMessage()); + errorPoms.add(pomPath); + } finally { + context.unindent(); + } + } + + return new UpgradeResult(processedPoms, modifiedPoms, errorPoms); + } + + /** + * Scans all dependency sections in the POM and replaces legacy Java EE artifacts + * with their Jakarta EE equivalents. + * + * @param pomDocument the POM document to modify + * @param context the upgrade context for logging + * @return {@code true} if any dependencies were migrated + */ + boolean migrateLegacyJavaEeApis(Document pomDocument, UpgradeContext context) { + Element root = pomDocument.root(); + boolean modified = false; + + // <dependencies> + modified |= migrateDependenciesSection(root.childElement(DEPENDENCIES).orElse(null), context); + + // <dependencyManagement><dependencies> + modified |= root.childElement(DEPENDENCY_MANAGEMENT).stream() + .flatMap(dm -> dm.childElement(DEPENDENCIES).stream()) + .reduce(false, (acc, deps) -> acc | migrateDependenciesSection(deps, context), Boolean::logicalOr); + + // profiles + Element profiles = root.childElement(PROFILES).orElse(null); + if (profiles != null) { + for (Element profile : profiles.childElements(PROFILE).toList()) { + modified |= migrateDependenciesSection( + profile.childElement(DEPENDENCIES).orElse(null), context); + modified |= profile.childElement(DEPENDENCY_MANAGEMENT).stream() + .flatMap(dm -> dm.childElement(DEPENDENCIES).stream()) + .reduce( + false, + (acc, deps) -> acc | migrateDependenciesSection(deps, context), + Boolean::logicalOr); + } + } + + // plugin-level dependencies (e.g. maven-antrun-plugin dependencies) + Stream<Element> pluginContainers = Stream.concat( + root.childElement(BUILD).stream() + .flatMap(build -> Stream.concat( + build.childElement(PLUGINS).stream(), + build.childElement(PLUGIN_MANAGEMENT).stream() + .flatMap(pm -> pm.childElement(PLUGINS).stream()))), + root.childElement(PROFILES).stream() + .flatMap(profs -> profs.childElements(PROFILE)) + .flatMap(profile -> profile.childElement(BUILD).stream()) + .flatMap(build -> Stream.concat( + build.childElement(PLUGINS).stream(), + build.childElement(PLUGIN_MANAGEMENT).stream() + .flatMap(pm -> pm.childElement(PLUGINS).stream())))); + + for (Element pluginsElem : pluginContainers.toList()) { + for (Element plugin : pluginsElem.childElements(PLUGIN).toList()) { + modified |= migrateDependenciesSection( + plugin.childElement(DEPENDENCIES).orElse(null), context); + } + } + + return modified; + } + + private boolean migrateDependenciesSection(Element dependenciesElement, UpgradeContext context) { + if (dependenciesElement == null) { + return false; + } + + boolean modified = false; + for (Element dependency : dependenciesElement.childElements(DEPENDENCY).toList()) { + modified |= migrateDependency(dependency, context); + } + return modified; + } + + /** + * Checks whether a dependency element matches a legacy Java EE artifact and, + * if so, updates its groupId and artifactId in-place. + * + * <p>The version element is updated only when it is explicitly declared in this + * dependency. If the version is managed via {@code <dependencyManagement>} and + * absent here, it is left untouched. + * + * @param dependency the dependency element to inspect and potentially modify + * @param context the upgrade context for logging + * @return {@code true} if the dependency was migrated + */ + boolean migrateDependency(Element dependency, UpgradeContext context) { + String groupId = dependency.childTextTrimmed(GROUP_ID); + String artifactId = dependency.childTextTrimmed(ARTIFACT_ID); + + if (groupId == null || artifactId == null) { + return false; + } + + String key = groupId + ":" + artifactId; + Replacement replacement = LEGACY_TO_JAKARTA.get(key); + if (replacement == null) { + return false; + } + + // Update groupId + dependency.childElement(GROUP_ID).ifPresent(e -> e.textContent(replacement.groupId())); + + // Update artifactId + dependency.childElement(ARTIFACT_ID).ifPresent(e -> e.textContent(replacement.artifactId())); + + // Update version only if it is explicitly declared in this element + Element versionElement = dependency.childElement(VERSION).orElse(null); + if (versionElement != null) { + versionElement.textContent(replacement.version()); + } + + context.detail("Migrated " + key + " → " + replacement.groupId() + ":" + replacement.artifactId() + ":" + + replacement.version()); + context.warning(key + " uses APIs that were removed from the JDK in Java 11 (JEP 320)." + + " Replaced with " + replacement.groupId() + ":" + replacement.artifactId() + ":" + + replacement.version() + + " which provides the same API as a standalone dependency."); Review Comment: 🔸 **Low: `context.warning()` after migration is not actionable** This warning fires _after_ the migration is already complete — the user has nothing to act on. `context.detail()` on line 307 already logs the migration. Per Maven convention, `WARN` should only be used when the user can/should do something in response. This is a migration log entry, not a warning. ```suggestion context.detail(key + " uses APIs that were removed from the JDK in Java 11 (JEP 320)." + " Replaced with " + replacement.groupId() + ":" + replacement.artifactId() + ":" + replacement.version() + " which provides the same API as a standalone dependency."); ``` ########## impl/maven-cli/src/test/java/org/apache/maven/cling/invoker/mvnup/goals/JakartaEeApiMigrationStrategyTest.java: ########## @@ -0,0 +1,529 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.maven.cling.invoker.mvnup.goals; + +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Map; + +import eu.maveniverse.domtrip.Document; +import eu.maveniverse.domtrip.Element; +import org.apache.maven.cling.invoker.mvnup.UpgradeContext; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * Unit tests for the {@link JakartaEeApiMigrationStrategy} class. + */ +@DisplayName("JakartaEeApiMigrationStrategy") +class JakartaEeApiMigrationStrategyTest { + + private static final Path POM_PATH = Paths.get("/project/pom.xml"); + + @Nested + @DisplayName("Applicability") + class ApplicabilityTests { + + @Test + @DisplayName("should be applicable when --all option is true") + void applicableWithAll() { + JakartaEeApiMigrationStrategy strategy = new JakartaEeApiMigrationStrategy(); + assertTrue(strategy.isApplicable(TestUtils.createMockContext(TestUtils.createOptionsWithAll(true)))); + } + + @Test + @DisplayName("should be applicable with default options (no flags)") + void applicableWithDefaults() { + JakartaEeApiMigrationStrategy strategy = new JakartaEeApiMigrationStrategy(); + assertTrue(strategy.isApplicable(TestUtils.createMockContext())); + } + + @Test + @DisplayName("should be applicable when --model is true") + void applicableWithModel() { + JakartaEeApiMigrationStrategy strategy = new JakartaEeApiMigrationStrategy(); + assertTrue(strategy.isApplicable(TestUtils.createMockContext(TestUtils.createOptionsWithFixModel(true)))); + } + + @Test + @DisplayName("should not be applicable when --model is false") + void notApplicableWithModelFalse() { + JakartaEeApiMigrationStrategy strategy = new JakartaEeApiMigrationStrategy(); + assertFalse(strategy.isApplicable(TestUtils.createMockContext(TestUtils.createOptionsWithFixModel(false)))); + } + } + + @Nested + @DisplayName("Single dependency migration") + class SingleDependencyMigrationTests { + + private JakartaEeApiMigrationStrategy strategy; + + @BeforeEach + void setUp() { + strategy = new JakartaEeApiMigrationStrategy(); + } + + @Test + @DisplayName("should migrate javax.xml.bind:jaxb-api to jakarta.xml.bind:jakarta.xml.bind-api") + void migrateJaxbApi() { + String pomXml = """ + <?xml version="1.0" encoding="UTF-8"?> + <project xmlns="http://maven.apache.org/POM/4.0.0"> + <modelVersion>4.0.0</modelVersion> + <groupId>com.example</groupId> + <artifactId>test</artifactId> + <version>1.0</version> + <dependencies> + <dependency> + <groupId>javax.xml.bind</groupId> + <artifactId>jaxb-api</artifactId> + <version>2.3.1</version> + </dependency> + </dependencies> + </project> + """; + Document doc = Document.of(pomXml); + UpgradeContext context = TestUtils.createMockContext(); + + UpgradeResult result = strategy.doApply(context, Map.of(POM_PATH, doc)); + + assertEquals(1, result.modifiedPoms().size()); + String output = doc.toXml(); + assertTrue(output.contains("jakarta.xml.bind"), "groupId should be migrated: " + output); + assertTrue(output.contains("jakarta.xml.bind-api"), "artifactId should be migrated: " + output); + assertTrue(output.contains("2.3.3"), "version should be updated: " + output); + assertFalse(output.contains("javax.xml.bind"), "old groupId should be gone: " + output); + } + + @Test + @DisplayName("should migrate javax.annotation:javax.annotation-api") + void migrateAnnotationApi() { + String pomXml = """ + <?xml version="1.0" encoding="UTF-8"?> + <project xmlns="http://maven.apache.org/POM/4.0.0"> + <modelVersion>4.0.0</modelVersion> + <groupId>com.example</groupId> + <artifactId>test</artifactId> + <version>1.0</version> + <dependencies> + <dependency> + <groupId>javax.annotation</groupId> + <artifactId>javax.annotation-api</artifactId> + <version>1.3.2</version> + </dependency> + </dependencies> + </project> + """; + Document doc = Document.of(pomXml); + UpgradeContext context = TestUtils.createMockContext(); + + UpgradeResult result = strategy.doApply(context, Map.of(POM_PATH, doc)); + + assertEquals(1, result.modifiedPoms().size()); + String output = doc.toXml(); + assertTrue(output.contains("jakarta.annotation"), "groupId should be migrated: " + output); + assertTrue(output.contains("jakarta.annotation-api"), "artifactId should be migrated: " + output); + assertTrue(output.contains("1.3.5"), "version should be updated: " + output); + } + + @Test + @DisplayName("should migrate javax.activation:activation") + void migrateActivation() { + String pomXml = """ + <?xml version="1.0" encoding="UTF-8"?> + <project xmlns="http://maven.apache.org/POM/4.0.0"> + <modelVersion>4.0.0</modelVersion> + <groupId>com.example</groupId> + <artifactId>test</artifactId> + <version>1.0</version> + <dependencies> + <dependency> + <groupId>javax.activation</groupId> + <artifactId>activation</artifactId> + <version>1.1.1</version> + </dependency> + </dependencies> + </project> + """; + Document doc = Document.of(pomXml); + UpgradeContext context = TestUtils.createMockContext(); + + UpgradeResult result = strategy.doApply(context, Map.of(POM_PATH, doc)); + + assertEquals(1, result.modifiedPoms().size()); + String output = doc.toXml(); + assertTrue(output.contains("jakarta.activation"), "groupId should be migrated: " + output); + assertTrue(output.contains("jakarta.activation-api"), "artifactId should be migrated: " + output); + assertTrue(output.contains("1.2.2"), "version should be updated: " + output); + } + + @Test + @DisplayName("should migrate javax.xml.ws:jaxws-api") + void migrateJaxwsApi() { + String pomXml = """ + <?xml version="1.0" encoding="UTF-8"?> + <project xmlns="http://maven.apache.org/POM/4.0.0"> + <modelVersion>4.0.0</modelVersion> + <groupId>com.example</groupId> + <artifactId>test</artifactId> + <version>1.0</version> + <dependencies> + <dependency> + <groupId>javax.xml.ws</groupId> + <artifactId>jaxws-api</artifactId> + <version>2.3.1</version> + </dependency> + </dependencies> + </project> + """; + Document doc = Document.of(pomXml); + UpgradeContext context = TestUtils.createMockContext(); + + UpgradeResult result = strategy.doApply(context, Map.of(POM_PATH, doc)); + + assertEquals(1, result.modifiedPoms().size()); + String output = doc.toXml(); + assertTrue(output.contains("jakarta.xml.ws"), "groupId should be migrated: " + output); + assertTrue(output.contains("jakarta.xml.ws-api"), "artifactId should be migrated: " + output); + } + + @Test + @DisplayName("should not modify POM with no legacy Java EE dependencies") + void noLegacyDependencies() { + String pomXml = """ + <?xml version="1.0" encoding="UTF-8"?> + <project xmlns="http://maven.apache.org/POM/4.0.0"> + <modelVersion>4.0.0</modelVersion> + <groupId>com.example</groupId> + <artifactId>test</artifactId> + <version>1.0</version> + <dependencies> + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-api</artifactId> + <version>1.7.36</version> + </dependency> + </dependencies> + </project> + """; + Document doc = Document.of(pomXml); + UpgradeContext context = TestUtils.createMockContext(); + + UpgradeResult result = strategy.doApply(context, Map.of(POM_PATH, doc)); + + assertEquals(0, result.modifiedPoms().size()); + } + + @Test + @DisplayName("should not modify POM with no dependencies section") + void noDependenciesSection() { + String pomXml = """ + <?xml version="1.0" encoding="UTF-8"?> + <project xmlns="http://maven.apache.org/POM/4.0.0"> + <modelVersion>4.0.0</modelVersion> + <groupId>com.example</groupId> + <artifactId>test</artifactId> + <version>1.0</version> + </project> + """; + Document doc = Document.of(pomXml); + UpgradeContext context = TestUtils.createMockContext(); + + UpgradeResult result = strategy.doApply(context, Map.of(POM_PATH, doc)); + + assertEquals(0, result.modifiedPoms().size()); + } + } + + @Nested + @DisplayName("Version handling") + class VersionHandlingTests { + + private JakartaEeApiMigrationStrategy strategy; + + @BeforeEach + void setUp() { + strategy = new JakartaEeApiMigrationStrategy(); + } + + @Test + @DisplayName("should update version when explicitly declared") + void updatesExplicitVersion() { + String pomXml = """ + <?xml version="1.0" encoding="UTF-8"?> + <project xmlns="http://maven.apache.org/POM/4.0.0"> + <modelVersion>4.0.0</modelVersion> + <groupId>com.example</groupId> + <artifactId>test</artifactId> + <version>1.0</version> + <dependencies> + <dependency> + <groupId>javax.xml.bind</groupId> + <artifactId>jaxb-api</artifactId> + <version>2.2.0</version> + </dependency> + </dependencies> + </project> + """; + Document doc = Document.of(pomXml); + UpgradeContext context = TestUtils.createMockContext(); + + strategy.doApply(context, Map.of(POM_PATH, doc)); + + String output = doc.toXml(); + assertTrue(output.contains("2.3.3"), "version should be updated to Jakarta equivalent: " + output); + assertFalse(output.contains("2.2.0"), "old version should be replaced: " + output); + } + + @Test + @DisplayName("should not add version when dependency has no version element (managed by depMgmt)") + void preservesManagedDependencyWithoutVersion() { + String pomXml = """ + <?xml version="1.0" encoding="UTF-8"?> + <project xmlns="http://maven.apache.org/POM/4.0.0"> + <modelVersion>4.0.0</modelVersion> + <groupId>com.example</groupId> + <artifactId>test</artifactId> + <version>1.0</version> + <dependencyManagement> + <dependencies> + <dependency> + <groupId>javax.xml.bind</groupId> + <artifactId>jaxb-api</artifactId> + <version>2.3.1</version> + </dependency> + </dependencies> + </dependencyManagement> + <dependencies> + <dependency> + <groupId>javax.xml.bind</groupId> + <artifactId>jaxb-api</artifactId> + </dependency> + </dependencies> + </project> + """; + Document doc = Document.of(pomXml); + UpgradeContext context = TestUtils.createMockContext(); + + strategy.doApply(context, Map.of(POM_PATH, doc)); + + String output = doc.toXml(); + // Both the depMgmt entry and the plain dep should have their groupId migrated + assertTrue(output.contains("jakarta.xml.bind"), "groupId should be migrated: " + output); + // The depMgmt entry has 2.3.3; the plain dep had no <version> so none should be added + assertTrue(output.contains("2.3.3"), "dependencyManagement version should be updated: " + output); + // Find the plain <dependencies> section (after </dependencyManagement>) and verify + // it does NOT contain a <version> element for the migrated dependency + int depMgmtClose = output.indexOf("</dependencyManagement>"); + assertTrue(depMgmtClose > 0, "dependencyManagement should be present: " + output); + String plainDepsSection = output.substring(depMgmtClose); + assertFalse( + plainDepsSection.contains("<version>2.3.3</version>"), + "Plain dependency (no version) should not have a version element added: " + plainDepsSection); + } + } + + @Nested + @DisplayName("Dependency management migration") + class DependencyManagementTests { + + private JakartaEeApiMigrationStrategy strategy; + + @BeforeEach + void setUp() { + strategy = new JakartaEeApiMigrationStrategy(); + } + + @Test + @DisplayName("should migrate legacy dependencies in dependencyManagement") + void migratesInDependencyManagement() { + String pomXml = """ + <?xml version="1.0" encoding="UTF-8"?> + <project xmlns="http://maven.apache.org/POM/4.0.0"> + <modelVersion>4.0.0</modelVersion> + <groupId>com.example</groupId> + <artifactId>parent</artifactId> + <version>1.0</version> + <packaging>pom</packaging> + <dependencyManagement> + <dependencies> + <dependency> + <groupId>javax.xml.bind</groupId> + <artifactId>jaxb-api</artifactId> + <version>2.3.1</version> + </dependency> + <dependency> + <groupId>javax.annotation</groupId> + <artifactId>javax.annotation-api</artifactId> + <version>1.3.2</version> + </dependency> + </dependencies> + </dependencyManagement> + </project> + """; + Document doc = Document.of(pomXml); + UpgradeContext context = TestUtils.createMockContext(); + + UpgradeResult result = strategy.doApply(context, Map.of(POM_PATH, doc)); + + assertEquals(1, result.modifiedPoms().size()); + String output = doc.toXml(); + assertTrue(output.contains("jakarta.xml.bind"), "JAXB groupId should be migrated: " + output); + assertTrue(output.contains("jakarta.annotation"), "annotation groupId should be migrated: " + output); + assertFalse(output.contains("javax.xml.bind"), "old JAXB groupId should be gone: " + output); + assertFalse(output.contains("javax.annotation"), "old annotation groupId should be gone: " + output); + } + } + + @Nested + @DisplayName("Profile dependency migration") + class ProfileDependencyTests { + + private JakartaEeApiMigrationStrategy strategy; + + @BeforeEach + void setUp() { + strategy = new JakartaEeApiMigrationStrategy(); + } + + @Test + @DisplayName("should migrate legacy dependencies inside profiles") + void migratesInProfiles() { + String pomXml = """ + <?xml version="1.0" encoding="UTF-8"?> + <project xmlns="http://maven.apache.org/POM/4.0.0"> + <modelVersion>4.0.0</modelVersion> + <groupId>com.example</groupId> + <artifactId>test</artifactId> + <version>1.0</version> + <profiles> + <profile> + <id>legacy-jee</id> + <dependencies> + <dependency> + <groupId>javax.activation</groupId> + <artifactId>javax.activation-api</artifactId> + <version>1.2.0</version> + </dependency> + </dependencies> + </profile> + </profiles> + </project> + """; + Document doc = Document.of(pomXml); + UpgradeContext context = TestUtils.createMockContext(); + + UpgradeResult result = strategy.doApply(context, Map.of(POM_PATH, doc)); + + assertEquals(1, result.modifiedPoms().size()); + String output = doc.toXml(); + assertTrue(output.contains("jakarta.activation"), "groupId should be migrated in profile: " + output); + assertFalse(output.contains("javax.activation"), "old groupId should be gone in profile: " + output); + } + } + + @Nested + @DisplayName("Single dependency unit tests") + class SingleDependencyUnitTests { + + private JakartaEeApiMigrationStrategy strategy; + + @BeforeEach + void setUp() { + strategy = new JakartaEeApiMigrationStrategy(); + } + + @Test + @DisplayName("migrateDependency returns false for non-legacy groupId") + void noMigrationForUnknownArtifact() { + String depXml = """ + <dependency> + <groupId>com.example</groupId> + <artifactId>my-lib</artifactId> + <version>1.0</version> + </dependency> + """; + Document doc = Document.of("<deps>" + depXml + "</deps>"); + Element dep = doc.root().childElement("dependency").orElseThrow(); + UpgradeContext context = TestUtils.createMockContext(); + + assertFalse(strategy.migrateDependency(dep, context)); + } + + @Test + @DisplayName("migrateDependency returns true and updates groupId for jaxb-api") + void migratesJaxbApiDependency() { + String depXml = """ + <dependency> + <groupId>javax.xml.bind</groupId> + <artifactId>jaxb-api</artifactId> + <version>2.3.1</version> + </dependency> + """; + Document doc = Document.of("<deps>" + depXml + "</deps>"); + Element dep = doc.root().childElement("dependency").orElseThrow(); + UpgradeContext context = TestUtils.createMockContext(); + + assertTrue(strategy.migrateDependency(dep, context)); + assertEquals("jakarta.xml.bind", dep.childTextTrimmed("groupId")); + assertEquals("jakarta.xml.bind-api", dep.childTextTrimmed("artifactId")); + assertEquals("2.3.3", dep.childTextTrimmed("version")); + } + } + + @Nested + @DisplayName("Migration map completeness") + class MigrationMapTests { + + @Test + @DisplayName("migration map should contain all JEP-320 removed Java EE APIs") + void migrationMapCompleteness() { + Map<String, JakartaEeApiMigrationStrategy.Replacement> map = + JakartaEeApiMigrationStrategy.LEGACY_TO_JAKARTA; + + assertTrue(map.containsKey("javax.xml.bind:jaxb-api"), "JAXB API should be mapped"); + assertTrue(map.containsKey("javax.xml.ws:jaxws-api"), "JAX-WS API should be mapped"); + assertTrue( + map.containsKey("javax.annotation:javax.annotation-api"), "javax.annotation API should be mapped"); + assertTrue(map.containsKey("javax.activation:activation"), "activation API (old) should be mapped"); + assertTrue( + map.containsKey("javax.activation:javax.activation-api"), "activation API (new) should be mapped"); + assertTrue(map.containsKey("javax.jws:javax.jws-api"), "JWS API should be mapped"); + } + + @Test + @DisplayName("all replacement versions should be non-null and non-empty") + void replacementVersionsNonEmpty() { + for (Map.Entry<String, JakartaEeApiMigrationStrategy.Replacement> entry : + JakartaEeApiMigrationStrategy.LEGACY_TO_JAKARTA.entrySet()) { + JakartaEeApiMigrationStrategy.Replacement r = entry.getValue(); + assertFalse(r.groupId().isBlank(), entry.getKey() + " should have groupId"); + assertFalse(r.artifactId().isBlank(), entry.getKey() + " should have artifactId"); + assertFalse(r.version().isBlank(), entry.getKey() + " should have version"); + } + } + } +} Review Comment: 🔹 **Low: No test for plugin-level dependency migration** The strategy traverses `<build><plugins><plugin><dependencies>` and profile plugin dependencies (see production code), but no test verifies migration in those code paths. The existing tests only cover `<dependencies>` and `<dependencyManagement>`. Consider adding a test with a POM that has a legacy JEE artifact in a plugin's `<dependencies>`. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
