From 30d2a16b5a4c33537103e61295be06abc3808819 Mon Sep 17 00:00:00 2001 From: John Dennis Casey Date: Tue, 27 Sep 2005 19:49:37 +0000 Subject: [PATCH] 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 --- maven-core-it-plugin/pom.xml | 7 +- .../apache/maven/plugin/coreit/CatchMojo.java | 78 +++++++++++++++++++ .../apache/maven/plugin/coreit/ThrowMojo.java | 39 ++++++++++ maven-core-it/it0073/expected-results.txt | 1 + maven-core-it/it0073/goals.txt | 1 + maven-core-it/it0073/pom.xml | 29 +++++++ maven-core-it/it0073/prebuild-hook.txt | 1 + .../maven/plugin/DefaultPluginManager.java | 23 +++++- .../org/apache/maven/plugin/AbstractMojo.java | 16 +++- .../apache/maven/plugin/ContextEnabled.java | 14 ++++ 10 files changed, 206 insertions(+), 3 deletions(-) create mode 100644 maven-core-it-plugin/src/main/java/org/apache/maven/plugin/coreit/CatchMojo.java create mode 100644 maven-core-it-plugin/src/main/java/org/apache/maven/plugin/coreit/ThrowMojo.java create mode 100644 maven-core-it/it0073/expected-results.txt create mode 100644 maven-core-it/it0073/goals.txt create mode 100644 maven-core-it/it0073/pom.xml create mode 100644 maven-core-it/it0073/prebuild-hook.txt create mode 100644 maven-plugin-api/src/main/java/org/apache/maven/plugin/ContextEnabled.java diff --git a/maven-core-it-plugin/pom.xml b/maven-core-it-plugin/pom.xml index 28e07a88a8..d5e484f801 100644 --- a/maven-core-it-plugin/pom.xml +++ b/maven-core-it-plugin/pom.xml @@ -3,7 +3,7 @@ maven-plugin-parent org.apache.maven.plugins - 2.0-beta-1 + 2.0-beta-2 4.0.0 maven-core-it-plugin @@ -12,6 +12,11 @@ 1.0-SNAPSHOT 2001 + + org.apache.maven + maven-plugin-api + 2.0-beta-3-SNAPSHOT + org.apache.maven maven-project diff --git a/maven-core-it-plugin/src/main/java/org/apache/maven/plugin/coreit/CatchMojo.java b/maven-core-it-plugin/src/main/java/org/apache/maven/plugin/coreit/CatchMojo.java new file mode 100644 index 0000000000..01b437f575 --- /dev/null +++ b/maven-core-it-plugin/src/main/java/org/apache/maven/plugin/coreit/CatchMojo.java @@ -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 ) + { + } + } + } + } + +} diff --git a/maven-core-it-plugin/src/main/java/org/apache/maven/plugin/coreit/ThrowMojo.java b/maven-core-it-plugin/src/main/java/org/apache/maven/plugin/coreit/ThrowMojo.java new file mode 100644 index 0000000000..d002a9565d --- /dev/null +++ b/maven-core-it-plugin/src/main/java/org/apache/maven/plugin/coreit/ThrowMojo.java @@ -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 ); + } + +} diff --git a/maven-core-it/it0073/expected-results.txt b/maven-core-it/it0073/expected-results.txt new file mode 100644 index 0000000000..9a0a986434 --- /dev/null +++ b/maven-core-it/it0073/expected-results.txt @@ -0,0 +1 @@ +target/thrown-value diff --git a/maven-core-it/it0073/goals.txt b/maven-core-it/it0073/goals.txt new file mode 100644 index 0000000000..1be4aba8c7 --- /dev/null +++ b/maven-core-it/it0073/goals.txt @@ -0,0 +1 @@ +core-it:throw core-it:catch diff --git a/maven-core-it/it0073/pom.xml b/maven-core-it/it0073/pom.xml new file mode 100644 index 0000000000..42ebece49c --- /dev/null +++ b/maven-core-it/it0073/pom.xml @@ -0,0 +1,29 @@ + + 4.0.0 + org.apache.maven.it + maven-core-it0073 + 1.0-SNAPSHOT + + + + snapshots + http://snapshots.maven.codehaus.org/maven2 + + false + + + + + + + + org.apache.maven.plugins + maven-core-it-plugin + + + thrown-value + + + + + diff --git a/maven-core-it/it0073/prebuild-hook.txt b/maven-core-it/it0073/prebuild-hook.txt new file mode 100644 index 0000000000..3ce9c85a20 --- /dev/null +++ b/maven-core-it/it0073/prebuild-hook.txt @@ -0,0 +1 @@ +rm ${artifact:org.apache.maven.plugins:maven-core-it-plugin:1.0-SNAPSHOT:maven-plugin} diff --git a/maven-core/src/main/java/org/apache/maven/plugin/DefaultPluginManager.java b/maven-core/src/main/java/org/apache/maven/plugin/DefaultPluginManager.java index 15b6f8d83f..f42eb3f9df 100644 --- a/maven-core/src/main/java/org/apache/maven/plugin/DefaultPluginManager.java +++ b/maven-core/src/main/java/org/apache/maven/plugin/DefaultPluginManager.java @@ -503,7 +503,7 @@ public class DefaultPluginManager 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 @@ 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; diff --git a/maven-plugin-api/src/main/java/org/apache/maven/plugin/AbstractMojo.java b/maven-plugin-api/src/main/java/org/apache/maven/plugin/AbstractMojo.java index 19f97cdc67..a270efac8a 100644 --- a/maven-plugin-api/src/main/java/org/apache/maven/plugin/AbstractMojo.java +++ b/maven-plugin-api/src/main/java/org/apache/maven/plugin/AbstractMojo.java @@ -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; + } + } diff --git a/maven-plugin-api/src/main/java/org/apache/maven/plugin/ContextEnabled.java b/maven-plugin-api/src/main/java/org/apache/maven/plugin/ContextEnabled.java new file mode 100644 index 0000000000..3225f17c1d --- /dev/null +++ b/maven-plugin-api/src/main/java/org/apache/maven/plugin/ContextEnabled.java @@ -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(); + +}