KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hudson > maven > MojoInfo


1 package hudson.maven;
2
3 import org.apache.maven.plugin.MojoExecution;
4 import org.codehaus.plexus.configuration.PlexusConfiguration;
5 import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
6 import org.codehaus.plexus.component.configurator.converters.lookup.ConverterLookup;
7 import org.codehaus.plexus.component.configurator.converters.lookup.DefaultConverterLookup;
8 import org.codehaus.plexus.component.configurator.converters.ConfigurationConverter;
9 import org.codehaus.plexus.component.configurator.ComponentConfigurationException;
10
11 import java.io.File JavaDoc;
12
13 /**
14  * Information about Mojo to be executed. This object provides
15  * convenient access to various mojo information, so that {@link MavenReporter}
16  * implementations are shielded to some extent from Maven internals.
17  *
18  * <p>
19  * For each mojo to be executed, this object is created and passed to
20  * {@link MavenReporter}.
21  *
22  * @author Kohsuke Kawaguchi
23  * @see MavenReporter
24  */

25 public final class MojoInfo {
26     /**
27      * Object from Maven that describes the Mojo to be executed.
28      */

29     public final MojoExecution mojoExecution;
30
31     /**
32      * PluginName of the plugin that contains this mojo.
33      */

34     public final PluginName pluginName;
35
36     /**
37      * Configuration of the mojo for the current execution.
38      * This reflects the default values, as well as values configured from POM,
39      * including inherited values.
40      */

41     public final PlexusConfiguration configuration;
42
43     /**
44      * Object that Maven uses to resolve variables like "${project}" to its
45      * corresponding object.
46      */

47     public final ExpressionEvaluator expressionEvaluator;
48
49     /**
50      * Used to obtain a value from {@link PlexusConfiguration} as a typed object,
51      * instead of String.
52      */

53     private final ConverterLookup converterLookup = new DefaultConverterLookup();
54
55     public MojoInfo(MojoExecution mojoExecution, PlexusConfiguration configuration, ExpressionEvaluator expressionEvaluator) {
56         this.mojoExecution = mojoExecution;
57         this.configuration = configuration;
58         this.expressionEvaluator = expressionEvaluator;
59         this.pluginName = new PluginName(mojoExecution.getMojoDescriptor().getPluginDescriptor());
60     }
61
62     /**
63      * Gets the goal name of the mojo to be executed,
64      * such as "javadoc". This is local to the plugin name.
65       */

66     public String JavaDoc getGoal() {
67         return mojoExecution.getMojoDescriptor().getGoal();
68     }
69
70     /**
71      * Obtains the configuration value of the mojo.
72      *
73      * @param configName
74      * The name of the child element in the &lt;configuration> of mojo.
75      * @param type
76      * The Java class of the configuration value. While every element
77      * can be read as {@link String}, often different types have a different
78      * conversion rules associated with it (for example, {@link File} would
79      * resolve relative path against POM base directory.)
80      *
81      * @return
82      * The configuration value either specified in POM, or inherited from
83      * parent POM, or default value if one is specified in mojo.
84      *
85      * @throws ComponentConfigurationException
86      * Not sure when exactly this is thrown, but it's probably when
87      * the configuration in POM is syntactically incorrect.
88      */

89     public <T> T getConfigurationValue(String JavaDoc configName, Class JavaDoc<T> type) throws ComponentConfigurationException {
90         PlexusConfiguration child = configuration.getChild(configName);
91         if(child==null) return null; // no such config
92

93         ConfigurationConverter converter = converterLookup.lookupConverterForType(type);
94         return type.cast(converter.fromConfiguration(converterLookup,child,type,
95             // the implementation seems to expect the type of the bean for which the configuration is done
96
// in this parameter, but we have no such type. So passing in a dummy
97
Object JavaDoc.class,
98             mojoExecution.getMojoDescriptor().getPluginDescriptor().getClassRealm().getClassLoader(),
99             expressionEvaluator));
100     }
101 }
102
Popular Tags