You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by jd...@apache.org on 2005/12/06 17:28:14 UTC

svn commit: r354462 - in /maven/components/trunk: maven-plugin-tools/ maven-profile/src/main/java/org/apache/maven/profiles/ maven-project/src/main/java/org/apache/maven/project/interpolation/ maven-settings/src/main/java/org/apache/maven/settings/

Author: jdcasey
Date: Tue Dec  6 08:28:07 2005
New Revision: 354462

URL: http://svn.apache.org/viewcvs?rev=354462&view=rev
Log:
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.

Modified:
    maven/components/trunk/maven-plugin-tools/pom.xml
    maven/components/trunk/maven-profile/src/main/java/org/apache/maven/profiles/DefaultMavenProfilesBuilder.java
    maven/components/trunk/maven-project/src/main/java/org/apache/maven/project/interpolation/RegexBasedModelInterpolator.java
    maven/components/trunk/maven-settings/src/main/java/org/apache/maven/settings/DefaultMavenSettingsBuilder.java

Modified: maven/components/trunk/maven-plugin-tools/pom.xml
URL: http://svn.apache.org/viewcvs/maven/components/trunk/maven-plugin-tools/pom.xml?rev=354462&r1=354461&r2=354462&view=diff
==============================================================================
--- maven/components/trunk/maven-plugin-tools/pom.xml (original)
+++ maven/components/trunk/maven-plugin-tools/pom.xml Tue Dec  6 08:28:07 2005
@@ -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>
\ No newline at end of file
+</project>

Modified: maven/components/trunk/maven-profile/src/main/java/org/apache/maven/profiles/DefaultMavenProfilesBuilder.java
URL: http://svn.apache.org/viewcvs/maven/components/trunk/maven-profile/src/main/java/org/apache/maven/profiles/DefaultMavenProfilesBuilder.java?rev=354462&r1=354461&r2=354462&view=diff
==============================================================================
--- maven/components/trunk/maven-profile/src/main/java/org/apache/maven/profiles/DefaultMavenProfilesBuilder.java (original)
+++ maven/components/trunk/maven-profile/src/main/java/org/apache/maven/profiles/DefaultMavenProfilesBuilder.java Tue Dec  6 08:28:07 2005
@@ -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 @@
             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
             {

Modified: maven/components/trunk/maven-project/src/main/java/org/apache/maven/project/interpolation/RegexBasedModelInterpolator.java
URL: http://svn.apache.org/viewcvs/maven/components/trunk/maven-project/src/main/java/org/apache/maven/project/interpolation/RegexBasedModelInterpolator.java?rev=354462&r1=354461&r2=354462&view=diff
==============================================================================
--- maven/components/trunk/maven-project/src/main/java/org/apache/maven/project/interpolation/RegexBasedModelInterpolator.java (original)
+++ maven/components/trunk/maven-project/src/main/java/org/apache/maven/project/interpolation/RegexBasedModelInterpolator.java Tue Dec  6 08:28:07 2005
@@ -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 @@
     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 @@
         }
 
         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 @@
         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;
-    }
 }

Modified: maven/components/trunk/maven-settings/src/main/java/org/apache/maven/settings/DefaultMavenSettingsBuilder.java
URL: http://svn.apache.org/viewcvs/maven/components/trunk/maven-settings/src/main/java/org/apache/maven/settings/DefaultMavenSettingsBuilder.java?rev=354462&r1=354461&r2=354462&view=diff
==============================================================================
--- maven/components/trunk/maven-settings/src/main/java/org/apache/maven/settings/DefaultMavenSettingsBuilder.java (original)
+++ maven/components/trunk/maven-settings/src/main/java/org/apache/maven/settings/DefaultMavenSettingsBuilder.java Tue Dec  6 08:28:07 2005
@@ -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 @@
             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 );