You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@maven.apache.org by jd...@apache.org on 2005/09/27 21:49:52 UTC

svn commit: r292023 - in /maven/components/trunk: maven-core-it-plugin/ maven-core-it-plugin/src/main/java/org/apache/maven/plugin/coreit/ maven-core-it/it0073/ maven-core/src/main/java/org/apache/maven/plugin/ maven-plugin-api/src/main/java/org/apache...

Author: jdcasey
Date: Tue Sep 27 12:49:37 2005
New Revision: 292023

URL: http://svn.apache.org/viewcvs?rev=292023&view=rev
Log:
Resolving: MNG-823...added context-awareness to AbstractMojo, using ContextEnabled interface to avoid breaking Mojo API for direct implementors. Added two mojos in the core-it plugin to use the plugin context map, and an IT - it0073 - to test the whole thing.

Added:
    maven/components/trunk/maven-core-it-plugin/src/main/java/org/apache/maven/plugin/coreit/CatchMojo.java   (with props)
    maven/components/trunk/maven-core-it-plugin/src/main/java/org/apache/maven/plugin/coreit/ThrowMojo.java   (with props)
    maven/components/trunk/maven-core-it/it0073/
    maven/components/trunk/maven-core-it/it0073/expected-results.txt   (with props)
    maven/components/trunk/maven-core-it/it0073/goals.txt   (with props)
    maven/components/trunk/maven-core-it/it0073/pom.xml   (with props)
    maven/components/trunk/maven-core-it/it0073/prebuild-hook.txt   (with props)
    maven/components/trunk/maven-plugin-api/src/main/java/org/apache/maven/plugin/ContextEnabled.java   (with props)
Modified:
    maven/components/trunk/maven-core-it-plugin/pom.xml
    maven/components/trunk/maven-core/src/main/java/org/apache/maven/plugin/DefaultPluginManager.java
    maven/components/trunk/maven-plugin-api/src/main/java/org/apache/maven/plugin/AbstractMojo.java

Modified: maven/components/trunk/maven-core-it-plugin/pom.xml
URL: http://svn.apache.org/viewcvs/maven/components/trunk/maven-core-it-plugin/pom.xml?rev=292023&r1=292022&r2=292023&view=diff
==============================================================================
--- maven/components/trunk/maven-core-it-plugin/pom.xml (original)
+++ maven/components/trunk/maven-core-it-plugin/pom.xml Tue Sep 27 12:49:37 2005
@@ -3,7 +3,7 @@
   <parent>
     <artifactId>maven-plugin-parent</artifactId>
     <groupId>org.apache.maven.plugins</groupId>
-    <version>2.0-beta-1</version>
+    <version>2.0-beta-2</version>
   </parent>
   <modelVersion>4.0.0</modelVersion>
   <artifactId>maven-core-it-plugin</artifactId>
@@ -12,6 +12,11 @@
   <version>1.0-SNAPSHOT</version>
   <inceptionYear>2001</inceptionYear>
   <dependencies>
+    <dependency>
+      <groupId>org.apache.maven</groupId>
+      <artifactId>maven-plugin-api</artifactId>
+      <version>2.0-beta-3-SNAPSHOT</version>
+    </dependency>
     <dependency>
       <groupId>org.apache.maven</groupId>
       <artifactId>maven-project</artifactId>

Added: maven/components/trunk/maven-core-it-plugin/src/main/java/org/apache/maven/plugin/coreit/CatchMojo.java
URL: http://svn.apache.org/viewcvs/maven/components/trunk/maven-core-it-plugin/src/main/java/org/apache/maven/plugin/coreit/CatchMojo.java?rev=292023&view=auto
==============================================================================
--- maven/components/trunk/maven-core-it-plugin/src/main/java/org/apache/maven/plugin/coreit/CatchMojo.java (added)
+++ maven/components/trunk/maven-core-it-plugin/src/main/java/org/apache/maven/plugin/coreit/CatchMojo.java Tue Sep 27 12:49:37 2005
@@ -0,0 +1,78 @@
+package org.apache.maven.plugin.coreit;
+
+import org.apache.maven.plugin.AbstractMojo;
+import org.apache.maven.plugin.MojoExecutionException;
+
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.Writer;
+
+/**
+ * "Catch" a parameter "thrown" by the ThrowMojo through the plugin context, and
+ * write a file based on it's value to the build output directory.
+ * 
+ * @goal catch
+ */
+public class CatchMojo
+    extends AbstractMojo
+{
+
+    /**
+     * @parameter expression="${project.build.directory}"
+     * @required
+     * @readonly
+     */
+    private File outDir;
+    
+    public File getOutDir()
+    {
+        return outDir;
+    }
+    
+    public void setOutDir( File outDir )
+    {
+        this.outDir = outDir;
+    }
+
+    public void execute()
+        throws MojoExecutionException
+    {
+        String value = (String) getPluginContext().get( ThrowMojo.THROWN_PARAMETER );
+
+        if ( !outDir.exists() )
+        {
+            outDir.mkdirs();
+        }
+        
+        File outfile = new File( outDir, value );
+
+        Writer writer = null;
+        try
+        {
+            writer = new FileWriter( outfile );
+            
+            writer.write( value );
+            
+            writer.flush();
+        }
+        catch ( IOException e )
+        {
+            throw new MojoExecutionException( "Cannot write output file: " + outfile, e );
+        }
+        finally
+        {
+            if ( writer != null )
+            {
+                try
+                {
+                    writer.close();
+                }
+                catch ( IOException e )
+                {
+                }
+            }
+        }
+    }
+
+}

Propchange: maven/components/trunk/maven-core-it-plugin/src/main/java/org/apache/maven/plugin/coreit/CatchMojo.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/components/trunk/maven-core-it-plugin/src/main/java/org/apache/maven/plugin/coreit/CatchMojo.java
------------------------------------------------------------------------------
    svn:keywords = "Author Date Id Revision"

Added: maven/components/trunk/maven-core-it-plugin/src/main/java/org/apache/maven/plugin/coreit/ThrowMojo.java
URL: http://svn.apache.org/viewcvs/maven/components/trunk/maven-core-it-plugin/src/main/java/org/apache/maven/plugin/coreit/ThrowMojo.java?rev=292023&view=auto
==============================================================================
--- maven/components/trunk/maven-core-it-plugin/src/main/java/org/apache/maven/plugin/coreit/ThrowMojo.java (added)
+++ maven/components/trunk/maven-core-it-plugin/src/main/java/org/apache/maven/plugin/coreit/ThrowMojo.java Tue Sep 27 12:49:37 2005
@@ -0,0 +1,39 @@
+package org.apache.maven.plugin.coreit;
+
+import org.apache.maven.plugin.AbstractMojo;
+import org.apache.maven.plugin.MojoExecutionException;
+
+/**
+ * "Throw" a parameter into the plugin context, for the "catch" mojo to 
+ * pickup and process.
+ * 
+ * @goal throw
+ */
+public class ThrowMojo
+    extends AbstractMojo
+{
+    
+    public static final String THROWN_PARAMETER = "throw-parameter";
+
+    /**
+     * @parameter expression="${value}" default-value="thrown"
+     */
+    private String value;
+    
+    public void setValue( String value )
+    {
+        this.value = value;
+    }
+    
+    public String getValue()
+    {
+        return value;
+    }
+    
+    public void execute()
+        throws MojoExecutionException
+    {
+        getPluginContext().put( THROWN_PARAMETER, value );
+    }
+
+}

Propchange: maven/components/trunk/maven-core-it-plugin/src/main/java/org/apache/maven/plugin/coreit/ThrowMojo.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/components/trunk/maven-core-it-plugin/src/main/java/org/apache/maven/plugin/coreit/ThrowMojo.java
------------------------------------------------------------------------------
    svn:keywords = "Author Date Id Revision"

Added: maven/components/trunk/maven-core-it/it0073/expected-results.txt
URL: http://svn.apache.org/viewcvs/maven/components/trunk/maven-core-it/it0073/expected-results.txt?rev=292023&view=auto
==============================================================================
--- maven/components/trunk/maven-core-it/it0073/expected-results.txt (added)
+++ maven/components/trunk/maven-core-it/it0073/expected-results.txt Tue Sep 27 12:49:37 2005
@@ -0,0 +1 @@
+target/thrown-value

Propchange: maven/components/trunk/maven-core-it/it0073/expected-results.txt
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/components/trunk/maven-core-it/it0073/expected-results.txt
------------------------------------------------------------------------------
    svn:keywords = "Author Date Id Revision"

Added: maven/components/trunk/maven-core-it/it0073/goals.txt
URL: http://svn.apache.org/viewcvs/maven/components/trunk/maven-core-it/it0073/goals.txt?rev=292023&view=auto
==============================================================================
--- maven/components/trunk/maven-core-it/it0073/goals.txt (added)
+++ maven/components/trunk/maven-core-it/it0073/goals.txt Tue Sep 27 12:49:37 2005
@@ -0,0 +1 @@
+core-it:throw core-it:catch

Propchange: maven/components/trunk/maven-core-it/it0073/goals.txt
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/components/trunk/maven-core-it/it0073/goals.txt
------------------------------------------------------------------------------
    svn:keywords = "Author Date Id Revision"

Added: maven/components/trunk/maven-core-it/it0073/pom.xml
URL: http://svn.apache.org/viewcvs/maven/components/trunk/maven-core-it/it0073/pom.xml?rev=292023&view=auto
==============================================================================
--- maven/components/trunk/maven-core-it/it0073/pom.xml (added)
+++ maven/components/trunk/maven-core-it/it0073/pom.xml Tue Sep 27 12:49:37 2005
@@ -0,0 +1,29 @@
+<project>
+  <modelVersion>4.0.0</modelVersion>
+  <groupId>org.apache.maven.it</groupId>
+  <artifactId>maven-core-it0073</artifactId>
+  <version>1.0-SNAPSHOT</version>
+
+  <repositories>
+    <repository>
+      <id>snapshots</id>
+      <url>http://snapshots.maven.codehaus.org/maven2</url>
+      <releases>
+        <enabled>false</enabled>
+      </releases>
+    </repository>
+  </repositories>
+
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-core-it-plugin</artifactId>
+        
+        <configuration>
+          <value>thrown-value</value>
+        </configuration>
+      </plugin>
+    </plugins>
+  </build>
+</project>

Propchange: maven/components/trunk/maven-core-it/it0073/pom.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/components/trunk/maven-core-it/it0073/pom.xml
------------------------------------------------------------------------------
    svn:keywords = "Author Date Id Revision"

Added: maven/components/trunk/maven-core-it/it0073/prebuild-hook.txt
URL: http://svn.apache.org/viewcvs/maven/components/trunk/maven-core-it/it0073/prebuild-hook.txt?rev=292023&view=auto
==============================================================================
--- maven/components/trunk/maven-core-it/it0073/prebuild-hook.txt (added)
+++ maven/components/trunk/maven-core-it/it0073/prebuild-hook.txt Tue Sep 27 12:49:37 2005
@@ -0,0 +1 @@
+rm ${artifact:org.apache.maven.plugins:maven-core-it-plugin:1.0-SNAPSHOT:maven-plugin}

Propchange: maven/components/trunk/maven-core-it/it0073/prebuild-hook.txt
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/components/trunk/maven-core-it/it0073/prebuild-hook.txt
------------------------------------------------------------------------------
    svn:keywords = "Author Date Id Revision"

Modified: maven/components/trunk/maven-core/src/main/java/org/apache/maven/plugin/DefaultPluginManager.java
URL: http://svn.apache.org/viewcvs/maven/components/trunk/maven-core/src/main/java/org/apache/maven/plugin/DefaultPluginManager.java?rev=292023&r1=292022&r2=292023&view=diff
==============================================================================
--- maven/components/trunk/maven-core/src/main/java/org/apache/maven/plugin/DefaultPluginManager.java (original)
+++ maven/components/trunk/maven-core/src/main/java/org/apache/maven/plugin/DefaultPluginManager.java Tue Sep 27 12:49:37 2005
@@ -503,7 +503,7 @@
         PluginDescriptor pluginDescriptor = mojoDescriptor.getPluginDescriptor();
 
         PlexusContainer pluginContainer = getPluginContainer( pluginDescriptor );
-
+        
         // if this is the first time this plugin has been used, the plugin's container will only
         // contain the plugin's artifact in isolation; we need to finish resolving the plugin's
         // dependencies, and add them to the container.
@@ -516,6 +516,27 @@
             return null;
         }
 
+        if ( plugin instanceof ContextEnabled )
+        {
+            Map pluginContext;
+            try
+            {
+                pluginContext = (Map) pluginContainer.getContext().get( ContextEnabled.PLUGIN_CONTEXT_SESSION_KEY );
+            }
+            catch ( ContextException e )
+            {
+                // this is thrown the first time for each plugin, since the map hasn't been initialized in the
+                // new plugin's container context.
+                getLogger().debug( "Initializing plugin context map for plugin: " + pluginDescriptor.getPluginLookupKey() );
+                
+                pluginContext = new HashMap();
+                
+                pluginContainer.getContext().put( ContextEnabled.PLUGIN_CONTEXT_SESSION_KEY, pluginContext );
+            }
+            
+            ( (ContextEnabled) plugin ).setPluginContext( pluginContext );
+        }
+        
         plugin.setLog( mojoLogger );
 
         XmlPlexusConfiguration pomConfiguration;

Modified: maven/components/trunk/maven-plugin-api/src/main/java/org/apache/maven/plugin/AbstractMojo.java
URL: http://svn.apache.org/viewcvs/maven/components/trunk/maven-plugin-api/src/main/java/org/apache/maven/plugin/AbstractMojo.java?rev=292023&r1=292022&r2=292023&view=diff
==============================================================================
--- maven/components/trunk/maven-plugin-api/src/main/java/org/apache/maven/plugin/AbstractMojo.java (original)
+++ maven/components/trunk/maven-plugin-api/src/main/java/org/apache/maven/plugin/AbstractMojo.java Tue Sep 27 12:49:37 2005
@@ -19,13 +19,16 @@
 import org.apache.maven.plugin.logging.Log;
 import org.apache.maven.plugin.logging.SystemStreamLog;
 
+import java.util.Map;
+
 /**
  * @version $Id$
  */
 public abstract class AbstractMojo
-    implements Mojo
+    implements Mojo, ContextEnabled
 {
     private Log log;
+    private Map pluginContext;
 
     public void setLog( Log log )
     {
@@ -41,4 +44,15 @@
 
         return log;
     }
+
+    public Map getPluginContext()
+    {
+        return pluginContext;
+    }
+
+    public void setPluginContext( Map pluginContext )
+    {
+        this.pluginContext = pluginContext;
+    }
+    
 }

Added: maven/components/trunk/maven-plugin-api/src/main/java/org/apache/maven/plugin/ContextEnabled.java
URL: http://svn.apache.org/viewcvs/maven/components/trunk/maven-plugin-api/src/main/java/org/apache/maven/plugin/ContextEnabled.java?rev=292023&view=auto
==============================================================================
--- maven/components/trunk/maven-plugin-api/src/main/java/org/apache/maven/plugin/ContextEnabled.java (added)
+++ maven/components/trunk/maven-plugin-api/src/main/java/org/apache/maven/plugin/ContextEnabled.java Tue Sep 27 12:49:37 2005
@@ -0,0 +1,14 @@
+package org.apache.maven.plugin;
+
+import java.util.Map;
+
+public interface ContextEnabled
+{
+    
+    String PLUGIN_CONTEXT_SESSION_KEY = "mavenPluginContext";
+    
+    void setPluginContext( Map pluginContext );
+    
+    Map getPluginContext();
+
+}

Propchange: maven/components/trunk/maven-plugin-api/src/main/java/org/apache/maven/plugin/ContextEnabled.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/components/trunk/maven-plugin-api/src/main/java/org/apache/maven/plugin/ContextEnabled.java
------------------------------------------------------------------------------
    svn:keywords = "Author Date Id Revision"



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
For additional commands, e-mail: dev-help@maven.apache.org


Re: issues with plugin context impl was: svn commit: r292023

Posted by Brett Porter <br...@apache.org>.
jdcasey@apache.org wrote:

>Modified: maven/components/trunk/maven-core-it-plugin/pom.xml
>URL: http://svn.apache.org/viewcvs/maven/components/trunk/maven-core-it-plugin/pom.xml?rev=292023&r1=292022&r2=292023&view=diff
>==============================================================================
>--- maven/components/trunk/maven-core-it-plugin/pom.xml (original)
>+++ maven/components/trunk/maven-core-it-plugin/pom.xml Tue Sep 27 12:49:37 2005
>@@ -3,7 +3,7 @@
>   <parent>
>     <artifactId>maven-plugin-parent</artifactId>
>     <groupId>org.apache.maven.plugins</groupId>
>-    <version>2.0-beta-1</version>
>+    <version>2.0-beta-2</version>
>   </parent>
>   <modelVersion>4.0.0</modelVersion>
>   <artifactId>maven-core-it-plugin</artifactId>
>@@ -12,6 +12,11 @@
>   <version>1.0-SNAPSHOT</version>
>   <inceptionYear>2001</inceptionYear>
>   <dependencies>
>+    <dependency>
>+      <groupId>org.apache.maven</groupId>
>+      <artifactId>maven-plugin-api</artifactId>
>+      <version>2.0-beta-3-SNAPSHOT</version>
>+    </dependency>
>     <dependency>
>       <groupId>org.apache.maven</groupId>
>       <artifactId>maven-project</artifactId>
>  
>
You should just need to use the beta-3-SNAPSHOT parent here. If it were
any other plugin, it should have a prerequisite too.

> 
>+        if ( plugin instanceof ContextEnabled )
>+        {
>+            Map pluginContext;
>+            try
>+            {
>+                pluginContext = (Map) pluginContainer.getContext().get( ContextEnabled.PLUGIN_CONTEXT_SESSION_KEY );
>+            }
>+            catch ( ContextException e )
>+            {
>+                // this is thrown the first time for each plugin, since the map hasn't been initialized in the
>+                // new plugin's container context.
>+                getLogger().debug( "Initializing plugin context map for plugin: " + pluginDescriptor.getPluginLookupKey() );
>+                
>+                pluginContext = new HashMap();
>+                
>+                pluginContainer.getContext().put( ContextEnabled.PLUGIN_CONTEXT_SESSION_KEY, pluginContext );
>+            }
>+            
>+            ( (ContextEnabled) plugin ).setPluginContext( pluginContext );
>+        }
>+        
>  
>
This doesn't seem lifecycle-aware. Shouldn't the context be on the project?

- Brett

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
For additional commands, e-mail: dev-help@maven.apache.org