Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package org.codehaus.mojo.versions.api;
/*
* 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.
*/
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.XMLEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;
import java.util.Set;
import java.util.Stack;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.function.Consumer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
import org.apache.maven.artifact.versioning.VersionRange;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Dependency;
import org.apache.maven.model.Model;
import org.apache.maven.model.Parent;
import org.apache.maven.model.Plugin;
import org.apache.maven.model.Profile;
import org.apache.maven.model.ReportPlugin;
import org.apache.maven.model.building.ModelBuildingRequest;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.project.DefaultProjectBuildingRequest;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.ProjectBuilder;
import org.apache.maven.project.ProjectBuildingException;
import org.apache.maven.project.ProjectBuildingRequest;
import org.apache.maven.project.ProjectBuildingResult;
import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader;
import org.codehaus.mojo.versions.utils.ModelNode;
import org.codehaus.mojo.versions.utils.RegexUtils;
import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.ReaderFactory;
import org.codehaus.plexus.util.StringUtils;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import org.codehaus.stax2.XMLInputFactory2;
import static java.util.Collections.singletonMap;
import static java.util.stream.IntStream.range;
/**
* Helper class for modifying pom files.
*
* @author Stephen Connolly
* @since 1.0-alpha-3
*/
public class PomHelper {
public static final String APACHE_MAVEN_PLUGINS_GROUPID = "org.apache.maven.plugins";
/**
* Gets the raw model before any interpolation what-so-ever.
*
* @param project The project to getModel the raw model for.
* @return The raw model.
* @throws IOException if the file is not found or if the file does not parse.
*/
public static Model getRawModel(MavenProject project) throws IOException {
return getRawModel(project.getFile());
}
/**
* Gets the raw model before any interpolation what-so-ever.
*
* @param moduleProjectFile The project file to getModel the raw model for.
* @return The raw model.
* @throws IOException if the file is not found or if the file does not parse.
*/
public static Model getRawModel(File moduleProjectFile) throws IOException {
try (Reader reader =
new BufferedReader(new InputStreamReader(Files.newInputStream(moduleProjectFile.toPath())))) {
Model result = getRawModel(reader);
result.setPomFile(moduleProjectFile);
return result;
}
}
/**
* Gets the current raw model before any interpolation what-so-ever.
*
* @param modifiedPomXMLEventReader The {@link ModifiedPomXMLEventReader} to getModel the raw model for.
* @return The raw model.
* @throws IOException if the file is not found or if the file does not parse.
*/
public static Model getRawModel(ModifiedPomXMLEventReader modifiedPomXMLEventReader) throws IOException {
try (Reader reader =
new StringReader(modifiedPomXMLEventReader.asStringBuilder().toString())) {
Model result = getRawModel(reader);
result.setPomFile(new File(modifiedPomXMLEventReader.getPath()));
return result;
}
}
/**
* Gets the current raw model before any interpolation what-so-ever.
*
* @param reader The {@link Reader} to getModel the raw model for.
* @return The raw model.
* @throws IOException if the file is not found or if the file does not parse.
*/
public static Model getRawModel(Reader reader) throws IOException {
try {
return new MavenXpp3Reader().read(reader);
} catch (XmlPullParserException e) {
throw new IOException(e.getMessage(), e);
}
}
/**
* Searches the pom re-defining the specified property to the specified version.
*
* @param pom The pom to modify.
* @param profileId The profile in which to modify the property.
* @param property The property to modify.
* @param value The new value of the property.
* @return true if a replacement was made.
* @throws XMLStreamException if somethinh went wrong.
*/
public static boolean setPropertyVersion(
final ModifiedPomXMLEventReader pom, final String profileId, final String property, final String value)
throws XMLStreamException {
Stack stack = new Stack<>();
String path = "";
final Pattern propertyRegex;
final Pattern matchScopeRegex;
final Pattern projectProfileId;
boolean inMatchScope = false;
boolean madeReplacement = false;
if (profileId == null) {
propertyRegex = Pattern.compile("/project/properties/" + RegexUtils.quote(property));
matchScopeRegex = Pattern.compile("/project/properties");
projectProfileId = null;
} else {
propertyRegex = Pattern.compile("/project/profiles/profile/properties/" + RegexUtils.quote(property));
matchScopeRegex = Pattern.compile("/project/profiles/profile");
projectProfileId = Pattern.compile("/project/profiles/profile/id");
}
pom.rewind();
while (pom.hasNext()) {
XMLEvent event = pom.nextEvent();
if (event.isStartElement()) {
stack.push(path);
path = path + "/" + event.asStartElement().getName().getLocalPart();
if (propertyRegex.matcher(path).matches()) {
pom.mark(0);
} else if (matchScopeRegex.matcher(path).matches()) {
// we're in a new match scope
// reset any previous partial matches
inMatchScope = profileId == null;
pom.clearMark(0);
pom.clearMark(1);
} else if (profileId != null && projectProfileId.matcher(path).matches()) {
String candidateId = pom.getElementText();
path = stack.pop(); // since getElementText will be after the end element
inMatchScope = profileId.trim().equals(candidateId.trim());
}
}
if (event.isEndElement()) {
if (propertyRegex.matcher(path).matches()) {
pom.mark(1);
} else if (matchScopeRegex.matcher(path).matches()) {
if (inMatchScope && pom.hasMark(0) && pom.hasMark(1)) {
pom.replaceBetween(0, 1, value);
madeReplacement = true;
}
pom.clearMark(0);
pom.clearMark(1);
inMatchScope = false;
}
path = stack.pop();
}
}
return madeReplacement;
}
/**
* Searches the pom re-defining the project version to the specified version.
*
* @param pom The pom to modify.
* @param value The new value of the property.
* @return true if a replacement was made.
* @throws XMLStreamException if somethinh went wrong.
*/
public static boolean setProjectVersion(final ModifiedPomXMLEventReader pom, final String value)
throws XMLStreamException {
return setElementValue(pom, "/project", "version", value, false);
}
/**
* 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.
*
* @param pom pom to modify
* @param parentPath path of the parent element
* @param elementName name of the element to set or create
* @param value the new value of the element
* @return {@code true} if the element was created or replaced
* @throws XMLStreamException if something went wrong
*/
public static boolean setElementValue(
ModifiedPomXMLEventReader pom, String parentPath, String elementName, String value)
throws XMLStreamException {
pom.rewind();
return setElementValue(pom, parentPath, elementName, value, true);
}
/**
* 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 {@code shouldCreate} is {@code true}.
*
* @param pom pom to modify
* @param parentPath path of the parent element
* @param elementName name of the element to set or create
* @param value the new value of the element
* @param shouldCreate should the element be created if it's not found in the first encountered parent element
* matching the parentPath
* @return {@code true} if the element was created or replaced
* @throws XMLStreamException if something went wrong
*/
public static boolean setElementValue(
ModifiedPomXMLEventReader pom, String parentPath, String elementName, String value, boolean shouldCreate)
throws XMLStreamException {
class ElementValueInternal {
private final String parentName;
private final String superParentPath;
private static final int MARK_CHILD_BEGIN = 0;
private static final int MARK_OPTION = 1;
private static final int PARENT_BEGIN = 2;
ElementValueInternal() {
int lastDelimeterIndex = parentPath.lastIndexOf('/');
parentName = parentPath.substring(lastDelimeterIndex + 1);
superParentPath = parentPath.substring(0, lastDelimeterIndex);
}
boolean process(String currentPath) throws XMLStreamException {
boolean replacementMade = false;
while (!replacementMade && pom.hasNext()) {
XMLEvent event = pom.nextEvent();
if (event.isStartElement()) {
String currentElementName =
event.asStartElement().getName().getLocalPart();
// here, we will only mark the beginning of the child or the parent element
if (currentPath.equals(parentPath) && elementName.equals(currentElementName)) {
pom.mark(MARK_CHILD_BEGIN);
} else if (currentPath.equals(superParentPath) && currentElementName.equals(parentName)) {
pom.mark(PARENT_BEGIN);
}
// process child element
replacementMade = process(currentPath + "/" + currentElementName);
} else if (event.isEndElement()) {
// here we're doing the replacement
if (currentPath.equals(parentPath + "/" + elementName)) {
// end of the child
replaceValueInChild();
replacementMade = true;
} else if (shouldCreate && currentPath.equals(parentPath)) {
// end of the parent
replaceValueInParent();
replacementMade = true;
} else {
return false;
}
}
}
return replacementMade;
}
private void replaceValueInChild() {
pom.mark(MARK_OPTION);
if (pom.getBetween(MARK_CHILD_BEGIN, MARK_OPTION).length() > 0) {
pom.replaceBetween(0, 1, value);
} else {
pom.replace(String.format("<%1$s>%2$s%1$s>", elementName, value));
}
}
private void replaceValueInParent() {
pom.mark(MARK_OPTION);
if (pom.hasMark(PARENT_BEGIN)) {
if (pom.getBetween(PARENT_BEGIN, MARK_OPTION).length() > 0) {
pom.replace(String.format("<%2$s>%3$s%2$s>%1$s>", parentName, elementName, value));
} else {
pom.replace(String.format("<%1$s><%2$s>%3$s%2$s>%1$s>", parentName, elementName, value));
}
} else {
pom.replace(String.format("<%1$s><%2$s>%3$s%2$s>%1$s>", parentName, elementName, value));
}
}
}
try {
pom.rewind();
return new ElementValueInternal().process("");
} finally {
range(0, 3).forEach(pom::clearMark);
}
}
/**
* Retrieves the project version from the pom.
*
* @param pom The pom.
* @return the project version or null if the project version is not defined (i.e. inherited from
* parent version).
* @throws XMLStreamException if something went wrong.
*/
public static String getProjectVersion(final ModifiedPomXMLEventReader pom) throws XMLStreamException {
Stack stack = new Stack<>();
String path = "";
final Pattern matchScopeRegex = Pattern.compile("/project/version");
pom.rewind();
while (pom.hasNext()) {
XMLEvent event = pom.nextEvent();
if (event.isStartElement()) {
stack.push(path);
path = path + "/" + event.asStartElement().getName().getLocalPart();
if (matchScopeRegex.matcher(path).matches()) {
pom.mark(0);
}
}
if (event.isEndElement()) {
if (matchScopeRegex.matcher(path).matches()) {
pom.mark(1);
if (pom.hasMark(0) && pom.hasMark(1)) {
return pom.getBetween(0, 1).trim();
}
pom.clearMark(0);
pom.clearMark(1);
}
path = stack.pop();
}
}
return null;
}
/**
* Searches the pom re-defining the project version to the specified version.
*
* @param pom The pom to modify.
* @param value The new value of the property.
* @return true if a replacement was made.
* @throws XMLStreamException if somethinh went wrong.
*/
public static boolean setProjectParentVersion(final ModifiedPomXMLEventReader pom, final String value)
throws XMLStreamException {
Stack stack = new Stack<>();
String path = "";
final Pattern matchScopeRegex;
boolean madeReplacement = false;
matchScopeRegex = Pattern.compile("/project/parent/version");
pom.rewind();
while (pom.hasNext()) {
XMLEvent event = pom.nextEvent();
if (event.isStartElement()) {
stack.push(path);
path = path + "/" + event.asStartElement().getName().getLocalPart();
if (matchScopeRegex.matcher(path).matches()) {
pom.mark(0);
}
}
if (event.isEndElement()) {
if (matchScopeRegex.matcher(path).matches()) {
pom.mark(1);
if (pom.hasMark(0) && pom.hasMark(1)) {
pom.replaceBetween(0, 1, value);
madeReplacement = true;
}
pom.clearMark(0);
pom.clearMark(1);
}
path = stack.pop();
}
}
return madeReplacement;
}
/**
* Searches the pom re-defining the specified dependency to the specified version.
*
* @param pom The pom to modify.
* @param groupId The groupId of the dependency.
* @param artifactId The artifactId of the dependency.
* @param oldVersion The old version of the dependency.
* @param newVersion The new version of the dependency.
* @param model The model to getModel the project properties from.
* @return true if a replacement was made.
* @throws XMLStreamException if something went wrong.
*/
@SuppressWarnings("checkstyle:MethodLength")
public static boolean setDependencyVersion(
final ModifiedPomXMLEventReader pom,
final String groupId,
final String artifactId,
final String oldVersion,
final String newVersion,
final Model model)
throws XMLStreamException {
Stack stack = new Stack<>();
String path = "";
Set implicitPaths = new HashSet<>(Arrays.asList(
"/project/parent/groupId", "/project/parent/artifactId",
"/project/parent/version", "/project/groupId",
"/project/artifactId", "/project/version"));
Map implicitProperties = new HashMap<>();
for (Map.Entry