mirror of https://github.com/apache/maven.git
[MNG-1927] This adds the test mojos required to fix ${pom.build.*} expansion to full paths, the problem was the
new interpolator so the one from 2.0 was used and some envar handling was back ported. Unit tests have been added for envar handling as well as an IT. The maven-core-it script will now set an envar which is used in it0090. git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@368108 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
88f3b941d4
commit
425a841a8b
|
@ -0,0 +1,52 @@
|
|||
package org.apache.maven.plugin.coreit;
|
||||
|
||||
import org.apache.maven.plugin.AbstractMojo;
|
||||
import org.apache.maven.plugin.MojoExecutionException;
|
||||
|
||||
import java.util.Properties;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.File;
|
||||
|
||||
/**
|
||||
* Interpolate some envars that are embedded in the POM and make sure they pass through
|
||||
* the system.
|
||||
*
|
||||
* @goal generate-envar-properties
|
||||
*/
|
||||
public class InterpolatedEnvarConfigurationMojo
|
||||
extends AbstractMojo
|
||||
{
|
||||
/**
|
||||
* @parameter expression="${basedir}"
|
||||
*/
|
||||
private String basedir;
|
||||
|
||||
/**
|
||||
* @parameter expression="${mavenTestEnvar}"
|
||||
*/
|
||||
private String mavenTestEnvar;
|
||||
|
||||
public void execute()
|
||||
throws MojoExecutionException
|
||||
{
|
||||
if ( mavenTestEnvar == null )
|
||||
{
|
||||
throw new MojoExecutionException( "The mavenTestEnvar field should not be null! You must run this using the maven-core-it scripts! ");
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
Properties mojoGeneratedPropeties = new Properties();
|
||||
|
||||
mojoGeneratedPropeties.put( "maven.test.envar", mavenTestEnvar );
|
||||
|
||||
FileOutputStream fos = new FileOutputStream( new File( basedir, "target/mojo-generated.properties" ) );
|
||||
|
||||
mojoGeneratedPropeties.store( fos, "# Properties generated by the execution of a mojo that uses interpolated envar values." );
|
||||
}
|
||||
catch( Exception e )
|
||||
{
|
||||
getLog().error( "Error creating mojo generated properties.", e );
|
||||
}
|
||||
}
|
||||
}
|
|
@ -22,7 +22,7 @@ public class InterpolatedPomConfigurationMojo
|
|||
private String basedir;
|
||||
|
||||
/**
|
||||
* @parameter expression="${project.build.directory}"
|
||||
* @parameter expression="${projectBuildDirectory}"
|
||||
*/
|
||||
private String projectBuildDirectory;
|
||||
|
||||
|
|
|
@ -78,3 +78,4 @@ public class PackagingMojo
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -1 +1,2 @@
|
|||
target/classes/test.properties
|
||||
target/mojo-generated.properties
|
||||
|
|
|
@ -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>
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
target/mojo-generated.properties
|
|
@ -0,0 +1 @@
|
|||
test
|
|
@ -0,0 +1,38 @@
|
|||
+ Error stacktraces are turned on.
|
||||
[INFO] Scanning for projects...
|
||||
[INFO] Searching repository for plugin with prefix: 'clean'.
|
||||
[INFO] ----------------------------------------------------------------------------
|
||||
[INFO] Building Unnamed - org.apache.maven.it:maven-core-it0090:jar:1.0
|
||||
[INFO] task-segment: [clean:clean, test]
|
||||
[INFO] ----------------------------------------------------------------------------
|
||||
[INFO] [clean:clean]
|
||||
[INFO] Deleting directory /home/jvanzyl/js/org/apache/maven/components/trunk/maven-core-it/it0090/target
|
||||
[INFO] [resources:resources]
|
||||
[INFO] Using default encoding to copy filtered resources.
|
||||
[INFO] [core-it:generate-envar-properties {execution: default}]
|
||||
>>>> MAVEN_TEST_ENVAR_VALUE
|
||||
[INFO] [compiler:compile]
|
||||
[INFO] No sources to compile
|
||||
[INFO] [resources:testResources]
|
||||
[INFO] Using default encoding to copy filtered resources.
|
||||
[INFO] [compiler:testCompile]
|
||||
Compiling 1 source file to /home/jvanzyl/js/org/apache/maven/components/trunk/maven-core-it/it0090/target/test-classes
|
||||
[INFO] [surefire:test]
|
||||
[INFO] Setting reports dir: /home/jvanzyl/js/org/apache/maven/components/trunk/maven-core-it/it0090/target/surefire-reports
|
||||
|
||||
-------------------------------------------------------
|
||||
T E S T S
|
||||
-------------------------------------------------------
|
||||
[surefire] Running org.apache.maven.it0090.PomInterpolationTest
|
||||
[surefire] Tests run: 1, Failures: 0, Errors: 0, Time elapsed: 0.017 sec
|
||||
|
||||
Results :
|
||||
[surefire] Tests run: 1, Failures: 0, Errors: 0
|
||||
|
||||
[INFO] ----------------------------------------------------------------------------
|
||||
[INFO] BUILD SUCCESSFUL
|
||||
[INFO] ----------------------------------------------------------------------------
|
||||
[INFO] Total time: 2 seconds
|
||||
[INFO] Finished at: Wed Jan 11 13:34:32 EST 2006
|
||||
[INFO] Final Memory: 4M/13M
|
||||
[INFO] ----------------------------------------------------------------------------
|
|
@ -0,0 +1,48 @@
|
|||
<project>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>org.apache.maven.it</groupId>
|
||||
<artifactId>maven-core-it0090</artifactId>
|
||||
<version>1.0</version>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>3.8.1</version>
|
||||
<type>jar</type>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-core-it-plugin</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>process-resources</phase>
|
||||
<configuration>
|
||||
<mavenTestEnvar>${env.MAVEN_TEST_ENVAR}</mavenTestEnvar>
|
||||
</configuration>
|
||||
<goals>
|
||||
<goal>generate-envar-properties</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</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>
|
||||
</project>
|
|
@ -0,0 +1,2 @@
|
|||
# When we want to test the filtering of envars
|
||||
maven.test.envar=${env.MAVEN_TEST_ENVAR}
|
|
@ -0,0 +1,35 @@
|
|||
package org.apache.maven.it0090;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import java.util.Properties;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
|
||||
public class PomInterpolationTest
|
||||
extends TestCase
|
||||
{
|
||||
private String basedir;
|
||||
|
||||
protected void setUp()
|
||||
throws Exception
|
||||
{
|
||||
basedir = System.getProperty( "basedir" );
|
||||
}
|
||||
|
||||
public void testProjectBuildDirectoryAfterForMojoExecution()
|
||||
throws Exception
|
||||
{
|
||||
Properties testProperties = new Properties();
|
||||
|
||||
File testPropertiesFile = new File( basedir, "target/mojo-generated.properties" );
|
||||
|
||||
assertTrue( testPropertiesFile.exists() );
|
||||
|
||||
testProperties.load( new FileInputStream( testPropertiesFile ) );
|
||||
|
||||
File projectBuildDirectory = new File( basedir, "target" );
|
||||
|
||||
assertEquals( testProperties.getProperty( "maven.test.envar" ), "MAVEN_TEST_ENVAR_VALUE" );
|
||||
}
|
||||
}
|
|
@ -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 %*
|
||||
|
||||
|
|
|
@ -33,5 +33,7 @@ if [ "$1" = "--debug" ]; then
|
|||
echo Debugging verifier on port 5005
|
||||
fi
|
||||
|
||||
export MAVEN_TEST_ENVAR=MAVEN_TEST_ENVAR_VALUE
|
||||
|
||||
java "$jvm_m2_home" $opts -cp "$cp" $verifier $@
|
||||
|
||||
|
|
|
@ -20,16 +20,19 @@ import org.apache.maven.model.Model;
|
|||
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 @@ public class RegexBasedModelInterpolator
|
|||
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 @@ public class RegexBasedModelInterpolator
|
|||
* <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 @@ public class RegexBasedModelInterpolator
|
|||
}
|
||||
|
||||
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 )
|
||||
{
|
||||
|
@ -114,4 +115,71 @@ public class RegexBasedModelInterpolator
|
|||
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;
|
||||
}
|
||||
|
||||
}
|
|
@ -24,6 +24,7 @@ import org.apache.maven.model.Repository;
|
|||
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* @author jdcasey
|
||||
|
@ -43,7 +44,7 @@ public class RegexBasedModelInterpolatorTest
|
|||
}
|
||||
|
||||
public void testShouldInterpolateDependencyVersionToSetSameAsProjectVersion()
|
||||
throws ModelInterpolationException
|
||||
throws Exception
|
||||
{
|
||||
Model model = new Model();
|
||||
model.setVersion( "3.8.1" );
|
||||
|
@ -59,7 +60,7 @@ public class RegexBasedModelInterpolatorTest
|
|||
}
|
||||
|
||||
public void testShouldNotInterpolateDependencyVersionWithInvalidReference()
|
||||
throws ModelInterpolationException
|
||||
throws Exception
|
||||
{
|
||||
Model model = new Model();
|
||||
model.setVersion( "3.8.1" );
|
||||
|
@ -90,7 +91,7 @@ public class RegexBasedModelInterpolatorTest
|
|||
}
|
||||
|
||||
public void testTwoReferences()
|
||||
throws ModelInterpolationException
|
||||
throws Exception
|
||||
{
|
||||
Model model = new Model();
|
||||
model.setVersion( "3.8.1" );
|
||||
|
@ -107,7 +108,7 @@ public class RegexBasedModelInterpolatorTest
|
|||
}
|
||||
|
||||
public void testBasedir()
|
||||
throws ModelInterpolationException
|
||||
throws Exception
|
||||
{
|
||||
Model model = new Model();
|
||||
model.setVersion( "3.8.1" );
|
||||
|
@ -124,4 +125,64 @@ public class RegexBasedModelInterpolatorTest
|
|||
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}" );
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue