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.

git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@292023 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
John Dennis Casey 2005-09-27 19:49:37 +00:00
parent 063f67e9a1
commit 30d2a16b5a
10 changed files with 206 additions and 3 deletions

View File

@ -3,7 +3,7 @@
<parent> <parent>
<artifactId>maven-plugin-parent</artifactId> <artifactId>maven-plugin-parent</artifactId>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<version>2.0-beta-1</version> <version>2.0-beta-2</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<artifactId>maven-core-it-plugin</artifactId> <artifactId>maven-core-it-plugin</artifactId>
@ -12,6 +12,11 @@
<version>1.0-SNAPSHOT</version> <version>1.0-SNAPSHOT</version>
<inceptionYear>2001</inceptionYear> <inceptionYear>2001</inceptionYear>
<dependencies> <dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>2.0-beta-3-SNAPSHOT</version>
</dependency>
<dependency> <dependency>
<groupId>org.apache.maven</groupId> <groupId>org.apache.maven</groupId>
<artifactId>maven-project</artifactId> <artifactId>maven-project</artifactId>

View File

@ -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 )
{
}
}
}
}
}

View File

@ -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 );
}
}

View File

@ -0,0 +1 @@
target/thrown-value

View File

@ -0,0 +1 @@
core-it:throw core-it:catch

View File

@ -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>

View File

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

View File

@ -503,7 +503,7 @@ public class DefaultPluginManager
PluginDescriptor pluginDescriptor = mojoDescriptor.getPluginDescriptor(); PluginDescriptor pluginDescriptor = mojoDescriptor.getPluginDescriptor();
PlexusContainer pluginContainer = getPluginContainer( pluginDescriptor ); PlexusContainer pluginContainer = getPluginContainer( pluginDescriptor );
// if this is the first time this plugin has been used, the plugin's container will only // 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 // contain the plugin's artifact in isolation; we need to finish resolving the plugin's
// dependencies, and add them to the container. // dependencies, and add them to the container.
@ -516,6 +516,27 @@ public class DefaultPluginManager
return null; 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 ); plugin.setLog( mojoLogger );
XmlPlexusConfiguration pomConfiguration; XmlPlexusConfiguration pomConfiguration;

View File

@ -19,13 +19,16 @@ package org.apache.maven.plugin;
import org.apache.maven.plugin.logging.Log; import org.apache.maven.plugin.logging.Log;
import org.apache.maven.plugin.logging.SystemStreamLog; import org.apache.maven.plugin.logging.SystemStreamLog;
import java.util.Map;
/** /**
* @version $Id$ * @version $Id$
*/ */
public abstract class AbstractMojo public abstract class AbstractMojo
implements Mojo implements Mojo, ContextEnabled
{ {
private Log log; private Log log;
private Map pluginContext;
public void setLog( Log log ) public void setLog( Log log )
{ {
@ -41,4 +44,15 @@ public abstract class AbstractMojo
return log; return log;
} }
public Map getPluginContext()
{
return pluginContext;
}
public void setPluginContext( Map pluginContext )
{
this.pluginContext = pluginContext;
}
} }

View File

@ -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();
}