Package org.codehaus.mojo.versions.api
Class PomHelper
java.lang.Object
org.codehaus.mojo.versions.api.PomHelper
Helper class for modifying pom files.
- Since:
- 1.0-alpha-3
- Author:
- Stephen Connolly
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic org.apache.maven.project.ProjectBuildingRequestcreateProjectBuilderRequest(org.apache.maven.execution.MavenSession mavenSession, Consumer<org.apache.maven.project.ProjectBuildingRequest>... initializers) Convenience method for creating aProjectBuildingRequestinstance based on maven session.static voiddebugModules(org.apache.maven.plugin.logging.Log logger, String message, Collection<String> modules) Outputs a debug message with a list of modules.static StringA lightweight expression evaluation function.extractExpression(String expr) 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.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).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 StringgetArtifactId(org.apache.maven.model.Model model) Extracts the artifactId from a raw model, interpolating from the parent if necessary.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.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 StringgetGAV(org.apache.maven.model.Model model) Returns the GAV coordinates of a model.static StringgetGroupId(org.apache.maven.model.Model model) Extracts the groupId from a raw model, interpolating from the parent if necessary.static org.apache.maven.project.MavenProjectgetLocalRoot(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 theMavenSessioninstance.static org.apache.maven.model.ModelReturns the model that has the specified groupId and artifactId ornullif no such model exists.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 ornullif no such model exists.static StringRetrieves the project version from the pom.static 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.ModelgetRawModel(File moduleProjectFile) Gets the raw model before any interpolation what-so-ever.static org.apache.maven.model.ModelgetRawModel(Reader reader) Gets the current raw model before any interpolation what-so-ever.static org.apache.maven.model.ModelgetRawModel(String modelString, File modelPath) Gets the current raw model before any interpolation what-so-ever.static org.apache.maven.model.ModelgetRawModel(org.apache.maven.project.MavenProject project) Gets the raw model before any interpolation what-so-ever.getRawModelTree(ModelNode rootNode, org.apache.maven.plugin.logging.Log logger) Builds aModelNodetree of raw models keyed by module path and returns a list of all nodes, ordered depth-first visiting order.static intgetReactorParentCount(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 StringgetVersion(org.apache.maven.model.Model model) Extracts the version from a raw model, interpolating from the parent if necessary.static booleanisExplicitVersion(org.apache.maven.model.Model model) Checks to see if the model contains an explicitly specified version.static booleanisVersionOverlap(String leftVersionOrRange, String rightVersionOrRange) Checks if two versions or ranges have an overlap.Reads an XML from a file.readXml(InputStream inputStream) Reads an XML from the given InputStream.static booleansetDependencyVersion(MutableXMLStreamReader pom, String groupId, String artifactId, String oldVersion, String newVersion, org.apache.maven.model.Model model, org.apache.maven.plugin.logging.Log logger) Searches the pom re-defining the specified dependency to the specified version.static booleansetElementValue(MutableXMLStreamReader pom, String parentPath, String elementName, String value) Sets the value of the given element given its parent element path.static booleansetElementValue(MutableXMLStreamReader pom, String parentPath, String elementName, String value, boolean shouldCreate) Sets the value of the given element given its parent element path.static booleansetPluginVersion(MutableXMLStreamReader pom, String groupId, String artifactId, String oldVersion, String newVersion) Searches the pom re-defining the specified plugin to the specified version.static booleansetProjectParentVersion(MutableXMLStreamReader pom, String value) Searches the pom re-defining the project version to the specified version.static booleansetProjectVersion(MutableXMLStreamReader pom, String value) Searches the pom re-defining the project version to the specified version.static booleansetPropertyVersion(MutableXMLStreamReader pom, String profileId, String property, String value) Searches the pom re-defining the specified property to the specified version.
-
Field Details
-
APACHE_MAVEN_PLUGINS_GROUPID
- See Also:
-
PATTERN_PROJECT_PROPERTIES
-
PATTERN_PROJECT_PROFILE
-
PATTERN_PROJECT_PROFILE_ID
-
PATTERN_PROJECT_VERSION
-
PATTERN_PROJECT_PARENT_VERSION
-
PATTERN_PROJECT_DEPENDENCY
-
PATTERN_PROJECT_DEPENDENCY_VERSION
-
PATTERN_PROJECT_PLUGIN
-
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
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 modelmodelPath- 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
Gets the current raw model before any interpolation what-so-ever.- Parameters:
reader- TheReaderto 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:
trueif 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:
trueif 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 modifyparentPath- path of the parent elementelementName- name of the element to set or createvalue- the new value of the element- Returns:
trueif 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 ifshouldCreateistrue.- Parameters:
pom- pom to modifyparentPath- path of the parent elementelementName- name of the element to set or createvalue- the new value of the elementshouldCreate- should the element be created if it's not found in the first encountered parent element matching the parentPath- Returns:
trueif the element was created or replaced- Throws:
XMLStreamException- if something went wrong
-
getProjectVersion
Retrieves the project version from the pom.- Parameters:
pom- The pom.- Returns:
- the project version or
nullif 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:
trueif 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:
trueif 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
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 testrightVersionOrRange- 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:
trueif 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
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:
trueif the model explicitly specifies the project version, i.e. /project/version
-
getArtifactId
Extracts the artifactId from a raw model, interpolating from the parent if necessary.- Parameters:
model- The model.- Returns:
- The artifactId.
-
getGroupId
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 theMavenSessioninstance.- Parameters:
projectBuilder-ProjectBuilderinstancemavenSession-MavenSessioninstancelogger- 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
ProjectBuildingRequestinstance 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-MavenSessioninstanceinitializers- 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 aModelNodetree 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 reactorlogger- logger to log parsing errors to- Returns:
- the root node of the
ModelNodeof raw models relative to the project's basedir. - Throws:
UncheckedIOException
-
findProperty
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 foundnode- model tree node at which the search should be started- Returns:
Optionalobject containing the model tree node containing the closest property definition, orOptional.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 ornullif 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
nullif 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 ornullif 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
nullif 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.XMLStreamExceptionTransformerException
-
getGAV
Returns the GAV coordinates of a model.- Parameters:
model- the model.- Returns:
- the GAV coordinates of a model.
-