[MNG-3631] Mojo Configuration. Allow embedder to get executions by goal, query config with xpath and execute mojo.

git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@744145 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Britton Isbell 2009-02-13 15:39:54 +00:00
parent 90ee1deab0
commit 0abb04c8bb
7 changed files with 124 additions and 71 deletions

View File

@ -117,6 +117,10 @@
<groupId>org.sonatype.plexus</groupId>
<artifactId>plexus-sec-dispatcher</artifactId>
</dependency>
<dependency>
<groupId>commons-jxpath</groupId>
<artifactId>commons-jxpath</artifactId>
</dependency>
</dependencies>
<build>
<plugins>

View File

@ -0,0 +1,55 @@
package org.apache.maven.plugin;
import org.codehaus.plexus.component.annotations.Requirement;
import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
import org.codehaus.plexus.util.xml.Xpp3Dom;
import org.apache.maven.plugin.descriptor.PluginDescriptor;
import org.apache.maven.plugin.descriptor.MojoDescriptor;
import org.apache.maven.execution.MavenSession;
import org.apache.commons.jxpath.JXPathContext;
import java.util.List;
import java.util.Collection;
import java.util.ArrayList;
import java.io.StringReader;
public class DefaultPluginContext implements PluginContext {
@Requirement
protected MavenPluginCollector pluginCollector;
@Requirement
protected PluginManager pluginManager;
public Collection<MojoExecution> getMojoExecutionsForGoal(String goal) throws Exception
{
List<MojoExecution> mojoExecutions = new ArrayList<MojoExecution>();
for(PluginDescriptor descriptor : pluginCollector.getPluginDescriptors())
{
MojoDescriptor mojoDescriptor = descriptor.getMojo(goal);
if(mojoDescriptor != null)
{
MojoExecution mojoExecution = new MojoExecution( mojoDescriptor );
mojoExecution.setConfiguration(
Xpp3DomBuilder.build( new StringReader( mojoDescriptor.getMojoConfiguration().toString() ) ) );
mojoExecutions.add(mojoExecution);
}
}
return mojoExecutions;
}
public Object getMojoParameterFor(MojoExecution mojoExecution, String xPath) throws Exception {
Xpp3Dom mojoDescriptorConfiguration =
Xpp3DomBuilder.build( new StringReader(mojoExecution.getMojoDescriptor().getMojoConfiguration().toString()));
Xpp3Dom mergedConfig = Xpp3Dom.mergeXpp3Dom( mojoExecution.getConfiguration(), mojoDescriptorConfiguration );
return JXPathContext.newContext( mergedConfig ).getValue( xPath );
}
public void executeMojo( MojoExecution mojoExecution, MavenSession session ) throws Exception
{
pluginManager.executeMojo(session.getCurrentProject(), mojoExecution, session);
}
}

View File

@ -847,13 +847,13 @@ public class DefaultPluginManager
getLogger(),
session.getExecutionProperties() );
List<InterpolatorProperty> interpolatorProperties = new ArrayList<InterpolatorProperty>();
List<InterpolatorProperty> interpolatorProperties = new ArrayList<InterpolatorProperty>();
interpolatorProperties.addAll(InterpolatorProperty.toInterpolatorProperties(session.getProjectBuilderConfiguration().getExecutionProperties(),
PomInterpolatorTag.EXECUTION_PROPERTIES.name()));
interpolatorProperties.addAll(InterpolatorProperty.toInterpolatorProperties(session.getProjectBuilderConfiguration().getExecutionProperties(),
PomInterpolatorTag.EXECUTION_PROPERTIES.name()));
interpolatorProperties.addAll(InterpolatorProperty.toInterpolatorProperties(session.getProjectBuilderConfiguration().getUserProperties(),
PomInterpolatorTag.USER_PROPERTIES.name()));
interpolatorProperties.addAll(InterpolatorProperty.toInterpolatorProperties(session.getProjectBuilderConfiguration().getUserProperties(),
PomInterpolatorTag.USER_PROPERTIES.name()));
Plugin plugin = null;
try {
@ -878,7 +878,7 @@ public class DefaultPluginManager
checkRequiredParameters( mojoDescriptor, mojoConfiguration, expressionEvaluator );
populatePluginFields( mojo, mojoDescriptor, mojoConfiguration, expressionEvaluator );
return mojo;
} finally {
@ -1005,29 +1005,6 @@ public class DefaultPluginManager
}
}
public static PlexusConfiguration copyConfiguration( PlexusConfiguration src )
{
// TODO: shouldn't be necessary
XmlPlexusConfiguration dom = new XmlPlexusConfiguration( src.getName() );
dom.setValue( src.getValue( null ) );
String[] attributeNames = src.getAttributeNames();
for ( int i = 0; i < attributeNames.length; i++ )
{
String attributeName = attributeNames[i];
dom.setAttribute( attributeName, src.getAttribute( attributeName, null ) );
}
PlexusConfiguration[] children = src.getChildren();
for ( int i = 0; i < children.length; i++ )
{
dom.addChild( copyConfiguration( children[i] ) );
}
return dom;
}
// ----------------------------------------------------------------------
// Mojo Parameter Handling
// ----------------------------------------------------------------------
@ -1063,7 +1040,7 @@ public class DefaultPluginManager
getLogger().debug( "Configuring mojo '" + mojoDescriptor.getId() + "' with "
+ ( configuratorId == null ? "basic" : configuratorId )
+ " configurator -->" );
// This needs to be able to use methods
configurator.configureComponent( plugin, configuration, expressionEvaluator, realm, listener );

View File

@ -2,14 +2,30 @@ package org.apache.maven.plugin;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.component.annotations.Requirement;
import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
import org.codehaus.plexus.component.configurator.ComponentConfigurator;
import org.codehaus.plexus.component.configurator.ConfigurationListener;
import org.codehaus.plexus.component.configurator.ComponentConfigurationException;
import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
import org.codehaus.plexus.component.repository.exception.ComponentLifecycleException;
import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
import org.codehaus.plexus.util.xml.Xpp3Dom;
import org.codehaus.plexus.util.StringUtils;
import org.codehaus.plexus.configuration.PlexusConfiguration;
import org.codehaus.plexus.configuration.xml.XmlPlexusConfiguration;
import org.codehaus.plexus.classworlds.realm.ClassRealm;
import org.apache.maven.model.Plugin;
import org.apache.maven.plugin.descriptor.PluginDescriptor;
import org.apache.maven.plugin.descriptor.MojoDescriptor;
import org.apache.maven.plugin.descriptor.Parameter;
import org.apache.maven.shared.model.InterpolatorProperty;
import org.apache.maven.project.builder.PomInterpolatorTag;
import org.apache.maven.project.builder.Mixer;
import org.apache.maven.execution.MavenSession;
import java.util.List;
import java.util.ArrayList;
import java.io.StringReader;
@Component( role = PluginRepository.class)

View File

@ -29,18 +29,14 @@ import org.codehaus.plexus.logging.LogEnabled;
import org.codehaus.plexus.logging.Logger;
import org.codehaus.plexus.logging.console.ConsoleLogger;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.*;
public class MavenPluginCollector
implements ComponentDiscoveryListener, LogEnabled
{
private Set pluginsInProcess = new HashSet();
private Map pluginDescriptors = new HashMap();
private Map<String, PluginDescriptor> pluginDescriptors = new HashMap();
private Map pluginIdsByPrefix = new HashMap();
@ -75,8 +71,12 @@ public class MavenPluginCollector
public PluginDescriptor getPluginDescriptor( Plugin plugin )
{
String key = constructPluginKey( plugin );
return (PluginDescriptor) pluginDescriptors.get( key );
return pluginDescriptors.get( constructPluginKey( plugin ) );
}
public Collection<PluginDescriptor> getPluginDescriptors()
{
return pluginDescriptors.values();
}
private String constructPluginKey( Plugin plugin )
@ -97,7 +97,7 @@ public class MavenPluginCollector
return pluginDescriptors.containsKey( key );
}
public Set getPluginDescriptorsForPrefix( String prefix )
public Set<PluginDescriptor> getPluginDescriptorsForPrefix( String prefix )
{
Set result = new HashSet();
for ( Iterator it = pluginDescriptors.values().iterator(); it.hasNext(); )

View File

@ -0,0 +1,15 @@
package org.apache.maven.plugin;
import org.apache.maven.execution.MavenSession;
import java.util.Collection;
public interface PluginContext {
Collection<MojoExecution> getMojoExecutionsForGoal(String goal) throws Exception;
Object getMojoParameterFor(MojoExecution mojoExecution, String xPath) throws Exception;
void executeMojo(MojoExecution mojoExecution, MavenSession session) throws Exception;
}

View File

@ -24,10 +24,7 @@ import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.net.URL;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.*;
import org.apache.maven.Maven;
import org.apache.maven.artifact.Artifact;
@ -90,8 +87,6 @@ import org.codehaus.plexus.classworlds.realm.NoSuchRealmException;
import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
import org.codehaus.plexus.component.repository.exception.ComponentRepositoryException;
import org.codehaus.plexus.configuration.PlexusConfigurationException;
import org.codehaus.plexus.configuration.PlexusConfiguration;
import org.codehaus.plexus.configuration.xml.XmlPlexusConfiguration;
import org.codehaus.plexus.logging.LoggerManager;
import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.ReaderFactory;
@ -170,6 +165,8 @@ public class MavenEmbedder
private Configuration configuration;
private PluginContext pluginContext;
// ----------------------------------------------------------------------------
// Constructors
// ----------------------------------------------------------------------------
@ -185,6 +182,21 @@ public class MavenEmbedder
return request;
}
public Collection<MojoExecution> getMojoExecutionsForGoal(String goal) throws Exception
{
return pluginContext.getMojoExecutionsForGoal( goal );
}
public Object getMojoParameterFor(MojoExecution mojoExecution, String xPath) throws Exception
{
return pluginContext.getMojoParameterFor( mojoExecution, xPath);
}
public void executeMojo(MojoExecution mojoExecution, MavenSession mavenSession ) throws Exception
{
pluginContext.executeMojo( mojoExecution, mavenSession );
}
// ----------------------------------------------------------------------
// Accessors
// ----------------------------------------------------------------------
@ -250,32 +262,6 @@ public class MavenEmbedder
modelWriter.write( writer, model );
}
public PlexusConfiguration getPluginConfiguration(String pluginId, String mojoId, Model model) throws Exception
{
try {
return mixer.mixPluginAndReturnConfig(pluginRepository.findPluginById(pluginId, mojoId), null, model, null);
} catch (PlexusConfigurationException e) {
throw new IOException(e.getMessage());
}
}
public Object getPluginConfigurationAsDom(String pluginId, String mojoId, Model model) throws Exception
{
try {
return mixer.mixPluginAndReturnConfigAsDom(pluginRepository.findPluginById(pluginId, mojoId), model);
} catch (PlexusConfigurationException e) {
throw new IOException(e.getMessage());
}
}
public Object getPluginConfigurationAsDom(String pluginId, String mojoId, Model model, String xpathExpression) throws Exception
{
try {
return mixer.mixPluginAndReturnConfigAsDom(pluginRepository.findPluginById(pluginId, mojoId), model, xpathExpression);
} catch (PlexusConfigurationException e) {
throw new IOException(e.getMessage());
}
}
// ----------------------------------------------------------------------
// Settings