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 2008/06/14 00:23:20 UTC

svn commit: r667675 - in /maven/components/branches/john-2.0.x-plxInterpolation: ./ maven-project/ maven-project/src/main/java/org/apache/maven/project/ maven-project/src/main/java/org/apache/maven/project/interpolation/ maven-project/src/main/resource...

Author: jdcasey
Date: Fri Jun 13 15:23:20 2008
New Revision: 667675

URL: http://svn.apache.org/viewvc?rev=667675&view=rev
Log:
Creating branch to do refactor of the maven-project interpolator to use plexus-interpolation (used to be the interpolation code from plexus-utils, which is what the assembly plugin uses).

Added:
    maven/components/branches/john-2.0.x-plxInterpolation/
      - copied from r667135, maven/components/branches/maven-2.0.x/
Modified:
    maven/components/branches/john-2.0.x-plxInterpolation/maven-project/pom.xml
    maven/components/branches/john-2.0.x-plxInterpolation/maven-project/src/main/java/org/apache/maven/project/DefaultMavenProjectBuilder.java
    maven/components/branches/john-2.0.x-plxInterpolation/maven-project/src/main/java/org/apache/maven/project/interpolation/ModelInterpolator.java
    maven/components/branches/john-2.0.x-plxInterpolation/maven-project/src/main/java/org/apache/maven/project/interpolation/RegexBasedModelInterpolator.java
    maven/components/branches/john-2.0.x-plxInterpolation/maven-project/src/main/resources/META-INF/plexus/components.xml
    maven/components/branches/john-2.0.x-plxInterpolation/maven-project/src/test/java/org/apache/maven/project/interpolation/RegexBasedModelInterpolatorTest.java
    maven/components/branches/john-2.0.x-plxInterpolation/pom.xml

Modified: maven/components/branches/john-2.0.x-plxInterpolation/maven-project/pom.xml
URL: http://svn.apache.org/viewvc/maven/components/branches/john-2.0.x-plxInterpolation/maven-project/pom.xml?rev=667675&r1=667135&r2=667675&view=diff
==============================================================================
--- maven/components/branches/john-2.0.x-plxInterpolation/maven-project/pom.xml (original)
+++ maven/components/branches/john-2.0.x-plxInterpolation/maven-project/pom.xml Fri Jun 13 15:23:20 2008
@@ -32,6 +32,10 @@
     and to retrieve remote models as required.</description>
   <dependencies>
     <dependency>
+      <groupId>org.codehaus.plexus</groupId>
+      <artifactId>plexus-interpolation</artifactId>
+    </dependency>
+    <dependency>
       <groupId>org.apache.maven</groupId>
       <artifactId>maven-settings</artifactId>
     </dependency>

Modified: maven/components/branches/john-2.0.x-plxInterpolation/maven-project/src/main/java/org/apache/maven/project/DefaultMavenProjectBuilder.java
URL: http://svn.apache.org/viewvc/maven/components/branches/john-2.0.x-plxInterpolation/maven-project/src/main/java/org/apache/maven/project/DefaultMavenProjectBuilder.java?rev=667675&r1=667135&r2=667675&view=diff
==============================================================================
--- maven/components/branches/john-2.0.x-plxInterpolation/maven-project/src/main/java/org/apache/maven/project/DefaultMavenProjectBuilder.java (original)
+++ maven/components/branches/john-2.0.x-plxInterpolation/maven-project/src/main/java/org/apache/maven/project/DefaultMavenProjectBuilder.java Fri Jun 13 15:23:20 2008
@@ -95,7 +95,6 @@
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
-import java.util.Properties;
 import java.util.Set;
 import java.util.TreeMap;
 
@@ -983,46 +982,7 @@
 
         activeProfiles.addAll( injectedProfiles );
 
-        // We don't need all the project methods that are added over those in the model, but we do need basedir
-        Map context = new HashMap();
-
-        Build build = model.getBuild();
-
-        if ( projectDir != null )
-        {
-            context.put( "basedir", projectDir.getAbsolutePath() );
-
-            // MNG-1927, MNG-2124, MNG-3355:
-            // If the build section is present and the project directory is non-null, we should make
-            // sure interpolation of the directories below uses translated paths.
-            // Afterward, we'll double back and translate any paths that weren't covered during interpolation via the
-            // code below...
-            context.put( "build.directory", pathTranslator.alignToBaseDirectory( build.getDirectory(), projectDir ) );
-            context.put( "build.outputDirectory", pathTranslator.alignToBaseDirectory( build.getOutputDirectory(), projectDir ) );
-            context.put( "build.testOutputDirectory", pathTranslator.alignToBaseDirectory( build.getTestOutputDirectory(), projectDir ) );
-            context.put( "build.sourceDirectory", pathTranslator.alignToBaseDirectory( build.getSourceDirectory(), projectDir ) );
-            context.put( "build.testSourceDirectory", pathTranslator.alignToBaseDirectory( build.getTestSourceDirectory(), projectDir ) );
-        }
-
-        if ( !isSuperPom )
-        {
-            Properties userProps = config.getUserProperties();
-            if ( userProps != null )
-            {
-                context.putAll( userProps );
-            }
-        }
-
-        model = modelInterpolator.interpolate( model, context, strict );
-
-        // second pass allows ${user.home} to work, if it needs to.
-        // [MNG-2339] ensure the system properties are still interpolated for backwards compat, but the model values must win
-        if ( config.getExecutionProperties() != null && !config.getExecutionProperties().isEmpty() )
-        {
-            context.putAll( config.getExecutionProperties() );
-        }
-
-        model = modelInterpolator.interpolate( model, context, strict );
+        model = modelInterpolator.interpolate( model, projectDir, config, getLogger().isDebugEnabled() );
 
         // MNG-3482: Make sure depMgmt is interpolated before merging.
         if ( !isSuperPom )

Modified: maven/components/branches/john-2.0.x-plxInterpolation/maven-project/src/main/java/org/apache/maven/project/interpolation/ModelInterpolator.java
URL: http://svn.apache.org/viewvc/maven/components/branches/john-2.0.x-plxInterpolation/maven-project/src/main/java/org/apache/maven/project/interpolation/ModelInterpolator.java?rev=667675&r1=667135&r2=667675&view=diff
==============================================================================
--- maven/components/branches/john-2.0.x-plxInterpolation/maven-project/src/main/java/org/apache/maven/project/interpolation/ModelInterpolator.java (original)
+++ maven/components/branches/john-2.0.x-plxInterpolation/maven-project/src/main/java/org/apache/maven/project/interpolation/ModelInterpolator.java Fri Jun 13 15:23:20 2008
@@ -20,7 +20,9 @@
  */
 
 import org.apache.maven.model.Model;
+import org.apache.maven.project.ProjectBuilderConfiguration;
 
+import java.io.File;
 import java.util.Map;
 
 /**
@@ -32,9 +34,22 @@
 {
     String ROLE = ModelInterpolator.class.getName();
 
+    /**
+     * @deprecated Use {@link ModelInterpolator#interpolate(Model, File, ProjectBuilderConfiguration, boolean)} instead.
+     */
     Model interpolate( Model project, Map context )
         throws ModelInterpolationException;
 
+    /**
+     * @deprecated Use {@link ModelInterpolator#interpolate(Model, File, ProjectBuilderConfiguration, boolean)} instead.
+     */
     Model interpolate( Model model, Map context, boolean strict )
         throws ModelInterpolationException;
+
+
+    Model interpolate( Model model,
+                       File projectDir,
+                       ProjectBuilderConfiguration config,
+                       boolean debugEnabled )
+        throws ModelInterpolationException;
 }
\ No newline at end of file

Modified: maven/components/branches/john-2.0.x-plxInterpolation/maven-project/src/main/java/org/apache/maven/project/interpolation/RegexBasedModelInterpolator.java
URL: http://svn.apache.org/viewvc/maven/components/branches/john-2.0.x-plxInterpolation/maven-project/src/main/java/org/apache/maven/project/interpolation/RegexBasedModelInterpolator.java?rev=667675&r1=667135&r2=667675&view=diff
==============================================================================
--- maven/components/branches/john-2.0.x-plxInterpolation/maven-project/src/main/java/org/apache/maven/project/interpolation/RegexBasedModelInterpolator.java (original)
+++ maven/components/branches/john-2.0.x-plxInterpolation/maven-project/src/main/java/org/apache/maven/project/interpolation/RegexBasedModelInterpolator.java Fri Jun 13 15:23:20 2008
@@ -22,20 +22,32 @@
 import org.apache.maven.model.Model;
 import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
 import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
+import org.apache.maven.project.DefaultProjectBuilderConfiguration;
+import org.apache.maven.project.ProjectBuilderConfiguration;
+import org.apache.maven.project.path.PathTranslator;
+import org.codehaus.plexus.interpolation.InterpolationException;
+import org.codehaus.plexus.interpolation.MapBasedValueSource;
+import org.codehaus.plexus.interpolation.ObjectBasedValueSource;
+import org.codehaus.plexus.interpolation.PrefixAwareRecursionInterceptor;
+import org.codehaus.plexus.interpolation.PrefixedObjectValueSource;
+import org.codehaus.plexus.interpolation.PrefixedValueSourceWrapper;
+import org.codehaus.plexus.interpolation.RecursionInterceptor;
+import org.codehaus.plexus.interpolation.RegexBasedInterpolator;
+import org.codehaus.plexus.interpolation.ValueSource;
 import org.codehaus.plexus.logging.AbstractLogEnabled;
 import org.codehaus.plexus.logging.Logger;
-import org.codehaus.plexus.util.StringUtils;
-import org.codehaus.plexus.util.cli.CommandLineUtils;
-import org.codehaus.plexus.util.introspection.ReflectionValueExtractor;
 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
 
+import java.io.File;
 import java.io.IOException;
 import java.io.StringReader;
 import java.io.StringWriter;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Iterator;
+import java.util.List;
 import java.util.Map;
 import java.util.Properties;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
 
 /**
  * Use a regular expression search to find and resolve expressions within the POM.
@@ -48,19 +60,43 @@
     extends AbstractLogEnabled
     implements ModelInterpolator
 {
-    private static final Pattern EXPRESSION_PATTERN = Pattern.compile( "\\$\\{(pom\\.|project\\.|env\\.)?([^}]+)\\}" );
+    private static final List PROJECT_PREFIXES = Arrays.asList( new String[]{ "pom.", "project." } );
 
-    private Properties envars;
+    private static final List TRANSLATED_PATH_EXPRESSIONS;
+
+    static
+    {
+        List translatedPrefixes = new ArrayList();
+
+        // MNG-1927, MNG-2124, MNG-3355:
+        // If the build section is present and the project directory is non-null, we should make
+        // sure interpolation of the directories below uses translated paths.
+        // Afterward, we'll double back and translate any paths that weren't covered during interpolation via the
+        // code below...
+        translatedPrefixes.add( "build.directory" );
+        translatedPrefixes.add( "build.outputDirectory" );
+        translatedPrefixes.add( "build.testOutputDirectory" );
+        translatedPrefixes.add( "build.sourceDirectory" );
+        translatedPrefixes.add( "build.testSourceDirectory" );
+        translatedPrefixes.add( "build.scriptSourceDirectory" );
+        translatedPrefixes.add( "reporting.outputDirectory" );
+
+        TRANSLATED_PATH_EXPRESSIONS = translatedPrefixes;
+    }
+
+    private PathTranslator pathTranslator;
 
     public RegexBasedModelInterpolator( Properties envars )
     {
-        this.envars = envars;
     }
 
+    /**
+     * @todo: Remove the throws clause.
+     * @throws IOException This exception is not thrown any more, and needs to be removed.
+     */
     public RegexBasedModelInterpolator()
         throws IOException
     {
-        envars = CommandLineUtils.getSystemEnvVars();
     }
 
     public Model interpolate( Model model, Map context )
@@ -78,6 +114,8 @@
      * @param model   The inbound Model instance, to serialize and reference for expression resolution
      * @param context The other context map to be used during resolution
      * @return The resolved instance of the inbound Model. This is a different instance!
+     *
+     * @deprecated Use {@link ModelInterpolator#interpolate(Model, File, ProjectBuilderConfiguration, boolean)} instead.
      */
     public Model interpolate( Model model, Map context, boolean strict )
         throws ModelInterpolationException
@@ -95,7 +133,15 @@
         }
 
         String serializedModel = sWriter.toString();
-        serializedModel = interpolateInternal( serializedModel, model, context );
+
+        Properties props = new Properties();
+        props.putAll( context );
+
+        serializedModel = interpolateInternal( serializedModel,
+                                               model,
+                                               null,
+                                               new DefaultProjectBuilderConfiguration().setExecutionProperties( props ),
+                                               true );
 
         StringReader sReader = new StringReader( serializedModel );
 
@@ -118,80 +164,159 @@
         return model;
     }
 
-    private String interpolateInternal( String src, Model model, Map context )
+    public Model interpolate( Model model,
+                              File projectDir,
+                              ProjectBuilderConfiguration config,
+                              boolean debugEnabled )
         throws ModelInterpolationException
     {
-        String result = src;
-        Matcher matcher = EXPRESSION_PATTERN.matcher( result );
-        while ( matcher.find() )
+        StringWriter sWriter = new StringWriter();
+
+        MavenXpp3Writer writer = new MavenXpp3Writer();
+        try
         {
-            String wholeExpr = matcher.group( 0 );
-            String realExpr = matcher.group( 2 );
+            writer.write( sWriter, model );
+        }
+        catch ( IOException e )
+        {
+            throw new ModelInterpolationException( "Cannot serialize project model for interpolation.", e );
+        }
+
+        String serializedModel = sWriter.toString();
+        serializedModel = interpolateInternal( serializedModel, model, projectDir, config, debugEnabled );
+
+        StringReader sReader = new StringReader( serializedModel );
+
+        MavenXpp3Reader modelReader = new MavenXpp3Reader();
+        try
+        {
+            model = modelReader.read( sReader );
+        }
+        catch ( IOException e )
+        {
+            throw new ModelInterpolationException(
+                "Cannot read project model from interpolating filter of serialized version.", e );
+        }
+        catch ( XmlPullParserException e )
+        {
+            throw new ModelInterpolationException(
+                "Cannot read project model from interpolating filter of serialized version.", e );
+        }
 
-            Object value = context.get( realExpr );
+        return model;
+    }
 
-            if ( value == null )
+    /**
+     * Interpolates all expressions in the src parameter.
+     * <p>
+     * The algorithm used for each expression is:
+     * <ul>
+     *   <li>If it starts with either "pom." or "project.", the expression is evaluated against the model.</li>
+     *   <li>If the value is null, get the value from the context.</li>
+     *   <li>If the value is null, but the context contains the expression, don't replace the expression string
+     *       with the value, and continue to find other expressions.</li>
+     *   <li>If the value is null, get it from the model properties.</li>
+     *   <li>
+     * @param overrideContext
+     * @param outputDebugMessages
+     */
+    private String interpolateInternal( String src,
+                                        Model model,
+                                        final File projectDir,
+                                        ProjectBuilderConfiguration config,
+                                        boolean debug )
+        throws ModelInterpolationException
+    {
+        Logger logger = getLogger();
+
+        ValueSource baseModelValueSource1 = new PrefixedObjectValueSource( PROJECT_PREFIXES, model, false );
+        ValueSource modelValueSource1 = new PathTranslatingValueSource( baseModelValueSource1,
+                                                                       TRANSLATED_PATH_EXPRESSIONS,
+                                                                       projectDir, pathTranslator );
+
+        ValueSource baseModelValueSource2 = new ObjectBasedValueSource( model );
+        ValueSource modelValueSource2 = new PathTranslatingValueSource( baseModelValueSource2,
+                                                                       TRANSLATED_PATH_EXPRESSIONS,
+                                                                       projectDir, pathTranslator );
+
+        ValueSource basedirValueSource = new PrefixedValueSourceWrapper( new ValueSource(){
+            public Object getValue( String expression )
             {
-                // This may look out of place, but its here for the MNG-2124/MNG-1927 fix described in the project builder
-                if ( context.containsKey( realExpr ) )
+                if ( projectDir != null && "basedir".equals( expression ) )
                 {
-                    // It existed, but was null. Leave it alone.
-                    continue;
+                    return projectDir.getAbsolutePath();
                 }
 
-                value = model.getProperties().getProperty( realExpr );
+                return null;
             }
+        },
+        PROJECT_PREFIXES, true );
+
+        RegexBasedInterpolator interpolator = new RegexBasedInterpolator();
+
+        // NOTE: Order counts here!
+        interpolator.addValueSource( basedirValueSource );
+        interpolator.addValueSource( new MapBasedValueSource( config.getExecutionProperties() ) );
+        interpolator.addValueSource( modelValueSource1 );
+        interpolator.addValueSource( new PrefixedValueSourceWrapper( new MapBasedValueSource( model.getProperties() ), PROJECT_PREFIXES, true ) );
+        interpolator.addValueSource( modelValueSource2 );
+        interpolator.addValueSource( new MapBasedValueSource( config.getUserProperties() ) );
 
-            if ( value == null )
+        RecursionInterceptor recursionInterceptor = new PrefixAwareRecursionInterceptor( PROJECT_PREFIXES );
+
+        String result = src;
+        try
+        {
+            result = interpolator.interpolate( result, "", recursionInterceptor );
+        }
+        catch( InterpolationException e )
+        {
+            throw new ModelInterpolationException( e.getMessage(), e );
+        }
+
+        if ( debug )
+        {
+            List feedback = interpolator.getFeedback();
+            if ( feedback != null && !feedback.isEmpty() )
             {
-                try
-                {
-                    // NOTE: We've already trimmed off any leading expression parts like 'project.'
-                    // or 'pom.', and now we have to ensure that the ReflectionValueExtractor
-                    // doesn't try to do it again.
-                    value = ReflectionValueExtractor.evaluate( realExpr, model, false );
-                }
-                catch ( Exception e )
+                logger.debug( "Maven encountered the following problems during initial POM interpolation:" );
+
+                Object last = null;
+                for ( Iterator it = feedback.iterator(); it.hasNext(); )
                 {
-                    Logger logger = getLogger();
-                    if ( logger != null )
+                    Object next = it.next();
+
+                    if ( next instanceof Throwable )
                     {
-                        logger.debug( "POM interpolation cannot proceed with expression: " + wholeExpr + ". Skipping...", e );
+                        if ( last == null )
+                        {
+                            logger.debug( "", ( (Throwable) next ) );
+                        }
+                        else
+                        {
+                            logger.debug( String.valueOf( last ), ( (Throwable) next ) );
+                        }
                     }
-                }
-            }
-
-            if ( value == null )
-            {
-                value = envars.getProperty( realExpr );
-            }
-
-            // if the expression refers to itself, skip it.
-            if ( String.valueOf( value ).indexOf( wholeExpr ) > -1 )
-            {
-                throw new ModelInterpolationException( wholeExpr, "Expression value '" + value + "' references itself in '" + model.getId() + "'." );
-            }
+                    else
+                    {
+                        if ( last != null )
+                        {
+                            logger.debug( String.valueOf( last ) );
+                        }
 
-            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
+                        last = next;
+                    }
+                }
 
-            else
-            {
-                throw new ModelInterpolationException(
-                    "Expression '" + wholeExpr + "' did not evaluate to anything in the model" );
+                if ( last != null )
+                {
+                    logger.debug( String.valueOf( last ) );
+                }
             }
-*/
         }
 
+        interpolator.clearFeedback();
+
         return result;
     }
 

Modified: maven/components/branches/john-2.0.x-plxInterpolation/maven-project/src/main/resources/META-INF/plexus/components.xml
URL: http://svn.apache.org/viewvc/maven/components/branches/john-2.0.x-plxInterpolation/maven-project/src/main/resources/META-INF/plexus/components.xml?rev=667675&r1=667135&r2=667675&view=diff
==============================================================================
--- maven/components/branches/john-2.0.x-plxInterpolation/maven-project/src/main/resources/META-INF/plexus/components.xml (original)
+++ maven/components/branches/john-2.0.x-plxInterpolation/maven-project/src/main/resources/META-INF/plexus/components.xml Fri Jun 13 15:23:20 2008
@@ -41,6 +41,11 @@
     <component>
       <role>org.apache.maven.project.interpolation.ModelInterpolator</role>
       <implementation>org.apache.maven.project.interpolation.RegexBasedModelInterpolator</implementation>
+      <requirements>
+        <requirement>
+          <role>org.apache.maven.project.path.PathTranslator</role>
+        </requirement>
+      </requirements>
     </component>
 <!--
      |

Modified: maven/components/branches/john-2.0.x-plxInterpolation/maven-project/src/test/java/org/apache/maven/project/interpolation/RegexBasedModelInterpolatorTest.java
URL: http://svn.apache.org/viewvc/maven/components/branches/john-2.0.x-plxInterpolation/maven-project/src/test/java/org/apache/maven/project/interpolation/RegexBasedModelInterpolatorTest.java?rev=667675&r1=667135&r2=667675&view=diff
==============================================================================
--- maven/components/branches/john-2.0.x-plxInterpolation/maven-project/src/test/java/org/apache/maven/project/interpolation/RegexBasedModelInterpolatorTest.java (original)
+++ maven/components/branches/john-2.0.x-plxInterpolation/maven-project/src/test/java/org/apache/maven/project/interpolation/RegexBasedModelInterpolatorTest.java Fri Jun 13 15:23:20 2008
@@ -29,6 +29,7 @@
 
 import java.io.IOException;
 import java.util.Collections;
+import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
@@ -210,9 +211,8 @@
     public void testEnvars()
         throws Exception
     {
-        Properties envars = new Properties();
-
-        envars.setProperty( "HOME", "/path/to/home" );
+        context = new HashMap( context );
+        context.put( "env.HOME", "/path/to/home" );
 
         Model model = new Model();
 
@@ -222,7 +222,7 @@
 
         model.setProperties( modelProperties );
 
-        Model out = new RegexBasedModelInterpolator( envars ).interpolate( model, context );
+        Model out = new RegexBasedModelInterpolator().interpolate( model, context );
 
         assertEquals( out.getProperties().getProperty( "outputDirectory" ), "/path/to/home" );
     }

Modified: maven/components/branches/john-2.0.x-plxInterpolation/pom.xml
URL: http://svn.apache.org/viewvc/maven/components/branches/john-2.0.x-plxInterpolation/pom.xml?rev=667675&r1=667135&r2=667675&view=diff
==============================================================================
--- maven/components/branches/john-2.0.x-plxInterpolation/pom.xml (original)
+++ maven/components/branches/john-2.0.x-plxInterpolation/pom.xml Fri Jun 13 15:23:20 2008
@@ -361,6 +361,11 @@
         <version>1.5.1</version>
       </dependency>
       <dependency>
+        <groupId>org.codehaus.plexus</groupId>
+        <artifactId>plexus-interpolation</artifactId>
+        <version>1.0</version>
+      </dependency>
+      <dependency>
         <groupId>org.apache.maven.wagon</groupId>
         <artifactId>wagon-provider-api</artifactId>
         <version>${wagonVersion}</version>