Class AbstractUpgradeStrategy

java.lang.Object
org.apache.maven.cling.invoker.mvnup.goals.AbstractUpgradeStrategy
All Implemented Interfaces:
UpgradeStrategy
Direct Known Subclasses:
CompatibilityFixStrategy, DeduplicateDependenciesStrategy, DuplicateElementStrategy, EnforcerVersionRangeStrategy, InferenceStrategy, ModelUpgradeStrategy, ModuleNameFixStrategy, NashornCompatibilityStrategy, PluginUpgradeStrategy, RepositoryHttpsUpgradeStrategy, ResourceFilteringStrategy, SourceStrategy, ToolchainPluginStrategy

public abstract class AbstractUpgradeStrategy extends Object implements UpgradeStrategy
Abstract base class for upgrade strategies that provides common functionality and reduces code duplication across strategy implementations.

Strategies work with domtrip Documents for perfect formatting preservation. Subclasses can create domtrip Editors from Documents as needed:

Editor editor = new Editor(document);
// ... perform domtrip operations ...
// Document is automatically updated

Effective model building

Each apply(UpgradeContext, Map) call pre-builds effective models for the whole reactor in a single BUILD_PROJECT pass via prebuildReactorModels(UpgradeContext, Set). This ensures that:

  • Profile activation (file, property, condition) works correctly — not just OS/JDK/activeByDefault profiles;
  • Maven 4 coordinate inference (version/groupId from reactor siblings) works because mappedSources is populated for the whole reactor before any individual POM is resolved;
  • Remote parent resolution shares a single model-builder session, so artifacts fetched for one module are not re-fetched for siblings.

The resulting Map<Path, Model> cache is stored in effectiveModelCache and consulted by buildEffectiveModel(UpgradeContext, Path). For paths not in the cache (external parents reached during a parent-walk), a fallback BUILD_EFFECTIVE call is made on the same ModelBuilder.ModelBuilderSession, which still benefits from the already-populated mappedSources.

Both the cache and the session are reset to null in the finally block of apply(UpgradeContext, Map) so that singleton strategy instances do not leak state across invocations or test runs.

  • Constructor Details

    • AbstractUpgradeStrategy

      public AbstractUpgradeStrategy()
  • Method Details

    • apply

      public final UpgradeResult apply(UpgradeContext context, Map<Path, eu.maveniverse.domtrip.Document> pomMap)
      Template method that handles common logging and error handling. Subclasses implement the actual upgrade logic in doApply().

      Before delegating to doApply(UpgradeContext, Map), pre-builds effective models for every POM in pomMap via a single BUILD_PROJECT reactor pass so that buildEffectiveModel(UpgradeContext, Path) can serve from cache for the common case.

      Specified by:
      apply in interface UpgradeStrategy
      Parameters:
      context - the upgrade context
      pomMap - map of all POM files in the project (domtrip Documents)
      Returns:
      the result of the upgrade operation
    • doApply

      protected abstract UpgradeResult doApply(UpgradeContext context, Map<Path, eu.maveniverse.domtrip.Document> pomMap)
      Subclasses implement the actual upgrade logic here.
      Parameters:
      context - the upgrade context
      pomMap - map of all POM files in the project
      Returns:
      the result of the upgrade operation
    • getOptions

      protected final UpgradeOptions getOptions(UpgradeContext context)
      Gets the upgrade options from the context.
      Parameters:
      context - the upgrade context
      Returns:
      the upgrade options
    • logSummary

      protected void logSummary(UpgradeContext context, UpgradeResult result)
      Logs a summary of the upgrade results.
      Parameters:
      context - the upgrade context
      result - the upgrade result
    • extractArtifactCoordinatesWithParentResolution

      public static eu.maveniverse.domtrip.maven.Coordinates extractArtifactCoordinatesWithParentResolution(UpgradeContext context, eu.maveniverse.domtrip.Document pomDocument)
      Extracts an Artifact from a POM document with parent resolution. If groupId or version are missing, attempts to resolve from parent.

      This method handles Maven's inheritance mechanism where groupId and version can be inherited from the parent POM.

      Parameters:
      context - the upgrade context for logging
      pomDocument - the POM document
      Returns:
      the Artifact or null if it cannot be determined
    • computeAllArtifactCoordinates

      public static Set<eu.maveniverse.domtrip.maven.Coordinates> computeAllArtifactCoordinates(UpgradeContext context, Map<Path, eu.maveniverse.domtrip.Document> pomMap)
      Computes all artifacts from all POMs in a multi-module project. This includes resolving parent inheritance.
      Parameters:
      context - the upgrade context for logging
      pomMap - map of all POM files in the project
      Returns:
      set of all Artifacts in the project
    • getSession

      protected Session getSession()
    • remoteResolutionUnsupportedReason

      protected static String remoteResolutionUnsupportedReason(UpgradeContext context)
      Returns the reason why remote resolution cannot honor the operator's configured repository posture, or null if remote resolution may proceed.

      The standalone resolver session used by mvnup does not apply mirrors, proxies or offline mode from the effective settings. Rather than silently resolving remote POMs while ignoring that configuration, strategies must call this method and skip the remote-model-dependent work whenever a posture is configured that the standalone session cannot honor.

      Blocked mirrors (such as the default external:http:* blocker shipped in the Maven installation settings) do not redirect traffic and therefore do not disable remote resolution by themselves.

      Parameters:
      context - the upgrade context
      Returns:
      a human-readable reason to skip remote resolution, or null if allowed
    • findCommonRoot

      protected Path findCommonRoot(Set<Path> pomPaths)
    • buildEffectiveModel

      protected Model buildEffectiveModel(UpgradeContext context, Path pomPath)
      Returns the effective model for the given POM path.

      Consults effectiveModelCache first (populated by the BUILD_PROJECT reactor pass in prebuildReactorModels(UpgradeContext, Set)). If the path is not in the cache (e.g. an external parent that was not part of the reactor), falls back to a BUILD_EFFECTIVE call on sharedModelBuilderSession, which still benefits from the mappedSources populated during the reactor build.

      Parameters:
      context - the upgrade context (used for debug logging)
      pomPath - the path to the POM file
      Returns:
      the effective model, never null