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 org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
import org.apache.maven.artifact.versioning.VersionRange;
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.io.xpp3.MavenXpp3Reader;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.profiles.ProfileManager;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.MavenProjectBuilder;
import org.apache.maven.project.ProjectBuildingException;
import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader;
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 javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.XMLEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Enumeration;
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.Properties;
import java.util.Set;
import java.util.Stack;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 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 get 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 get 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
{
FileInputStream input = null;
try
{
input = new FileInputStream( moduleProjectFile );
MavenXpp3Reader reader = new MavenXpp3Reader();
return reader.read( input );
}
catch ( XmlPullParserException e )
{
IOException ioe = new IOException( e.getMessage() );
ioe.initCause( e );
throw ioe;
}
finally
{
if ( input != null )
{
input.close();
}
}
}
/**
* Gets the current raw model before any interpolation what-so-ever.
*
* @param modifiedPomXMLEventReader The {@link ModifiedPomXMLEventReader} to get 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
{
StringReader stringReader = null;
try
{
stringReader = new StringReader( modifiedPomXMLEventReader.asStringBuilder().toString() );
MavenXpp3Reader reader = new MavenXpp3Reader();
return reader.read( stringReader );
}
catch ( XmlPullParserException e )
{
IOException ioe = new IOException( e.getMessage() );
ioe.initCause( e );
throw ioe;
}
finally
{
if ( stringReader != null )
{
stringReader.close();
}
}
}
/**
* 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
{
Stack stack = new Stack();
String path = "";
final Pattern matchScopeRegex;
boolean madeReplacement = false;
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 ) )
{
pom.replaceBetween( 0, 1, value );
madeReplacement = true;
}
pom.clearMark( 0 );
pom.clearMark( 1 );
}
path = stack.pop();
}
}
return madeReplacement;
}
/**
* 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;
}
/**
* Gets the parent artifact from the pom.
*
* @param pom The pom.
* @param helper The helper (used to create the artifact).
* @return The parent artifact or null if no parent is specified.
* @throws XMLStreamException if something went wrong.
*/
public static Artifact getProjectParent( final ModifiedPomXMLEventReader pom, VersionsHelper helper )
throws XMLStreamException
{
Stack stack = new Stack();
String path = "";
final Pattern matchScopeRegex = Pattern.compile( "/project/parent((/groupId)|(/artifactId)|(/version))" );
String groupId = null;
String artifactId = null;
String version = null;
pom.rewind();
while ( pom.hasNext() )
{
XMLEvent event = pom.nextEvent();
if ( event.isStartElement() )
{
stack.push( path );
final String elementName = event.asStartElement().getName().getLocalPart();
path = path + "/" + elementName;
if ( matchScopeRegex.matcher( path ).matches() )
{
if ( "groupId".equals( elementName ) )
{
groupId = pom.getElementText().trim();
path = stack.pop();
}
else if ( "artifactId".equals( elementName ) )
{
artifactId = pom.getElementText().trim();
path = stack.pop();
}
else if ( "version".equals( elementName ) )
{
version = pom.getElementText().trim();
path = stack.pop();
}
}
}
if ( event.isEndElement() )
{
path = stack.pop();
}
}
if ( groupId == null || artifactId == null || version == null )
{
return null;
}
return helper.createDependencyArtifact( groupId, artifactId, VersionRange.createFromVersion( version ), "pom",
null, null, false );
}
/**
* 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 get the project properties from.
* @return true if a replacement was made.
* @throws XMLStreamException if something went wrong.
*/
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