PR: MNG-1525

Submitted By: Edwin Punzalan
Reviewed By: John Casey

NOT applying this patch. I found a better solution that will factor the interpolation of the POM into a flexible utility in plexus-utils, and will allow introduction of envar resolution to the POM. It will also make interpolating the settings.xml and profiles.xml using any of a number of expression resolvers (using envar resolution only for now).

BTW, I tried using System.getenv(..) in JDK1.4, and it fails with java.lang.Error and a deprecation message. So, I'm using Edwin's code to extract the envars into a Properties object. We can improve this later.


git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@354462 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
John Dennis Casey 2005-12-06 16:28:07 +00:00
parent 601181f4c2
commit a321da38d6
4 changed files with 77 additions and 74 deletions

View File

@ -14,5 +14,7 @@
<module>maven-plugin-tools-java</module>
<module>maven-plugin-tools-beanshell</module>
<module>maven-plugin-tools-pluggy</module>
<module>maven-plugin-tools-model</module>
<module>maven-plugin-tools-ant</module>
</modules>
</project>
</project>

View File

@ -17,14 +17,20 @@ package org.apache.maven.profiles;
*/
import org.apache.maven.profiles.io.xpp3.ProfilesXpp3Reader;
import org.codehaus.plexus.logging.AbstractLogEnabled;
import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.interpolation.EnvarBasedValueSource;
import org.codehaus.plexus.util.interpolation.RegexBasedInterpolator;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
public class DefaultMavenProfilesBuilder
extends AbstractLogEnabled
implements MavenProfilesBuilder
{
private static final String PROFILES_XML_FILE = "profiles.xml";
@ -42,9 +48,28 @@ public class DefaultMavenProfilesBuilder
FileReader fileReader = null;
try
{
fileReader = new FileReader( profilesXml );
StringWriter sWriter = new StringWriter();
IOUtil.copy( fileReader, sWriter );
String rawInput = sWriter.toString();
try
{
RegexBasedInterpolator interpolator = new RegexBasedInterpolator();
interpolator.addValueSource( new EnvarBasedValueSource() );
rawInput = interpolator.interpolate( rawInput, "settings" );
}
catch ( Exception e )
{
getLogger().warn( "Failed to initialize environment variable resolver. Skipping environment substitution in " + PROFILES_XML_FILE + "." );
getLogger().debug( "Failed to initialize envar resolver. Skipping resolution.", e );
}
profilesRoot = reader.read( fileReader );
StringReader sReader = new StringReader( rawInput );
profilesRoot = reader.read( sReader );
}
finally
{

View File

@ -20,17 +20,16 @@ import org.apache.maven.model.Model;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
import org.codehaus.plexus.logging.AbstractLogEnabled;
import org.codehaus.plexus.logging.Logger;
import org.codehaus.plexus.util.StringUtils;
import org.codehaus.plexus.util.introspection.ReflectionValueExtractor;
import org.codehaus.plexus.util.interpolation.EnvarBasedValueSource;
import org.codehaus.plexus.util.interpolation.MapBasedValueSource;
import org.codehaus.plexus.util.interpolation.ObjectBasedValueSource;
import org.codehaus.plexus.util.interpolation.RegexBasedInterpolator;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Use a regular expression search to find and resolve expressions within the POM.
@ -43,8 +42,6 @@ public class RegexBasedModelInterpolator
extends AbstractLogEnabled
implements ModelInterpolator
{
private static final Pattern EXPRESSION_PATTERN = Pattern.compile( "\\$\\{(pom\\.|project\\.)?([^}]+)\\}" );
/**
* Serialize the inbound Model instance to a StringWriter, perform the regex replacement to resolve
* POM expressions, then re-parse into the resolved Model instance.
@ -71,7 +68,24 @@ public class RegexBasedModelInterpolator
}
String serializedModel = sWriter.toString();
serializedModel = interpolateInternal( serializedModel, model, context );
RegexBasedInterpolator interpolator = new RegexBasedInterpolator();
interpolator.addValueSource( new MapBasedValueSource( context ) );
interpolator.addValueSource( new MapBasedValueSource( model.getProperties() ) );
interpolator.addValueSource( new ObjectBasedValueSource( model ) );
try
{
interpolator.addValueSource( new EnvarBasedValueSource() );
}
catch ( IOException e )
{
getLogger().warn( "Cannot initialize environment variables resolver. Skipping environmental resolution." );
getLogger().debug( "Failed to initialize envar resolver. Skipping environmental resolution.", e );
}
serializedModel = interpolator.interpolate(serializedModel, "pom|project" );
StringReader sReader = new StringReader( serializedModel );
@ -94,66 +108,4 @@ public class RegexBasedModelInterpolator
return model;
}
private String interpolateInternal( String src, Model model, Map context )
throws ModelInterpolationException
{
String result = src;
Matcher matcher = EXPRESSION_PATTERN.matcher( result );
while ( matcher.find() )
{
String wholeExpr = matcher.group( 0 );
String realExpr = matcher.group( 2 );
Object value = context.get( realExpr );
if ( value == null )
{
value = model.getProperties().getProperty( realExpr );
}
try
{
if ( value == null )
{
value = ReflectionValueExtractor.evaluate( realExpr, model );
}
}
catch ( Exception e )
{
Logger logger = getLogger();
if ( logger != null )
{
logger.debug( "POM interpolation cannot proceed with expression: " + wholeExpr + ". Skipping...",
e );
}
}
// if the expression refers to itself, skip it.
if ( wholeExpr.equals( value ) )
{
throw new ModelInterpolationException( wholeExpr, model.getId() + " references itself." );
}
if ( value != null )
{
result = StringUtils.replace( result, wholeExpr, String.valueOf( value ) );
// could use:
// result = matcher.replaceFirst( stringValue );
// but this could result in multiple lookups of stringValue, and replaceAll is not correct behaviour
matcher.reset( result );
}
/*
// This is the desired behaviour, however there are too many crappy poms in the repo and an issue with the
// timing of executing the interpolation
else
{
throw new ModelInterpolationException(
"Expression '" + wholeExpr + "' did not evaluate to anything in the model" );
}
*/
}
return result;
}
}

View File

@ -21,11 +21,15 @@ import org.codehaus.plexus.logging.AbstractLogEnabled;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.StringUtils;
import org.codehaus.plexus.util.interpolation.EnvarBasedValueSource;
import org.codehaus.plexus.util.interpolation.RegexBasedInterpolator;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
/**
* @author jdcasey
@ -85,10 +89,30 @@ public class DefaultMavenSettingsBuilder
try
{
reader = new FileReader( settingsFile );
StringWriter sWriter = new StringWriter();
IOUtil.copy( reader, sWriter );
String rawInput = sWriter.toString();
try
{
RegexBasedInterpolator interpolator = new RegexBasedInterpolator();
interpolator.addValueSource( new EnvarBasedValueSource() );
rawInput = interpolator.interpolate( rawInput, "settings" );
}
catch ( Exception e )
{
getLogger().warn( "Failed to initialize environment variable resolver. Skipping environment substitution in settings." );
getLogger().debug( "Failed to initialize envar resolver. Skipping resolution.", e );
}
StringReader sReader = new StringReader( rawInput );
SettingsXpp3Reader modelReader = new SettingsXpp3Reader();
settings = modelReader.read( reader );
settings = modelReader.read( sReader );
RuntimeInfo rtInfo = new RuntimeInfo( settings );