mirror of https://github.com/apache/maven.git
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:
parent
063f67e9a1
commit
30d2a16b5a
|
@ -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>
|
||||
|
|
|
@ -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 )
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -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 );
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
target/thrown-value
|
|
@ -0,0 +1 @@
|
|||
core-it:throw core-it:catch
|
|
@ -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>
|
|
@ -0,0 +1 @@
|
|||
rm ${artifact:org.apache.maven.plugins:maven-core-it-plugin:1.0-SNAPSHOT:maven-plugin}
|
|
@ -516,6 +516,27 @@ public class DefaultPluginManager
|
|||
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;
|
||||
|
|
|
@ -19,13 +19,16 @@ package org.apache.maven.plugin;
|
|||
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 @@ public abstract class AbstractMojo
|
|||
|
||||
return log;
|
||||
}
|
||||
|
||||
public Map getPluginContext()
|
||||
{
|
||||
return pluginContext;
|
||||
}
|
||||
|
||||
public void setPluginContext( Map pluginContext )
|
||||
{
|
||||
this.pluginContext = pluginContext;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
|
||||
}
|
Loading…
Reference in New Issue