This is an automated email from the ASF dual-hosted git repository.

hboutemy pushed a commit to annotated tag maven-invoker-plugin-1.1
in repository https://gitbox.apache.org/repos/asf/maven-invoker-plugin.git

commit c4eb8bb8a70e31ff25e3c45be2f923309cf34b45
Author: Oliver Lamy <ol...@apache.org>
AuthorDate: Thu Nov 29 23:05:54 2007 +0000

    [MINVOKER-12] Interpolate it pom with a different token ${..} -> @...@
    
    
    
    git-svn-id: 
https://svn.apache.org/repos/asf/maven/plugins/trunk/maven-invoker-plugin@599618
 13f79535-47bb-0310-9956-ffa450edef68
---
 .../apache/maven/plugin/invoker/InvokerMojo.java   | 119 ++++++++++++++++++---
 src/site/apt/advance-usage.apt                     |   9 +-
 .../maven/plugin/invoker/InterpolationTest.java    |  41 ++++++-
 src/test/resources/unit/interpolation/pom.xml      |  29 +++++
 4 files changed, 179 insertions(+), 19 deletions(-)

diff --git a/src/main/java/org/apache/maven/plugin/invoker/InvokerMojo.java 
b/src/main/java/org/apache/maven/plugin/invoker/InvokerMojo.java
index a5d1077..1fdfea5 100644
--- a/src/main/java/org/apache/maven/plugin/invoker/InvokerMojo.java
+++ b/src/main/java/org/apache/maven/plugin/invoker/InvokerMojo.java
@@ -23,6 +23,7 @@ import java.io.BufferedReader;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileReader;
+import java.io.FileWriter;
 import java.io.IOException;
 import java.io.PrintStream;
 import java.util.ArrayList;
@@ -50,6 +51,7 @@ import org.codehaus.plexus.util.FileUtils;
 import org.codehaus.plexus.util.IOUtil;
 import org.codehaus.plexus.util.InterpolationFilterReader;
 import org.codehaus.plexus.util.cli.CommandLineException;
+import org.codehaus.plexus.util.xml.XmlStreamReader;
 
 import bsh.EvalError;
 import bsh.Interpreter;
@@ -228,6 +230,9 @@ public class InvokerMojo
      * @since 1.1
      */
     private MavenProject project;    
+    
+    // list to store interpolated pom for delete at the end
+    private List/*File*/ interpolatedPomFiles = new ArrayList();
 
     public void execute()
         throws MojoExecutionException, MojoFailureException
@@ -294,11 +299,19 @@ public class InvokerMojo
 
         final List failures = new ArrayList();
 
-        for ( int i = 0; i < includedPoms.length; i++ )
+        try
         {
-            final String pom = includedPoms[i];
+            for ( int i = 0; i < includedPoms.length; i++ )
+            {
+                final String pom = includedPoms[i];
 
-            runBuild( projectsDir, pom, failures );
+                runBuild( projectsDir, pom, failures );
+            }
+        }
+        finally
+        {
+            // interpolated files cleanup
+            cleanupInterpolatedPomFiles();
         }
 
         if ( !suppressSummaries )
@@ -408,18 +421,17 @@ public class InvokerMojo
     private void runBuild( final File projectsDir, final String pom, final 
List failures )
         throws MojoExecutionException
     {
+        
         File pomFile = new File( projectsDir, pom );
-
         final File basedir = pomFile.getParentFile();
-
-        getLog().info( "Building: " + pom );
-
-        final File outputLog = new File( basedir, "build.log" );
-
+        File interpolatedPomFile = buildInterpolatedPomFile( pomFile, basedir 
);
         FileLogger logger = null;
-
         try
         {
+            getLog().info( "Building: " + pom );
+
+            final File outputLog = new File( basedir, "build.log" );
+
             if ( !noLog )
             {
                 outputLog.getParentFile().mkdirs();
@@ -448,7 +460,7 @@ public class InvokerMojo
                 }
             }
 
-            if ( !prebuild( basedir, pom, failures, logger ) )
+            if ( !prebuild( basedir, interpolatedPomFile, failures, logger ) )
             {
                 getLog().info( "...FAILED[pre-build script returned false]" );
 
@@ -529,8 +541,7 @@ public class InvokerMojo
                 request.setOutputHandler( logger );
             }
 
-            request.setPomFile( pomFile );
-
+            request.setPomFile( interpolatedPomFile );
             if ( profiles != null )
             {
                 request.setProfiles( profiles );
@@ -580,7 +591,7 @@ public class InvokerMojo
 
                 failures.add( pom );
             }
-            else if ( !verify( basedir, pom, failures, logger ) )
+            else if ( !verify( basedir, interpolatedPomFile, failures, logger 
) )
             {
                 if ( !suppressSummaries )
                 {
@@ -589,7 +600,7 @@ public class InvokerMojo
 
                 failures.add( pom );
             }
-            else if (!suppressSummaries )
+            else if ( !suppressSummaries )
             {
                 getLog().info( "...SUCCESS." );
             }
@@ -632,7 +643,7 @@ public class InvokerMojo
         return testProps;
     }
 
-    private boolean verify( final File basedir, final String pom, final List 
failures, final FileLogger logger )
+    private boolean verify( final File basedir, final File pom, final List 
failures, final FileLogger logger )
     {
         boolean result = true;
 
@@ -715,7 +726,7 @@ public class InvokerMojo
         return scriptResult;
     }
 
-    private boolean prebuild( final File basedir, final String pom, final List 
failures, final FileLogger logger )
+    private boolean prebuild( final File basedir, final File pom, final List 
failures, final FileLogger logger )
     {
         boolean result = true;
 
@@ -904,5 +915,79 @@ public class InvokerMojo
 
         return result;
     }
+    
+    protected File buildInterpolatedPomFile( File pomFile, File 
targetDirectory )
+        throws MojoExecutionException
+    {
+        File interpolatedPomFile = new File( targetDirectory, 
"interpolated-pom.xml" );
+        interpolatedPomFiles.add( interpolatedPomFile );
+        Map composite = new CompositeMap( this.project, 
this.interpolationsProperties );
+
+        try
+        {
+            boolean created = interpolatedPomFile.createNewFile();
+            if ( !created )
+            {
+                throw new MojoExecutionException( "fail to create file " + 
interpolatedPomFile.getPath() );
+            }
+        }
+        catch ( IOException e )
+        {
+            throw new MojoExecutionException( "fail to create file " + 
interpolatedPomFile.getPath() );
+        }
+        getLog().debug( "interpolate it pom to create interpolated in " + 
interpolatedPomFile.getPath() );
+
+        BufferedReader reader = null;
+        FileWriter fileWriter = null;
+        try
+        {
+            // pom interpolation with token @...@
+            reader = new BufferedReader( new InterpolationFilterReader( new 
XmlStreamReader( pomFile ), composite, "@",
+                                                                        "@" ) 
);
+            fileWriter = new FileWriter( interpolatedPomFile );
+            String line = null;
+            while ( ( line = reader.readLine() ) != null )
+            {
+                fileWriter.write( line );
+            }
+            fileWriter.flush();
+        }
+        catch ( IOException e )
+        {
+            String message = "error when interpolating it pom";
+            throw new MojoExecutionException( message, e );
+        }
+        finally
+        {
+            // IOUtil in p-u is null check and silently NPE
+            IOUtil.close( reader );
+            IOUtil.close( fileWriter );
+        }
+
+        if ( interpolatedPomFile == null )
+        {
+            // null check : normally impossibe but :-)
+            throw new MojoExecutionException( "pom file is null after 
interpolation" );
+        }
+        return interpolatedPomFile;
+    }
+    
+    private void cleanupInterpolatedPomFiles()
+    {
+        for ( Iterator iterator = this.interpolatedPomFiles.iterator(); 
iterator.hasNext(); )
+        {
+            File file = (File) iterator.next();
+            if ( file.exists() )
+            {
+                try
+                {
+                FileUtils.forceDelete( file );
+                } catch (IOException e)
+                {
+                    getLog().warn( "fail to clean file " + file.getPath() );
+                }
+            }
+        }
+    }
 
 }
diff --git a/src/site/apt/advance-usage.apt b/src/site/apt/advance-usage.apt
index 5a77453..33e33c9 100755
--- a/src/site/apt/advance-usage.apt
+++ b/src/site/apt/advance-usage.apt
@@ -12,10 +12,17 @@ Advance Usage
 
   goals files can be interpolated with some values : 
   
-  * values from the project pom (you can use $\{pom.groupId} in your goals 
file)
+  * values from the project pom (you must use $\{pom.groupId} notation in your 
goals file)
   
   * In the plugin configuration you can add some properties in a 
interpolationsProperties element
+
+* it Pom Interpolation 
+
+  It poms can be interpolated with some values : 
     
+  * values from the project pom (you must use @pom.groupId@ notation in your 
pom file)
+  
+  * In the plugin configuration you can add some properties in a 
interpolationsProperties element    
     
   
    
\ No newline at end of file
diff --git 
a/src/test/java/org/apache/maven/plugin/invoker/InterpolationTest.java 
b/src/test/java/org/apache/maven/plugin/invoker/InterpolationTest.java
index 0a4ec44..9fd8166 100755
--- a/src/test/java/org/apache/maven/plugin/invoker/InterpolationTest.java
+++ b/src/test/java/org/apache/maven/plugin/invoker/InterpolationTest.java
@@ -19,12 +19,14 @@
 package org.apache.maven.plugin.invoker;
 
 import java.io.File;
+import java.io.FileReader;
 import java.util.List;
 import java.util.Properties;
 
 import org.apache.maven.model.Scm;
 import org.apache.maven.plugin.testing.AbstractMojoTestCase;
 import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
+import org.codehaus.plexus.util.IOUtil;
 
 /**
  * @author <a href="mailto:ol...@apache.org";>olamy</a>
@@ -71,10 +73,47 @@ public class InterpolationTest
         properties.put( "cleanProps", "clean" );
         properties.put( "version", "2.0-SNAPSHOT" );
         setVariableValueToObject( invokerMojo, "interpolationsProperties", 
properties );
-        String dirPath = getBasedir() + 
"/src/test/resources/unit/interpolation/";
+        String dirPath = getBasedir() + File.separatorChar + "src" + 
File.separatorChar + "test"
+        + File.separatorChar + "resources" + File.separatorChar + "unit" + 
File.separatorChar + "interpolation";
         List goals = invokerMojo.getGoals( new File( dirPath ) );
         assertEquals( goals.toString(), 2, goals.size() );
         assertEquals( "clean", goals.get( 0 ) );
         assertEquals( "bar:foo:1.0-SNAPSHOT:mygoal", goals.get( 1 ) );
     }
+    
+    public void testPomInterpolation()
+        throws Exception
+    {
+        FileReader fileReader = null;
+        File interpolatedPomFile = null;
+        try
+        {
+            InvokerMojo invokerMojo = new InvokerMojo();
+            setVariableValueToObject( invokerMojo, "goalsFile", "goals.txt" );
+            setVariableValueToObject( invokerMojo, "project", 
buildMavenProjectStub() );
+            Properties properties = new Properties();
+            properties.put( "foo", "bar" );
+            properties.put( "version", "2.0-SNAPSHOT" );
+            setVariableValueToObject( invokerMojo, "interpolationsProperties", 
properties );
+            String dirPath = getBasedir() + File.separatorChar + "src" + 
File.separatorChar + "test"
+                + File.separatorChar + "resources" + File.separatorChar + 
"unit" + File.separatorChar + "interpolation";
+            interpolatedPomFile = invokerMojo.buildInterpolatedPomFile( new 
File( dirPath, "pom.xml" ),
+                                                                        new 
File( getBasedir() + File.separatorChar
+                                                                            + 
"target" ) );
+            fileReader = new FileReader( interpolatedPomFile );
+            String content = IOUtil.toString( fileReader );
+            assertTrue( content.indexOf( 
"<interpolateValue>bar</interpolateValue>" ) > 0 );
+        }
+        finally
+        {
+            if ( fileReader != null )
+            {
+                fileReader.close();
+            }
+            if ( interpolatedPomFile != null )
+            {
+                interpolatedPomFile.delete();
+            }
+        }
+    }
 }
diff --git a/src/test/resources/unit/interpolation/pom.xml 
b/src/test/resources/unit/interpolation/pom.xml
new file mode 100755
index 0000000..ab2014a
--- /dev/null
+++ b/src/test/resources/unit/interpolation/pom.xml
@@ -0,0 +1,29 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements.  See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership.  The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License.  You may obtain a copy of the License at
+
+  http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing,
+software distributed under the License is distributed on an
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+KIND, either express or implied.  See the License for the
+specific language governing permissions and limitations
+under the License.
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0"; 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; 
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/maven-v4_0_0.xsd";>
+  <modelVersion>4.0.0</modelVersion>
+  <groupId>org.apache.maven.test</groupId>
+  <artifactId>unit</artifactId>
+  <packaging>pom</packaging>
+  <version>0.1-SNAPSHOT</version>
+  <properties>
+    <interpolateValue>@foo@</interpolateValue>
+  </properties>
+</project>
\ No newline at end of file

-- 
To stop receiving notification emails like this one, please contact
"commits@maven.apache.org" <commits@maven.apache.org>.

Reply via email to