Class PomHelper

java.lang.Object
org.codehaus.mojo.versions.api.PomHelper

public class PomHelper extends Object
Helper class for modifying pom files.
Since:
1.0-alpha-3
Author:
Stephen Connolly
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
     
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static org.apache.maven.project.ProjectBuildingRequest
    createProjectBuilderRequest(org.apache.maven.execution.MavenSession mavenSession, Consumer<org.apache.maven.project.ProjectBuildingRequest>... initializers)
    Convenience method for creating a ProjectBuildingRequest instance based on maven session.
    static void
    debugModules(org.apache.maven.plugin.logging.Log logger, String message, Collection<String> modules)
    Outputs a debug message with a list of modules.
    static String
    evaluate(String expr, Map<String,String> properties)
    A lightweight expression evaluation function.
    Strips the expression token markers from the start and end of the string.
    findProperty(String propertyName, ModelNode node)
    Traverses the module tree upwards searching for the closest definition of a property with the given name.
    static Set<String>
    getAllChildModules(org.apache.maven.model.Model model, org.apache.maven.plugin.logging.Log logger)
    Returns a set of all child modules for a project, including any defined in profiles (ignoring profile activation).
    static Set<String>
    getAllChildModules(org.apache.maven.project.MavenProject project, org.apache.maven.plugin.logging.Log logger)
    Returns a set of all child modules for a project, including any defined in profiles (ignoring profile activation).
    static String
    getArtifactId(org.apache.maven.model.Model model)
    Extracts the artifactId from a raw model, interpolating from the parent if necessary.
    static Map<File,org.apache.maven.model.Model>
    getChildModels(Map<File,org.apache.maven.model.Model> reactor, String groupId, String artifactId)
    Returns all the models that have a specified groupId and artifactId as parent.
    static Map<File,org.apache.maven.model.Model>
    getChildModels(org.apache.maven.project.MavenProject project, org.apache.maven.plugin.logging.Log logger)
    Builds a map of raw models keyed by module path.
    static String
    getGAV(org.apache.maven.model.Model model)
    Returns the GAV coordinates of a model.
    static String
    getGroupId(org.apache.maven.model.Model model)
    Extracts the groupId from a raw model, interpolating from the parent if necessary.
    static org.apache.maven.project.MavenProject
    getLocalRoot(org.apache.maven.project.ProjectBuilder projectBuilder, org.apache.maven.execution.MavenSession mavenSession, org.apache.maven.plugin.logging.Log logger)
    Finds the local root of the current project of the MavenSession instance.
    static org.apache.maven.model.Model
    getModel(Map<File,org.apache.maven.model.Model> reactor, String groupId, String artifactId)
    Returns the model that has the specified groupId and artifactId or null if no such model exists.
    static Map.Entry<File,org.apache.maven.model.Model>
    getModelEntry(Map<File,org.apache.maven.model.Model> reactor, String groupId, String artifactId)
    Returns the model that has the specified groupId (if specified) and artifactId or null if no such model exists.
    static String
    Retrieves the project version from the pom.
    static org.codehaus.mojo.versions.api.PropertyVersionsBuilder[]
    getPropertyVersionsBuilders(VersionsHelper helper, org.apache.maven.project.MavenProject project, boolean includeParent)
    Examines the project to find any properties which are associated with versions of artifacts in the project.
    static org.apache.maven.model.Model
    getRawModel(File moduleProjectFile)
    Gets the raw model before any interpolation what-so-ever.
    static org.apache.maven.model.Model
    Gets the current raw model before any interpolation what-so-ever.
    static org.apache.maven.model.Model
    getRawModel(org.apache.maven.project.MavenProject project)
    Gets the raw model before any interpolation what-so-ever.
    static org.apache.maven.model.Model
    getRawModel(ModifiedPomXMLEventReader modifiedPomXMLEventReader)
    Gets the current raw model before any interpolation what-so-ever.
    static List<ModelNode>
    getRawModelTree(ModelNode rootNode, org.apache.maven.plugin.logging.Log logger)
    Builds a ModelNode tree of raw models keyed by module path and returns a list of all nodes, ordered depth-first visiting order.
    static int
    getReactorParentCount(Map<File,org.apache.maven.model.Model> reactor, org.apache.maven.model.Model model)
    Returns a count of how many parents a model has in the reactor.
    static String
    getVersion(org.apache.maven.model.Model model)
    Extracts the version from a raw model, interpolating from the parent if necessary.
    static boolean
    isExplicitVersion(org.apache.maven.model.Model model)
    Checks to see if the model contains an explicitly specified version.
    static boolean
    isVersionOverlap(String leftVersionOrRange, String rightVersionOrRange)
    Checks if two versions or ranges have an overlap.
    readXmlFile(File outFile)
    Reads a file into a String.
    static boolean
    setDependencyVersion(ModifiedPomXMLEventReader pom, String groupId, String artifactId, String oldVersion, String newVersion, org.apache.maven.model.Model model)
    Searches the pom re-defining the specified dependency to the specified version.
    static boolean
    setElementValue(ModifiedPomXMLEventReader pom, String parentPath, String elementName, String value)
    Sets the value of the given element given its parent element path.
    static boolean
    setElementValue(ModifiedPomXMLEventReader pom, String parentPath, String elementName, String value, boolean shouldCreate)
    Sets the value of the given element given its parent element path.
    static boolean
    setPluginVersion(ModifiedPomXMLEventReader pom, String groupId, String artifactId, String oldVersion, String newVersion)
    Searches the pom re-defining the specified plugin to the specified version.
    static boolean
    Searches the pom re-defining the project version to the specified version.
    static boolean
    Searches the pom re-defining the project version to the specified version.
    static boolean
    setPropertyVersion(ModifiedPomXMLEventReader pom, String profileId, String property, String value)
    Searches the pom re-defining the specified property to the specified version.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Constructor Details

    • PomHelper

      public PomHelper()
  • Method Details

    • getRawModel

      public static org.apache.maven.model.Model getRawModel(org.apache.maven.project.MavenProject project) throws IOException
      Gets the raw model before any interpolation what-so-ever.
      Parameters:
      project - The project to getModel the raw model for.
      Returns:
      The raw model.
      Throws:
      IOException - if the file is not found or if the file does not parse.
    • getRawModel

      public static org.apache.maven.model.Model getRawModel(File moduleProjectFile) throws IOException
      Gets the raw model before any interpolation what-so-ever.
      Parameters:
      moduleProjectFile - The project file to getModel the raw model for.
      Returns:
      The raw model.
      Throws:
      IOException - if the file is not found or if the file does not parse.
    • getRawModel

      public static org.apache.maven.model.Model getRawModel(ModifiedPomXMLEventReader modifiedPomXMLEventReader) throws IOException
      Gets the current raw model before any interpolation what-so-ever.
      Parameters:
      modifiedPomXMLEventReader - The ModifiedPomXMLEventReader to getModel the raw model for.
      Returns:
      The raw model.
      Throws:
      IOException - if the file is not found or if the file does not parse.
    • getRawModel

      public static org.apache.maven.model.Model getRawModel(Reader reader) throws IOException
      Gets the current raw model before any interpolation what-so-ever.
      Parameters:
      reader - The Reader to getModel the raw model for.
      Returns:
      The raw model.
      Throws:
      IOException - if the file is not found or if the file does not parse.
    • setPropertyVersion

      public static boolean setPropertyVersion(ModifiedPomXMLEventReader pom, String profileId, String property, String value) throws XMLStreamException
      Searches the pom re-defining the specified property to the specified version.
      Parameters:
      pom - The pom to modify.
      profileId - The profile in which to modify the property.
      property - The property to modify.
      value - The new value of the property.
      Returns:
      true if a replacement was made.
      Throws:
      XMLStreamException - if somethinh went wrong.
    • setProjectVersion

      public static boolean setProjectVersion(ModifiedPomXMLEventReader pom, String value) throws XMLStreamException
      Searches the pom re-defining the project version to the specified version.
      Parameters:
      pom - The pom to modify.
      value - The new value of the property.
      Returns:
      true if a replacement was made.
      Throws:
      XMLStreamException - if somethinh went wrong.
    • setElementValue

      public static boolean setElementValue(ModifiedPomXMLEventReader pom, String parentPath, String elementName, String value) throws XMLStreamException
      Sets the value of the given element given its parent element path. Will only consider the first found occurrence of the parent element. If the element is not found in the parent element, the method will create the element.
      Parameters:
      pom - pom to modify
      parentPath - path of the parent element
      elementName - name of the element to set or create
      value - the new value of the element
      Returns:
      true if the element was created or replaced
      Throws:
      XMLStreamException - if something went wrong
    • setElementValue

      public static boolean setElementValue(ModifiedPomXMLEventReader pom, String parentPath, String elementName, String value, boolean shouldCreate) throws XMLStreamException
      Sets the value of the given element given its parent element path. Will only consider the first found occurrence of the parent element. If the element is not found in the parent element, the method will create the element if shouldCreate is true.
      Parameters:
      pom - pom to modify
      parentPath - path of the parent element
      elementName - name of the element to set or create
      value - the new value of the element
      shouldCreate - should the element be created if it's not found in the first encountered parent element matching the parentPath
      Returns:
      true if the element was created or replaced
      Throws:
      XMLStreamException - if something went wrong
    • getProjectVersion

      public static String getProjectVersion(ModifiedPomXMLEventReader pom) throws XMLStreamException
      Retrieves the project version from the pom.
      Parameters:
      pom - The pom.
      Returns:
      the project version or null if the project version is not defined (i.e. inherited from parent version).
      Throws:
      XMLStreamException - if something went wrong.
    • setProjectParentVersion

      public static boolean setProjectParentVersion(ModifiedPomXMLEventReader pom, String value) throws XMLStreamException
      Searches the pom re-defining the project version to the specified version.
      Parameters:
      pom - The pom to modify.
      value - The new value of the property.
      Returns:
      true if a replacement was made.
      Throws:
      XMLStreamException - if somethinh went wrong.
    • setDependencyVersion

      public static boolean setDependencyVersion(ModifiedPomXMLEventReader pom, String groupId, String artifactId, String oldVersion, String newVersion, org.apache.maven.model.Model model) throws XMLStreamException
      Searches the pom re-defining the specified dependency to the specified version.
      Parameters:
      pom - The pom to modify.
      groupId - The groupId of the dependency.
      artifactId - The artifactId of the dependency.
      oldVersion - The old version of the dependency.
      newVersion - The new version of the dependency.
      model - The model to getModel the project properties from.
      Returns:
      true if a replacement was made.
      Throws:
      XMLStreamException - if something went wrong.
    • evaluate

      public static String evaluate(String expr, Map<String,String> properties)
      A lightweight expression evaluation function.
      Parameters:
      expr - The expression to evaluate.
      properties - The properties to substitute.
      Returns:
      The evaluated expression.
    • extractExpression

      public static Optional<String> extractExpression(String expr)
      Strips the expression token markers from the start and end of the string.
      Parameters:
      expr - the string (perhaps with token markers)
      Returns:
      the string (without token markers) if a property has been found, Optional.empty() otherwise
    • isVersionOverlap

      public static boolean isVersionOverlap(String leftVersionOrRange, String rightVersionOrRange) throws org.apache.maven.artifact.versioning.InvalidVersionSpecificationException
      Checks if two versions or ranges have an overlap.
      Parameters:
      leftVersionOrRange - the 1st version number or range to test
      rightVersionOrRange - the 2nd version number or range to test
      Returns:
      true if both versions have an overlap
      Throws:
      org.apache.maven.artifact.versioning.InvalidVersionSpecificationException - if the versions can't be parsed to a range
    • setPluginVersion

      public static boolean setPluginVersion(ModifiedPomXMLEventReader pom, String groupId, String artifactId, String oldVersion, String newVersion) throws XMLStreamException
      Searches the pom re-defining the specified plugin to the specified version.
      Parameters:
      pom - The pom to modify.
      groupId - The groupId of the dependency.
      artifactId - The artifactId of the dependency.
      oldVersion - The old version of the dependency.
      newVersion - The new version of the dependency.
      Returns:
      true if a replacement was made.
      Throws:
      XMLStreamException - if somethinh went wrong.
    • getPropertyVersionsBuilders

      public static org.codehaus.mojo.versions.api.PropertyVersionsBuilder[] getPropertyVersionsBuilders(VersionsHelper helper, org.apache.maven.project.MavenProject project, boolean includeParent) throws org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException, IOException
      Examines the project to find any properties which are associated with versions of artifacts in the project.
      Parameters:
      helper - Our versions helper.
      project - The project to examine.
      includeParent - whether parent POMs should be included
      Returns:
      An array of properties that are associated within the project.
      Throws:
      org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException - if an expression cannot be evaluated.
      IOException - if the project's pom file cannot be parsed.
      Since:
      1.0-alpha-3
    • getAllChildModules

      public static Set<String> getAllChildModules(org.apache.maven.project.MavenProject project, org.apache.maven.plugin.logging.Log logger)
      Returns a set of all child modules for a project, including any defined in profiles (ignoring profile activation).
      Parameters:
      project - The project.
      logger - The logger to use.
      Returns:
      the set of all child modules of the project.
    • getAllChildModules

      public static Set<String> getAllChildModules(org.apache.maven.model.Model model, org.apache.maven.plugin.logging.Log logger)
      Returns a set of all child modules for a project, including any defined in profiles (ignoring profile activation).
      Parameters:
      model - The project model.
      logger - The logger to use.
      Returns:
      the set of all child modules of the project.
    • debugModules

      public static void debugModules(org.apache.maven.plugin.logging.Log logger, String message, Collection<String> modules)
      Outputs a debug message with a list of modules.
      Parameters:
      logger - The logger to log to.
      message - The message to display.
      modules - The modules to append to the message.
    • getVersion

      public static String getVersion(org.apache.maven.model.Model model)
      Extracts the version from a raw model, interpolating from the parent if necessary.
      Parameters:
      model - The model.
      Returns:
      The version.
    • isExplicitVersion

      public static boolean isExplicitVersion(org.apache.maven.model.Model model)
      Checks to see if the model contains an explicitly specified version.
      Parameters:
      model - The model.
      Returns:
      true if the model explicitly specifies the project version, i.e. /project/version
    • getArtifactId

      public static String getArtifactId(org.apache.maven.model.Model model)
      Extracts the artifactId from a raw model, interpolating from the parent if necessary.
      Parameters:
      model - The model.
      Returns:
      The artifactId.
    • getGroupId

      public static String getGroupId(org.apache.maven.model.Model model)
      Extracts the groupId from a raw model, interpolating from the parent if necessary.
      Parameters:
      model - The model.
      Returns:
      The groupId.
    • getLocalRoot

      public static org.apache.maven.project.MavenProject getLocalRoot(org.apache.maven.project.ProjectBuilder projectBuilder, org.apache.maven.execution.MavenSession mavenSession, org.apache.maven.plugin.logging.Log logger)
      Finds the local root of the current project of the MavenSession instance.
      Parameters:
      projectBuilder - ProjectBuilder instance
      mavenSession - MavenSession instance
      logger - The logger to log tog
      Returns:
      The local root (note this may be the project passed as an argument).
    • createProjectBuilderRequest

      @SafeVarargs public static org.apache.maven.project.ProjectBuildingRequest createProjectBuilderRequest(org.apache.maven.execution.MavenSession mavenSession, Consumer<org.apache.maven.project.ProjectBuildingRequest>... initializers)

      Convenience method for creating a ProjectBuildingRequest instance based on maven session.

      Note: The method initializes the remote repositories with the remote artifact repositories. Please use the initializers if you need to override this.

      Parameters:
      mavenSession - MavenSession instance
      initializers - optional additional initializers, which will be executed after the object is initialized
      Returns:
      constructed builder request
    • getRawModelTree

      public static List<ModelNode> getRawModelTree(ModelNode rootNode, org.apache.maven.plugin.logging.Log logger) throws UncheckedIOException
      Builds a ModelNode tree of raw models keyed by module path and returns a list of all nodes, ordered depth-first visiting order. The root node is always the first node of the list.
      Parameters:
      rootNode - The root node of the reactor
      logger - logger to log parsing errors to
      Returns:
      the root node of the ModelNode of raw models relative to the project's basedir.
      Throws:
      UncheckedIOException - thrown if reading one of the models down the tree fails
    • findProperty

      public static Optional<ModelNode> findProperty(String propertyName, ModelNode node)
      Traverses the module tree upwards searching for the closest definition of a property with the given name.
      Parameters:
      propertyName - name of the property to be found
      node - model tree node at which the search should be started
      Returns:
      Optional object containing the model tree node containing the closest property definition, or Optional.empty() if none has been found
    • getChildModels

      public static Map<File,org.apache.maven.model.Model> getChildModels(org.apache.maven.project.MavenProject project, org.apache.maven.plugin.logging.Log logger) throws IOException
      Builds a map of raw models keyed by module path.
      Parameters:
      project - The project to build from.
      logger - The logger for logging.
      Returns:
      A map of raw models keyed by path relative to the project's basedir.
      Throws:
      IOException - if things go wrong.
    • getChildModels

      public static Map<File,org.apache.maven.model.Model> getChildModels(Map<File,org.apache.maven.model.Model> reactor, String groupId, String artifactId)
      Returns all the models that have a specified groupId and artifactId as parent.
      Parameters:
      reactor - The map of models keyed by path.
      groupId - The groupId of the parent.
      artifactId - The artifactId of the parent.
      Returns:
      a map of models that have a specified groupId and artifactId as parent keyed by path.
    • getModel

      public static org.apache.maven.model.Model getModel(Map<File,org.apache.maven.model.Model> reactor, String groupId, String artifactId)
      Returns the model that has the specified groupId and artifactId or null if no such model exists.
      Parameters:
      reactor - The map of models keyed by path.
      groupId - The groupId to match.
      artifactId - The artifactId to match.
      Returns:
      The model or null if the model was not in the reactor.
    • getModelEntry

      public static Map.Entry<File,org.apache.maven.model.Model> getModelEntry(Map<File,org.apache.maven.model.Model> reactor, String groupId, String artifactId)
      Returns the model that has the specified groupId (if specified) and artifactId or null if no such model exists.
      Parameters:
      reactor - The map of models keyed by path.
      groupId - The groupId to match.
      artifactId - The artifactId to match.
      Returns:
      The model entry or null if the model was not in the reactor.
    • getReactorParentCount

      public static int getReactorParentCount(Map<File,org.apache.maven.model.Model> reactor, org.apache.maven.model.Model model)
      Returns a count of how many parents a model has in the reactor.
      Parameters:
      reactor - The map of models keyed by path.
      model - The model.
      Returns:
      The number of parents of this model in the reactor.
    • readXmlFile

      public static StringBuilder readXmlFile(File outFile) throws IOException
      Reads a file into a String.
      Parameters:
      outFile - The file to read.
      Returns:
      String The content of the file.
      Throws:
      IOException - when things go wrong.
    • getGAV

      public static String getGAV(org.apache.maven.model.Model model)
      Returns the GAV coordinates of a model.
      Parameters:
      model - the model.
      Returns:
      the GAV coordinates of a model.