From a321da38d649d5cce704785bb34d6a684286265c Mon Sep 17 00:00:00 2001 From: John Dennis Casey Date: Tue, 6 Dec 2005 16:28:07 +0000 Subject: [PATCH] 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 --- maven-plugin-tools/pom.xml | 4 +- .../profiles/DefaultMavenProfilesBuilder.java | 29 +++++- .../RegexBasedModelInterpolator.java | 92 +++++-------------- .../settings/DefaultMavenSettingsBuilder.java | 26 +++++- 4 files changed, 77 insertions(+), 74 deletions(-) diff --git a/maven-plugin-tools/pom.xml b/maven-plugin-tools/pom.xml index cc923575b4..fe61489b13 100644 --- a/maven-plugin-tools/pom.xml +++ b/maven-plugin-tools/pom.xml @@ -14,5 +14,7 @@ maven-plugin-tools-java maven-plugin-tools-beanshell maven-plugin-tools-pluggy + maven-plugin-tools-model + maven-plugin-tools-ant - \ No newline at end of file + diff --git a/maven-profile/src/main/java/org/apache/maven/profiles/DefaultMavenProfilesBuilder.java b/maven-profile/src/main/java/org/apache/maven/profiles/DefaultMavenProfilesBuilder.java index ed610f1b6f..88107df387 100644 --- a/maven-profile/src/main/java/org/apache/maven/profiles/DefaultMavenProfilesBuilder.java +++ b/maven-profile/src/main/java/org/apache/maven/profiles/DefaultMavenProfilesBuilder.java @@ -17,14 +17,20 @@ */ 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 ProfilesRoot buildProfiles( File basedir ) 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 { diff --git a/maven-project/src/main/java/org/apache/maven/project/interpolation/RegexBasedModelInterpolator.java b/maven-project/src/main/java/org/apache/maven/project/interpolation/RegexBasedModelInterpolator.java index 9c8fbb7328..48d9319eaf 100644 --- a/maven-project/src/main/java/org/apache/maven/project/interpolation/RegexBasedModelInterpolator.java +++ b/maven-project/src/main/java/org/apache/maven/project/interpolation/RegexBasedModelInterpolator.java @@ -20,17 +20,16 @@ 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 Model interpolate( Model model, Map context ) } 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 Model interpolate( Model model, Map context ) 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; - } } \ No newline at end of file diff --git a/maven-settings/src/main/java/org/apache/maven/settings/DefaultMavenSettingsBuilder.java b/maven-settings/src/main/java/org/apache/maven/settings/DefaultMavenSettingsBuilder.java index 1783549256..2420a79910 100644 --- a/maven-settings/src/main/java/org/apache/maven/settings/DefaultMavenSettingsBuilder.java +++ b/maven-settings/src/main/java/org/apache/maven/settings/DefaultMavenSettingsBuilder.java @@ -21,11 +21,15 @@ 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 @@ private Settings readSettings( File settingsFile ) 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 );