You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by jv...@apache.org on 2006/01/12 01:35:52 UTC

svn commit: r368187 - in /maven/components/branches/maven-2.0.x: integration-tests/maven-core-it-plugin/src/main/java/org/apache/maven/plugin/coreit/ maven-core-it/ maven-core-it/it0088/ maven-core-it/it0090/ maven-core-it/it0090/src/ maven-core-it/it0...

Author: jvanzyl
Date: Wed Jan 11 16:35:23 2006
New Revision: 368187

URL: http://svn.apache.org/viewcvs?rev=368187&view=rev
Log:
[MNG-1927] merging in changes from the trunk

Added:
    maven/components/branches/maven-2.0.x/integration-tests/maven-core-it-plugin/src/main/java/org/apache/maven/plugin/coreit/InterpolatedEnvarConfigurationMojo.java   (props changed)
      - copied unchanged from r368108, maven/components/trunk/integration-tests/maven-core-it-plugin/src/main/java/org/apache/maven/plugin/coreit/InterpolatedEnvarConfigurationMojo.java
    maven/components/branches/maven-2.0.x/maven-core-it/it0090/
      - copied from r368108, maven/components/trunk/maven-core-it/it0090/
    maven/components/branches/maven-2.0.x/maven-core-it/it0090/expected-results.txt   (props changed)
      - copied unchanged from r368108, maven/components/trunk/maven-core-it/it0090/expected-results.txt
    maven/components/branches/maven-2.0.x/maven-core-it/it0090/goals.txt   (props changed)
      - copied unchanged from r368108, maven/components/trunk/maven-core-it/it0090/goals.txt
    maven/components/branches/maven-2.0.x/maven-core-it/it0090/log.txt   (props changed)
      - copied unchanged from r368108, maven/components/trunk/maven-core-it/it0090/log.txt
    maven/components/branches/maven-2.0.x/maven-core-it/it0090/pom.xml   (props changed)
      - copied unchanged from r368108, maven/components/trunk/maven-core-it/it0090/pom.xml
    maven/components/branches/maven-2.0.x/maven-core-it/it0090/src/
      - copied from r368108, maven/components/trunk/maven-core-it/it0090/src/
    maven/components/branches/maven-2.0.x/maven-core-it/it0090/src/main/
      - copied from r368108, maven/components/trunk/maven-core-it/it0090/src/main/
    maven/components/branches/maven-2.0.x/maven-core-it/it0090/src/main/resources/
      - copied from r368108, maven/components/trunk/maven-core-it/it0090/src/main/resources/
    maven/components/branches/maven-2.0.x/maven-core-it/it0090/src/main/resources/test.properties   (props changed)
      - copied unchanged from r368108, maven/components/trunk/maven-core-it/it0090/src/main/resources/test.properties
    maven/components/branches/maven-2.0.x/maven-core-it/it0090/src/test/
      - copied from r368108, maven/components/trunk/maven-core-it/it0090/src/test/
    maven/components/branches/maven-2.0.x/maven-core-it/it0090/src/test/java/
      - copied from r368108, maven/components/trunk/maven-core-it/it0090/src/test/java/
    maven/components/branches/maven-2.0.x/maven-core-it/it0090/src/test/java/org/
      - copied from r368108, maven/components/trunk/maven-core-it/it0090/src/test/java/org/
    maven/components/branches/maven-2.0.x/maven-core-it/it0090/src/test/java/org/apache/
      - copied from r368108, maven/components/trunk/maven-core-it/it0090/src/test/java/org/apache/
    maven/components/branches/maven-2.0.x/maven-core-it/it0090/src/test/java/org/apache/maven/
      - copied from r368108, maven/components/trunk/maven-core-it/it0090/src/test/java/org/apache/maven/
    maven/components/branches/maven-2.0.x/maven-core-it/it0090/src/test/java/org/apache/maven/it0090/
      - copied from r368108, maven/components/trunk/maven-core-it/it0090/src/test/java/org/apache/maven/it0090/
    maven/components/branches/maven-2.0.x/maven-core-it/it0090/src/test/java/org/apache/maven/it0090/PomInterpolationTest.java   (props changed)
      - copied unchanged from r368108, maven/components/trunk/maven-core-it/it0090/src/test/java/org/apache/maven/it0090/PomInterpolationTest.java
Modified:
    maven/components/branches/maven-2.0.x/integration-tests/maven-core-it-plugin/src/main/java/org/apache/maven/plugin/coreit/InterpolatedPomConfigurationMojo.java
    maven/components/branches/maven-2.0.x/integration-tests/maven-core-it-plugin/src/main/java/org/apache/maven/plugin/coreit/PackagingMojo.java
    maven/components/branches/maven-2.0.x/maven-core-it/it0088/expected-results.txt
    maven/components/branches/maven-2.0.x/maven-core-it/it0088/pom.xml
    maven/components/branches/maven-2.0.x/maven-core-it/maven-core-it.bat
    maven/components/branches/maven-2.0.x/maven-project/src/main/java/org/apache/maven/project/interpolation/RegexBasedModelInterpolator.java
    maven/components/branches/maven-2.0.x/maven-project/src/test/java/org/apache/maven/project/interpolation/RegexBasedModelInterpolatorTest.java

Propchange: maven/components/branches/maven-2.0.x/integration-tests/maven-core-it-plugin/src/main/java/org/apache/maven/plugin/coreit/InterpolatedEnvarConfigurationMojo.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/components/branches/maven-2.0.x/integration-tests/maven-core-it-plugin/src/main/java/org/apache/maven/plugin/coreit/InterpolatedEnvarConfigurationMojo.java
------------------------------------------------------------------------------
    svn:keywords = "Author Date Id Revision"

Modified: maven/components/branches/maven-2.0.x/integration-tests/maven-core-it-plugin/src/main/java/org/apache/maven/plugin/coreit/InterpolatedPomConfigurationMojo.java
URL: http://svn.apache.org/viewcvs/maven/components/branches/maven-2.0.x/integration-tests/maven-core-it-plugin/src/main/java/org/apache/maven/plugin/coreit/InterpolatedPomConfigurationMojo.java?rev=368187&r1=368186&r2=368187&view=diff
==============================================================================
--- maven/components/branches/maven-2.0.x/integration-tests/maven-core-it-plugin/src/main/java/org/apache/maven/plugin/coreit/InterpolatedPomConfigurationMojo.java (original)
+++ maven/components/branches/maven-2.0.x/integration-tests/maven-core-it-plugin/src/main/java/org/apache/maven/plugin/coreit/InterpolatedPomConfigurationMojo.java Wed Jan 11 16:35:23 2006
@@ -22,7 +22,7 @@
     private String basedir;
 
     /**
-     * @parameter expression="${project.build.directory}"
+     * @parameter expression="${projectBuildDirectory}"
      */
     private String projectBuildDirectory;
 

Modified: maven/components/branches/maven-2.0.x/integration-tests/maven-core-it-plugin/src/main/java/org/apache/maven/plugin/coreit/PackagingMojo.java
URL: http://svn.apache.org/viewcvs/maven/components/branches/maven-2.0.x/integration-tests/maven-core-it-plugin/src/main/java/org/apache/maven/plugin/coreit/PackagingMojo.java?rev=368187&r1=368186&r2=368187&view=diff
==============================================================================
--- maven/components/branches/maven-2.0.x/integration-tests/maven-core-it-plugin/src/main/java/org/apache/maven/plugin/coreit/PackagingMojo.java (original)
+++ maven/components/branches/maven-2.0.x/integration-tests/maven-core-it-plugin/src/main/java/org/apache/maven/plugin/coreit/PackagingMojo.java Wed Jan 11 16:35:23 2006
@@ -78,3 +78,4 @@
     }
 
 }
+                    

Modified: maven/components/branches/maven-2.0.x/maven-core-it/it0088/expected-results.txt
URL: http://svn.apache.org/viewcvs/maven/components/branches/maven-2.0.x/maven-core-it/it0088/expected-results.txt?rev=368187&r1=368186&r2=368187&view=diff
==============================================================================
--- maven/components/branches/maven-2.0.x/maven-core-it/it0088/expected-results.txt (original)
+++ maven/components/branches/maven-2.0.x/maven-core-it/it0088/expected-results.txt Wed Jan 11 16:35:23 2006
@@ -1 +1,2 @@
 target/classes/test.properties
+target/mojo-generated.properties

Modified: maven/components/branches/maven-2.0.x/maven-core-it/it0088/pom.xml
URL: http://svn.apache.org/viewcvs/maven/components/branches/maven-2.0.x/maven-core-it/it0088/pom.xml?rev=368187&r1=368186&r2=368187&view=diff
==============================================================================
--- maven/components/branches/maven-2.0.x/maven-core-it/it0088/pom.xml (original)
+++ maven/components/branches/maven-2.0.x/maven-core-it/it0088/pom.xml Wed Jan 11 16:35:23 2006
@@ -28,7 +28,7 @@
           <execution>
             <phase>process-resources</phase>
             <configuration>
-              <pomBuildDirectory>${project.build.directory}</pomBuildDirectory>
+              <projectBuildDirectory>${project.build.directory}</projectBuildDirectory>
             </configuration>
             <goals>
               <goal>generate-properties</goal>

Propchange: maven/components/branches/maven-2.0.x/maven-core-it/it0090/expected-results.txt
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/components/branches/maven-2.0.x/maven-core-it/it0090/expected-results.txt
------------------------------------------------------------------------------
    svn:keywords = "Author Date Id Revision"

Propchange: maven/components/branches/maven-2.0.x/maven-core-it/it0090/goals.txt
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/components/branches/maven-2.0.x/maven-core-it/it0090/goals.txt
------------------------------------------------------------------------------
    svn:keywords = "Author Date Id Revision"

Propchange: maven/components/branches/maven-2.0.x/maven-core-it/it0090/log.txt
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/components/branches/maven-2.0.x/maven-core-it/it0090/log.txt
------------------------------------------------------------------------------
    svn:keywords = "Author Date Id Revision"

Propchange: maven/components/branches/maven-2.0.x/maven-core-it/it0090/pom.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/components/branches/maven-2.0.x/maven-core-it/it0090/pom.xml
------------------------------------------------------------------------------
    svn:keywords = "Author Date Id Revision"

Propchange: maven/components/branches/maven-2.0.x/maven-core-it/it0090/src/main/resources/test.properties
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/components/branches/maven-2.0.x/maven-core-it/it0090/src/main/resources/test.properties
------------------------------------------------------------------------------
    svn:keywords = "Author Date Id Revision"

Propchange: maven/components/branches/maven-2.0.x/maven-core-it/it0090/src/test/java/org/apache/maven/it0090/PomInterpolationTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/components/branches/maven-2.0.x/maven-core-it/it0090/src/test/java/org/apache/maven/it0090/PomInterpolationTest.java
------------------------------------------------------------------------------
    svn:keywords = "Author Date Id Revision"

Modified: maven/components/branches/maven-2.0.x/maven-core-it/maven-core-it.bat
URL: http://svn.apache.org/viewcvs/maven/components/branches/maven-2.0.x/maven-core-it/maven-core-it.bat?rev=368187&r1=368186&r2=368187&view=diff
==============================================================================
--- maven/components/branches/maven-2.0.x/maven-core-it/maven-core-it.bat (original)
+++ maven/components/branches/maven-2.0.x/maven-core-it/maven-core-it.bat Wed Jan 11 16:35:23 2006
@@ -20,5 +20,7 @@
 @REM NOTE: for simplicity, only Windows NT/2000/XP is current supported
 @REM This also assumes that M2_HOME and JAVA_HOME are set, which are verified in the bootstrap script only
 
+SET MAVEN_TEST_ENVAR=MAVEN_TEST_ENVAR_VALUE
+
 "%JAVA_HOME%\bin\java.exe" -Dmaven.home="%M2_HOME%" %MAVEN_OPTS% -cp "..\maven-core-it-verifier\target\maven-core-it-verifier.jar" org.apache.maven.it.Verifier %*
 

Modified: maven/components/branches/maven-2.0.x/maven-project/src/main/java/org/apache/maven/project/interpolation/RegexBasedModelInterpolator.java
URL: http://svn.apache.org/viewcvs/maven/components/branches/maven-2.0.x/maven-project/src/main/java/org/apache/maven/project/interpolation/RegexBasedModelInterpolator.java?rev=368187&r1=368186&r2=368187&view=diff
==============================================================================
--- maven/components/branches/maven-2.0.x/maven-project/src/main/java/org/apache/maven/project/interpolation/RegexBasedModelInterpolator.java (original)
+++ maven/components/branches/maven-2.0.x/maven-project/src/main/java/org/apache/maven/project/interpolation/RegexBasedModelInterpolator.java Wed Jan 11 16:35:23 2006
@@ -20,16 +20,19 @@
 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.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.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.IOException;
 import java.io.StringReader;
 import java.io.StringWriter;
 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.
@@ -42,10 +45,25 @@
     extends AbstractLogEnabled
     implements ModelInterpolator
 {
-    public Model interpolate( Model project, Map context )
+    private static final Pattern EXPRESSION_PATTERN = Pattern.compile( "\\$\\{(pom\\.|project\\.|env\\.)?([^}]+)\\}" );
+
+    private Properties envars;
+
+    public RegexBasedModelInterpolator( Properties envars )
+    {
+        this.envars = envars;
+    }
+
+    public RegexBasedModelInterpolator()
+        throws IOException
+    {
+        envars = CommandLineUtils.getSystemEnvVars();
+    }
+
+    public Model interpolate( Model model, Map context )
         throws ModelInterpolationException
     {
-        return interpolate( project, context, true );
+        return interpolate( model, context, true );
     }
 
     /**
@@ -54,7 +72,7 @@
      * <br/>
      * <b>NOTE:</b> This will result in a different instance of Model being returned!!!
      *
-     * @param model The inbound Model instance, to serialize and reference for expression resolution
+     * @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!
      */
@@ -74,31 +92,14 @@
         }
 
         String serializedModel = sWriter.toString();
-
-        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" );
+        serializedModel = interpolateInternal( serializedModel, model, context );
 
         StringReader sReader = new StringReader( serializedModel );
 
         MavenXpp3Reader modelReader = new MavenXpp3Reader();
         try
         {
-            model = modelReader.read( sReader, strict );
+            model = modelReader.read( sReader );
         }
         catch ( IOException e )
         {
@@ -112,6 +113,73 @@
         }
 
         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 );
+            }
+
+            if ( value == null )
+            {
+                try
+                {
+                    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 ( value == null )
+            {
+                value = envars.getProperty( realExpr );
+            }
+
+            // 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/branches/maven-2.0.x/maven-project/src/test/java/org/apache/maven/project/interpolation/RegexBasedModelInterpolatorTest.java
URL: http://svn.apache.org/viewcvs/maven/components/branches/maven-2.0.x/maven-project/src/test/java/org/apache/maven/project/interpolation/RegexBasedModelInterpolatorTest.java?rev=368187&r1=368186&r2=368187&view=diff
==============================================================================
--- maven/components/branches/maven-2.0.x/maven-project/src/test/java/org/apache/maven/project/interpolation/RegexBasedModelInterpolatorTest.java (original)
+++ maven/components/branches/maven-2.0.x/maven-project/src/test/java/org/apache/maven/project/interpolation/RegexBasedModelInterpolatorTest.java Wed Jan 11 16:35:23 2006
@@ -24,6 +24,7 @@
 
 import java.util.Collections;
 import java.util.Map;
+import java.util.Properties;
 
 /**
  * @author jdcasey
@@ -43,7 +44,7 @@
     }
 
     public void testShouldInterpolateDependencyVersionToSetSameAsProjectVersion()
-        throws ModelInterpolationException
+        throws Exception
     {
         Model model = new Model();
         model.setVersion( "3.8.1" );
@@ -59,7 +60,7 @@
     }
 
     public void testShouldNotInterpolateDependencyVersionWithInvalidReference()
-        throws ModelInterpolationException
+        throws Exception
     {
         Model model = new Model();
         model.setVersion( "3.8.1" );
@@ -90,7 +91,7 @@
     }
 
     public void testTwoReferences()
-        throws ModelInterpolationException
+        throws Exception
     {
         Model model = new Model();
         model.setVersion( "3.8.1" );
@@ -107,7 +108,7 @@
     }
 
     public void testBasedir()
-        throws ModelInterpolationException
+        throws Exception
     {
         Model model = new Model();
         model.setVersion( "3.8.1" );
@@ -123,5 +124,65 @@
 
         assertEquals( "file://localhost/myBasedir/temp-repo",
                       ( (Repository) out.getRepositories().get( 0 ) ).getUrl() );
+    }
+
+    public void testEnvars()
+        throws Exception
+    {
+        Properties envars = new Properties();
+
+        envars.setProperty( "HOME", "/path/to/home" );
+
+        Model model = new Model();
+
+        Properties modelProperties = new Properties();
+
+        modelProperties.setProperty( "outputDirectory", "${env.HOME}" );
+
+        model.setProperties( modelProperties );
+
+        Model out = new RegexBasedModelInterpolator( envars ).interpolate( model, context );
+
+        assertEquals( out.getProperties().getProperty( "outputDirectory" ), "/path/to/home" );
+    }
+
+    public void testEnvarExpressionThatEvaluatesToNullReturnsTheLiteralString()
+        throws Exception
+    {
+        Properties envars = new Properties();
+
+        Model model = new Model();
+
+        Properties modelProperties = new Properties();
+
+        modelProperties.setProperty( "outputDirectory", "${env.DOES_NOT_EXIST}" );
+
+        model.setProperties( modelProperties );
+
+        Model out = new RegexBasedModelInterpolator( envars ).interpolate( model, context );
+
+        System.out.println( ">>> " + out.getProperties().getProperty( "outputDirectory" ) );        
+
+        assertEquals( out.getProperties().getProperty( "outputDirectory" ), "${env.DOES_NOT_EXIST}" );
+    }
+
+
+
+    public void testExpressionThatEvaluatesToNullReturnsTheLiteralString()
+        throws Exception
+    {
+        Model model = new Model();
+
+        Properties modelProperties = new Properties();
+
+        modelProperties.setProperty( "outputDirectory", "${DOES_NOT_EXIST}" );
+
+        model.setProperties( modelProperties );
+
+        Model out = new RegexBasedModelInterpolator().interpolate( model, context );
+
+        System.out.println( ">>> " + out.getProperties().getProperty( "outputDirectory" ) );
+
+        assertEquals( out.getProperties().getProperty( "outputDirectory" ), "${DOES_NOT_EXIST}" );
     }
 }