Resolving: MNG-885

o Extracting basedir from the project instance when PluginParameterExpressionEvaluator is init'ed if project != null...otherwise, using ${user.dir} from sysprops.

o Extracting values for resolution from POM properties before POM instance during POM interpolation, and adding checks to guard against self-reference of POM elements.

o Added three ITs (one contra) to test these resolutions.



git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@291735 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
John Dennis Casey 2005-09-26 20:29:20 +00:00
parent 8f272dc79f
commit da33bafe84
19 changed files with 261 additions and 23 deletions

View File

@ -190,6 +190,12 @@ it0067: Test activation of a profile from the command line.
it0070: Test a RAR generation.
it0071: Verifies that dotted property references work within plugin
configurations.
it0072: Verifies that property references with dotted notation work within
POM interpolation.
-------------------------------------------------------------------------------
- generated sources
@ -248,6 +254,12 @@ it1011: Tests the fail-at-end reactor behavior. First module fails, and second
-------------------------------------------------------------------------------
it1012: Test that the DefaultLifecycleExecutor will throw an exception when
encountering an unknown packaging.
-------------------------------------------------------------------------------
it1015: Test that expressions that self-reference within the POM result in an
error during POM interpolation.
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------

View File

@ -1,3 +1,5 @@
it0072
it0071
it0070
it0067
it0066

View File

@ -0,0 +1 @@
target/foo2

View File

@ -0,0 +1 @@
core-it:touch

View File

@ -0,0 +1,39 @@
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>org.apache.maven.it</groupId>
<artifactId>maven-core-it0071</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<type>jar</type>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-core-it-plugin</artifactId>
<version>1.0-SNAPSHOT</version>
<configuration>
<pluginItem>${m2.foo}</pluginItem>
</configuration>
</plugin>
</plugins>
</build>
<pluginRepositories>
<pluginRepository>
<id>snapshots</id>
<name>Maven Central Plugins Development Repository</name>
<url>http://snapshots.maven.codehaus.org/maven2</url>
</pluginRepository>
</pluginRepositories>
<properties>
<m2.foo>foo2</m2.foo>
</properties>
</project>

View File

@ -0,0 +1 @@
rm ${artifact:org.apache.maven.plugins:maven-core-it-plugin:1.0-SNAPSHOT:maven-plugin}

View File

@ -0,0 +1 @@
target/maven-core-it0072-1.0-SNAPSHOT.jar

View File

@ -0,0 +1 @@
package

View File

@ -0,0 +1,28 @@
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>org.apache.maven.it</groupId>
<artifactId>maven-core-it0072</artifactId>
<packaging>jar</packaging>
<version>${m2.version}</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<type>jar</type>
<scope>test</scope>
</dependency>
</dependencies>
<pluginRepositories>
<pluginRepository>
<id>snapshots</id>
<name>Maven Central Plugins Development Repository</name>
<url>http://snapshots.maven.codehaus.org/maven2</url>
</pluginRepository>
</pluginRepositories>
<properties>
<m2.foo>foo2</m2.foo>
<m2.version>1.0-SNAPSHOT</m2.version>
</properties>
</project>

View File

@ -0,0 +1 @@
target/maven-core-it1015-1.0-SNAPSHOT.jar

View File

@ -0,0 +1 @@
core-it:touch

View File

@ -0,0 +1,23 @@
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>org.apache.maven.it</groupId>
<artifactId>maven-core-it1015</artifactId>
<packaging>jar</packaging>
<version>${version}</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<type>jar</type>
<scope>test</scope>
</dependency>
</dependencies>
<pluginRepositories>
<pluginRepository>
<id>snapshots</id>
<name>Maven Central Plugins Development Repository</name>
<url>http://snapshots.maven.codehaus.org/maven2</url>
</pluginRepository>
</pluginRepositories>
</project>

View File

@ -0,0 +1 @@
rm ${artifact:org.apache.maven.plugins:maven-core-it-plugin:1.0-SNAPSHOT:maven-plugin}

View File

@ -66,6 +66,8 @@ public class PluginParameterExpressionEvaluator
private final MavenProject project;
private final String basedir;
public PluginParameterExpressionEvaluator( MavenSession context, MojoExecution mojoExecution,
PathTranslator pathTranslator, Logger logger, MavenProject project )
{
@ -74,6 +76,26 @@ public class PluginParameterExpressionEvaluator
this.pathTranslator = pathTranslator;
this.logger = logger;
this.project = project;
String basedir = null;
if ( project != null )
{
File projectFile = project.getFile();
// this should always be the case for non-super POM instances...
if ( projectFile != null )
{
basedir = projectFile.getParentFile().getAbsolutePath();
}
}
if ( basedir == null )
{
basedir = System.getProperty( "user.dir" );
}
this.basedir = basedir;
}
public Object evaluate( String expr )
@ -224,7 +246,7 @@ public class PluginParameterExpressionEvaluator
}
else if ( "basedir".equals( expression ) )
{
value = project.getBasedir().getAbsolutePath();
value = basedir;
}
else if ( expression.startsWith( "basedir" ) )
{
@ -232,7 +254,7 @@ public class PluginParameterExpressionEvaluator
if ( pathSeparator > 0 )
{
value = project.getFile().getParentFile().getAbsolutePath() + expression.substring( pathSeparator );
value = basedir + expression.substring( pathSeparator );
}
else
{
@ -242,10 +264,8 @@ public class PluginParameterExpressionEvaluator
if ( value == null )
{
// Check properties that have been injected via profiles before we default over to
// system properties.
if ( project.getProperties() != null )
// Check POM-level properties before we default over to system properties.
if ( project != null && project.getProperties() != null )
{
value = project.getProperties().getProperty( expression );
}

View File

@ -38,6 +38,7 @@ import org.codehaus.plexus.util.dag.CycleDetectedException;
import java.io.File;
import java.util.Collections;
import java.util.List;
import java.util.Properties;
/**
* @author <a href="mailto:jason@maven.org">Jason van Zyl </a>
@ -69,6 +70,71 @@ public class PluginParameterExpressionEvaluatorTest
assertEquals( expected, actual );
}
public void testPOMPropertyExtractionWithMissingProject_WithDotNotation()
throws Exception
{
String key = "m2.name";
String checkValue = "value";
Properties properties = new Properties();
properties.setProperty( key, checkValue );
Model model = new Model();
model.setProperties( properties );
MavenProject project = new MavenProject( model );
ExpressionEvaluator ee = createExpressionEvaluator( project, null );
Object value = ee.evaluate( "${" + key + "}" );
assertEquals( checkValue, value );
}
public void testBasedirExtractionWithMissingProject()
throws Exception
{
ExpressionEvaluator ee = createExpressionEvaluator( null, null );
Object value = ee.evaluate( "${basedir}" );
assertEquals( System.getProperty( "user.dir" ), value );
}
public void testValueExtractionFromSystemPropertiesWithMissingProject()
throws Exception
{
String sysprop = "PPEET_sysprop1";
if ( System.getProperty( sysprop ) == null )
{
System.setProperty( sysprop, "value" );
}
ExpressionEvaluator ee = createExpressionEvaluator( null, null );
Object value = ee.evaluate( "${" + sysprop + "}" );
assertEquals( "value", value );
}
public void testValueExtractionFromSystemPropertiesWithMissingProject_WithDotNotation()
throws Exception
{
String sysprop = "PPEET.sysprop2";
if ( System.getProperty( sysprop ) == null )
{
System.setProperty( sysprop, "value" );
}
ExpressionEvaluator ee = createExpressionEvaluator( null, null );
Object value = ee.evaluate( "${" + sysprop + "}" );
assertEquals( "value", value );
}
private static MavenSession createSession( PlexusContainer container, ArtifactRepository repo )
throws CycleDetectedException
{

View File

@ -31,7 +31,7 @@ import java.util.Set;
* A base class for EAR-processing related tasks.
*
* @author <a href="snicoll@apache.org">Stephane Nicoll</a>
* @version $Id: $
* @version $Id$
*/
public abstract class AbstractEarMojo
extends AbstractMojo

View File

@ -26,7 +26,7 @@ import java.io.IOException;
* A Mojo used to build the application.xml file.
*
* @author <a href="snicoll@apache.org">Stephane Nicoll</a>
* @version $Id: $
* @version $Id$
* @goal generate-application-xml
* @phase generate-resources
* @requiresDependencyResolution test

View File

@ -24,12 +24,38 @@ package org.apache.maven.project.interpolation;
public class ModelInterpolationException
extends Exception
{
/**
* Added: Feb 2, 2005 by jdcasey
*/
public ModelInterpolationException( String expression, Throwable cause )
{
super( "The POM expression: " + expression + " could not be evaluated.", cause );
private String expression;
private String originalMessage;
public ModelInterpolationException( String message, Throwable cause )
{
super( message, cause );
}
public ModelInterpolationException( String expression, String message, Throwable cause )
{
super( "The POM expression: " + expression + " could not be evaluated. Reason: " + message, cause );
this.expression = expression;
this.originalMessage = message;
}
public ModelInterpolationException( String expression, String message )
{
super( "The POM expression: " + expression + " could not be evaluated. Reason: " + message );
this.expression = expression;
this.originalMessage = message;
}
public String getExpression()
{
return expression;
}
public String getOriginalMessage()
{
return originalMessage;
}
}

View File

@ -33,8 +33,11 @@ import java.util.regex.Pattern;
import java.util.Map;
/**
* Use a regular expression search to find and resolve expressions within the POM.
*
* @author jdcasey Created on Feb 3, 2005
* @version $Id$
* @todo Consolidate this logic with the PluginParameterExpressionEvaluator, minus deprecations/bans.
*/
public class RegexBasedModelInterpolator
extends AbstractLogEnabled
@ -43,7 +46,14 @@ public class RegexBasedModelInterpolator
private static final Pattern EXPRESSION_PATTERN = Pattern.compile( "\\$\\{(pom\\.|project\\.)?([^}]+)\\}" );
/**
* Added: Feb 3, 2005 by jdcasey
* Serialize the inbound Model instance to a StringWriter, perform the regex replacement to resolve
* POM expressions, then re-parse into the resolved Model instance.
* <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 context The other context map to be used during resolution
* @return The resolved instance of the inbound Model. This is a different instance!
*/
public Model interpolate( Model model, Map context )
throws ModelInterpolationException
@ -84,10 +94,8 @@ public class RegexBasedModelInterpolator
return model;
}
/**
* Added: Feb 3, 2005 by jdcasey
*/
private String interpolateInternal( String src, Model model, Map context )
throws ModelInterpolationException
{
String result = src;
Matcher matcher = EXPRESSION_PATTERN.matcher( result );
@ -98,17 +106,17 @@ public class RegexBasedModelInterpolator
Object value = context.get( realExpr );
if ( value == null )
{
value = model.getProperties().getProperty( realExpr );
}
try
{
if ( value == null )
{
value = ReflectionValueExtractor.evaluate( realExpr, model );
}
if ( value == null )
{
value = model.getProperties().getProperty( realExpr );
}
}
catch ( Exception e )
{
@ -120,6 +128,12 @@ public class RegexBasedModelInterpolator
}
}
// 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 ) );