Class PomHelper

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

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

    • APACHE_MAVEN_PLUGINS_GROUPID

      public static final String APACHE_MAVEN_PLUGINS_GROUPID
      See Also:
    • PATTERN_PROJECT_PROPERTIES

      public static final Pattern PATTERN_PROJECT_PROPERTIES
    • PATTERN_PROJECT_PROFILE

      public static final Pattern PATTERN_PROJECT_PROFILE
    • PATTERN_PROJECT_PROFILE_ID

      public static final Pattern PATTERN_PROJECT_PROFILE_ID
    • PATTERN_PROJECT_VERSION

      public static final Pattern PATTERN_PROJECT_VERSION
    • PATTERN_PROJECT_PARENT_VERSION

      public static final Pattern PATTERN_PROJECT_PARENT_VERSION
    • PATTERN_PROJECT_DEPENDENCY

      public static final Pattern PATTERN_PROJECT_DEPENDENCY
    • PATTERN_PROJECT_DEPENDENCY_VERSION

      public static final Pattern PATTERN_PROJECT_DEPENDENCY_VERSION
    • PATTERN_PROJECT_PLUGIN

      public static final Pattern PATTERN_PROJECT_PLUGIN
    • PATTERN_PROJECT_PLUGIN_VERSION

      public static final Pattern PATTERN_PROJECT_PLUGIN_VERSION
  • 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(String modelString, File modelPath) throws IOException
      Gets the current raw model before any interpolation what-so-ever.
      Parameters:
      modelString - a string containing the raw model
      modelPath - the File containing the model
      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(MutableXMLStreamReader 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(MutableXMLStreamReader 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(MutableXMLStreamReader 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(MutableXMLStreamReader 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(MutableXMLStreamReader 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(MutableXMLStreamReader 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(MutableXMLStreamReader pom, String groupId, String artifactId, String oldVersion, String newVersion, org.apache.maven.model.Model model, org.apache.maven.plugin.logging.Log logger) 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.
      logger - The logger to use.
      Returns:
      true if a replacement was made.
      Throws:
      XMLStreamException - if something went wrong.
    • evaluate

      public static String evaluate(String expr, Map<String,String> properties, org.apache.maven.plugin.logging.Log logger)
      A lightweight expression evaluation function.
      Parameters:
      expr - The expression to evaluate.
      properties - The properties to substitute.
      logger - The logger to use.
      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(MutableXMLStreamReader 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 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
    • 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.
    • readXml

      public static org.apache.commons.lang3.tuple.Pair<String,Charset> readXml(InputStream inputStream) throws IOException, XMLStreamException
      Reads an XML from the given InputStream.
      Parameters:
      inputStream - The input stream to read.
      Returns:
      Pair<String, Charset> The (mutable) content of the file with the charset of the file
      Throws:
      IOException - when things go wrong.
      XMLStreamException
    • readXml

      public static org.apache.commons.lang3.tuple.Pair<String,Charset> readXml(File file) throws IOException, XMLStreamException, TransformerException
      Reads an XML from a file.
      Parameters:
      file - The file to read.
      Returns:
      Pair<String, Charset> The contents of the file with the charset of the file
      Throws:
      IOException - when things go wrong.
      XMLStreamException
      TransformerException
    • 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.