mirror of https://github.com/apache/maven.git
o Added marmalade scripted mojo discoverer and tests.
o Minimal modifications to the MavenPluginDescriptor to allow building of instances without privileged field access. o Still need to add a pom.xml for the marmalade-plugin-loader (or whatever we should call it). git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@162912 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
4f12e1cfa6
commit
859b2cac34
|
@ -0,0 +1,361 @@
|
|||
/* Created on Aug 7, 2004 */
|
||||
package org.apache.maven.plugin.loader.marmalade;
|
||||
|
||||
import org.apache.maven.plugin.MavenMojoDescriptor;
|
||||
import org.apache.maven.plugin.MavenPluginDependency;
|
||||
import org.apache.maven.plugin.MavenPluginDescriptor;
|
||||
import org.apache.maven.plugin.descriptor.Dependency;
|
||||
import org.apache.maven.plugin.descriptor.MojoDescriptor;
|
||||
import org.apache.maven.plugin.descriptor.PluginDescriptor;
|
||||
import org.apache.maven.plugin.loader.marmalade.tags.MojoTag;
|
||||
import org.codehaus.classworlds.ClassRealm;
|
||||
import org.codehaus.marmalade.el.ognl.OgnlExpressionEvaluator;
|
||||
import org.codehaus.marmalade.metamodel.MarmaladeTaglibResolver;
|
||||
import org.codehaus.marmalade.model.MarmaladeScript;
|
||||
import org.codehaus.marmalade.model.MarmaladeTag;
|
||||
import org.codehaus.marmalade.parsetime.CachingScriptParser;
|
||||
import org.codehaus.marmalade.parsetime.DefaultParsingContext;
|
||||
import org.codehaus.marmalade.parsetime.MarmaladeModelBuilderException;
|
||||
import org.codehaus.marmalade.parsetime.MarmaladeParsetimeException;
|
||||
import org.codehaus.marmalade.parsetime.MarmaladeParsingContext;
|
||||
import org.codehaus.marmalade.parsetime.ScriptBuilder;
|
||||
import org.codehaus.marmalade.parsetime.ScriptParser;
|
||||
import org.codehaus.marmalade.runtime.DefaultContext;
|
||||
import org.codehaus.marmalade.runtime.MarmaladeExecutionContext;
|
||||
import org.codehaus.marmalade.runtime.MarmaladeExecutionException;
|
||||
import org.codehaus.marmalade.util.RecordingReader;
|
||||
import org.codehaus.plexus.component.discovery.AbstractComponentDiscoverer;
|
||||
import org.codehaus.plexus.component.factory.ComponentInstantiationException;
|
||||
import org.codehaus.plexus.component.repository.ComponentDescriptor;
|
||||
import org.codehaus.plexus.component.repository.ComponentSetDescriptor;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.Reader;
|
||||
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.jar.JarEntry;
|
||||
import java.util.jar.JarInputStream;
|
||||
|
||||
/**
|
||||
* @author jdcasey
|
||||
*/
|
||||
public class MarmaladePluginDiscoverer extends AbstractComponentDiscoverer
|
||||
{
|
||||
private static final String FILE_PROTO = "file";
|
||||
private static final String MARMALADE_MOJO_PATH_PATTERN = ".*\\.mmld";
|
||||
private static final String MARMALADE_COMPONENT_FACTORY = "marmalade";
|
||||
|
||||
protected String getComponentDescriptorLocation( )
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
protected ComponentSetDescriptor createComponentDescriptors(
|
||||
Reader reader, String source )
|
||||
throws Exception
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public List findComponents( ClassRealm classRealm )
|
||||
{
|
||||
URL[] candidateLocations = classRealm.getConstituents( );
|
||||
|
||||
List componentSetDescriptors = new LinkedList( );
|
||||
|
||||
for ( int i = 0; i < candidateLocations.length; i++ )
|
||||
{
|
||||
URL url = candidateLocations[i];
|
||||
|
||||
String urlProto = url.getProtocol( );
|
||||
|
||||
try
|
||||
{
|
||||
PluginDescriptor pluginDescriptor = null;
|
||||
|
||||
if ( FILE_PROTO.equals( urlProto ) )
|
||||
{
|
||||
// scan as directory...
|
||||
pluginDescriptor = scanAsDirectory( url.getPath( ),
|
||||
classRealm );
|
||||
}
|
||||
else
|
||||
{
|
||||
// Skip this one...not sure what to do with it.
|
||||
}
|
||||
|
||||
if ( pluginDescriptor != null )
|
||||
{
|
||||
MavenPluginDescriptor descriptor = new MavenPluginDescriptor( pluginDescriptor );
|
||||
|
||||
// Add the dependencies
|
||||
List dependencies = new ArrayList();
|
||||
|
||||
for ( Iterator it = pluginDescriptor.getDependencies().iterator(); it.hasNext() ; )
|
||||
{
|
||||
Dependency dependency = (Dependency)it.next();
|
||||
dependencies.add( new MavenPluginDependency( dependency ) );
|
||||
}
|
||||
|
||||
descriptor.setDependencies( dependencies );
|
||||
|
||||
List mojoDescriptors = pluginDescriptor.getMojos( );
|
||||
List componentDescriptors = new LinkedList( );
|
||||
|
||||
for ( Iterator it = mojoDescriptors.iterator( );
|
||||
it.hasNext( ); )
|
||||
{
|
||||
MojoDescriptor mojoDescriptor = ( MojoDescriptor ) it
|
||||
.next( );
|
||||
|
||||
mojoDescriptor.setImplementation(url.getPath());
|
||||
|
||||
ComponentDescriptor mmDesc = new MavenMojoDescriptor( mojoDescriptor );
|
||||
|
||||
mmDesc.setComponentFactory( MARMALADE_COMPONENT_FACTORY );
|
||||
|
||||
componentDescriptors.add( mmDesc );
|
||||
}
|
||||
|
||||
descriptor.setComponents( componentDescriptors );
|
||||
|
||||
componentSetDescriptors.add( descriptor );
|
||||
}
|
||||
}
|
||||
catch ( Exception e )
|
||||
{
|
||||
// TODO Log and what...skip???
|
||||
e.printStackTrace( );
|
||||
}
|
||||
}
|
||||
|
||||
return componentSetDescriptors;
|
||||
}
|
||||
|
||||
private PluginDescriptor scanAsJar( String path, ClassRealm classRealm )
|
||||
throws Exception
|
||||
{
|
||||
String jarPart = path;
|
||||
|
||||
int bangIdx = jarPart.indexOf( "!" );
|
||||
|
||||
if ( bangIdx > -1 )
|
||||
{
|
||||
jarPart = jarPart.substring( 0, bangIdx );
|
||||
}
|
||||
|
||||
File file = new File(path);
|
||||
JarInputStream jis = new JarInputStream( new FileInputStream( file ) );
|
||||
|
||||
List mojoDescriptors = new LinkedList( );
|
||||
List dependencies = new LinkedList( );
|
||||
|
||||
JarEntry je = null;
|
||||
|
||||
while ( ( je = jis.getNextJarEntry( ) ) != null )
|
||||
{
|
||||
String entryName = je.getName( );
|
||||
|
||||
if ( entryName.matches( MARMALADE_MOJO_PATH_PATTERN ) )
|
||||
{
|
||||
mojoDescriptors.add( loadMojoDescriptor( entryName,
|
||||
dependencies, classRealm ) );
|
||||
}
|
||||
}
|
||||
|
||||
PluginDescriptor descriptor = null;
|
||||
|
||||
if ( !mojoDescriptors.isEmpty( ) )
|
||||
{
|
||||
descriptor = new PluginDescriptor( );
|
||||
descriptor.setMojos( mojoDescriptors );
|
||||
descriptor.setDependencies( dependencies );
|
||||
}
|
||||
|
||||
return descriptor;
|
||||
}
|
||||
|
||||
private PluginDescriptor scanAsDirectory( String path, ClassRealm classRealm )
|
||||
throws Exception
|
||||
{
|
||||
List dependencies = new LinkedList( );
|
||||
|
||||
File file = new File(path);
|
||||
|
||||
PluginDescriptor descriptor = null;
|
||||
|
||||
if(!file.isDirectory() && path.endsWith(".jar")) {
|
||||
// try to scan it as a jar...
|
||||
descriptor = scanAsJar(path, classRealm);
|
||||
}
|
||||
else {
|
||||
List mojoDescriptors = scanDir( path, file,
|
||||
new LinkedList( ), dependencies, classRealm );
|
||||
|
||||
if ( !mojoDescriptors.isEmpty( ) )
|
||||
{
|
||||
descriptor = new PluginDescriptor( );
|
||||
descriptor.setMojos( mojoDescriptors );
|
||||
descriptor.setDependencies( dependencies );
|
||||
}
|
||||
}
|
||||
|
||||
return descriptor;
|
||||
}
|
||||
|
||||
private List scanDir( String basePath, File parent, List results,
|
||||
List dependencies, ClassRealm classRealm )
|
||||
throws Exception
|
||||
{
|
||||
if ( parent.isDirectory( ) )
|
||||
{
|
||||
String[] subPaths = parent.list( );
|
||||
|
||||
for ( int i = 0; i < subPaths.length; i++ )
|
||||
{
|
||||
File file = new File( parent, subPaths[i] );
|
||||
|
||||
if ( file.isDirectory( ) )
|
||||
{
|
||||
results = scanDir( basePath, file, results, dependencies,
|
||||
classRealm );
|
||||
}
|
||||
else if ( file.getPath( ).matches( MARMALADE_MOJO_PATH_PATTERN ) )
|
||||
{
|
||||
String scriptResource = file.getAbsolutePath( ).substring( basePath
|
||||
.length( ) );
|
||||
|
||||
results.add( loadMojoDescriptor( scriptResource,
|
||||
dependencies, classRealm ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
private MojoDescriptor loadMojoDescriptor( String entryName,
|
||||
List dependencies, ClassRealm classRealm )
|
||||
throws Exception
|
||||
{
|
||||
MarmaladeParsingContext context = buildParsingContext( entryName,
|
||||
classRealm );
|
||||
MarmaladeScript script = getScriptInstance( context );
|
||||
|
||||
MojoDescriptor desc = getDescriptor( script, dependencies );
|
||||
|
||||
return desc;
|
||||
}
|
||||
|
||||
private MojoDescriptor getDescriptor( MarmaladeScript script,
|
||||
List dependencies ) throws ComponentInstantiationException
|
||||
{
|
||||
MojoTag root = ( MojoTag ) script.getRoot( );
|
||||
|
||||
root.describeOnly( true );
|
||||
|
||||
MarmaladeExecutionContext execCtx = new DefaultContext( );
|
||||
|
||||
try
|
||||
{
|
||||
script.execute( execCtx );
|
||||
}
|
||||
catch ( MarmaladeExecutionException e )
|
||||
{
|
||||
throw new ComponentInstantiationException(
|
||||
"failed to execute component script: " + script.getLocation( ),
|
||||
e );
|
||||
}
|
||||
|
||||
MojoDescriptor descriptor = root.getMojoDescriptor( );
|
||||
|
||||
dependencies = root.addDependencies( dependencies );
|
||||
|
||||
return descriptor;
|
||||
}
|
||||
|
||||
private MarmaladeScript getScriptInstance(
|
||||
MarmaladeParsingContext parsingContext )
|
||||
throws Exception
|
||||
{
|
||||
ScriptBuilder builder = null;
|
||||
|
||||
try
|
||||
{
|
||||
builder = new ScriptParser().parse( parsingContext );
|
||||
}
|
||||
catch ( MarmaladeParsetimeException e )
|
||||
{
|
||||
throw new Exception( "failed to parse component script: "
|
||||
+ parsingContext.getInputLocation( ), e );
|
||||
}
|
||||
catch ( MarmaladeModelBuilderException e )
|
||||
{
|
||||
throw new Exception( "failed to parse component script: "
|
||||
+ parsingContext.getInputLocation( ), e );
|
||||
}
|
||||
|
||||
MarmaladeScript script = null;
|
||||
|
||||
try
|
||||
{
|
||||
script = builder.build( );
|
||||
}
|
||||
catch ( MarmaladeModelBuilderException e )
|
||||
{
|
||||
throw new Exception( "failed to build component script: "
|
||||
+ parsingContext.getInputLocation( ), e );
|
||||
}
|
||||
|
||||
MarmaladeTag root = script.getRoot( );
|
||||
|
||||
if ( !( root instanceof MojoTag ) )
|
||||
{
|
||||
throw new Exception(
|
||||
"marmalade script does not contain a mojo header" );
|
||||
}
|
||||
|
||||
return script;
|
||||
}
|
||||
|
||||
private MarmaladeParsingContext buildParsingContext( String scriptName,
|
||||
ClassRealm classRealm )
|
||||
throws Exception
|
||||
{
|
||||
InputStream scriptResource = classRealm.getResourceAsStream( scriptName );
|
||||
|
||||
if ( scriptResource == null )
|
||||
{
|
||||
throw new Exception( "can't get script from classpath: "
|
||||
+ scriptName );
|
||||
}
|
||||
|
||||
RecordingReader scriptIn = new RecordingReader( new InputStreamReader(
|
||||
scriptResource ) );
|
||||
|
||||
MarmaladeTaglibResolver resolver = new MarmaladeTaglibResolver( MarmaladeTaglibResolver.DEFAULT_STRATEGY_CHAIN );
|
||||
|
||||
OgnlExpressionEvaluator el = new OgnlExpressionEvaluator( );
|
||||
|
||||
MarmaladeParsingContext context = new DefaultParsingContext( );
|
||||
|
||||
context.setDefaultExpressionEvaluator( el );
|
||||
context.setInput( scriptIn );
|
||||
context.setInputLocation( scriptName );
|
||||
context.setTaglibResolver( resolver );
|
||||
|
||||
return context;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
/* Created on Aug 7, 2004 */
|
||||
package org.apache.maven.plugin.loader.marmalade.tags;
|
||||
|
||||
/**
|
||||
* @author jdcasey
|
||||
*/
|
||||
public interface DescriptionOwner {
|
||||
|
||||
public void setDescription(String description);
|
||||
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
/* Created on Aug 7, 2004 */
|
||||
package org.apache.maven.plugin.loader.marmalade.tags;
|
||||
|
||||
import org.codehaus.marmalade.el.ExpressionEvaluationException;
|
||||
import org.codehaus.marmalade.model.AbstractMarmaladeTag;
|
||||
import org.codehaus.marmalade.runtime.MarmaladeExecutionContext;
|
||||
import org.codehaus.marmalade.runtime.MarmaladeExecutionException;
|
||||
|
||||
public class DescriptionTag extends AbstractMarmaladeTag
|
||||
{
|
||||
protected void doExecute( MarmaladeExecutionContext context )
|
||||
throws MarmaladeExecutionException
|
||||
{
|
||||
String description = (String)getBody(context, String.class);
|
||||
|
||||
DescriptionOwner parent = (DescriptionOwner)requireParent(DescriptionOwner.class);
|
||||
|
||||
parent.setDescription(description);
|
||||
}
|
||||
|
||||
protected boolean preserveBodyWhitespace( MarmaladeExecutionContext arg0 )
|
||||
throws ExpressionEvaluationException
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
/* Created on Aug 7, 2004 */
|
||||
package org.apache.maven.plugin.loader.marmalade.tags;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.maven.plugin.descriptor.Dependency;
|
||||
import org.codehaus.marmalade.model.AbstractMarmaladeTag;
|
||||
import org.codehaus.marmalade.runtime.MarmaladeExecutionContext;
|
||||
import org.codehaus.marmalade.runtime.MarmaladeExecutionException;
|
||||
|
||||
/**
|
||||
* @author jdcasey
|
||||
*/
|
||||
public class MojoDependenciesTag extends AbstractMarmaladeTag {
|
||||
|
||||
private List dependencies = new LinkedList();
|
||||
|
||||
protected void doExecute(MarmaladeExecutionContext context)
|
||||
throws MarmaladeExecutionException {
|
||||
|
||||
processChildren(context);
|
||||
|
||||
MojoDescriptorTag parent = (MojoDescriptorTag)requireParent(MojoDescriptorTag.class);
|
||||
parent.setDependencies(dependencies);
|
||||
}
|
||||
|
||||
public void addDependency(Dependency dependency) {
|
||||
dependencies.add(dependency);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,43 @@
|
|||
/* Created on Aug 7, 2004 */
|
||||
package org.apache.maven.plugin.loader.marmalade.tags;
|
||||
|
||||
import org.apache.maven.plugin.descriptor.Dependency;
|
||||
import org.codehaus.marmalade.model.AbstractMarmaladeTag;
|
||||
import org.codehaus.marmalade.runtime.MarmaladeExecutionContext;
|
||||
import org.codehaus.marmalade.runtime.MarmaladeExecutionException;
|
||||
|
||||
public class MojoDependencyTag extends AbstractMarmaladeTag
|
||||
{
|
||||
public static final String GROUP_ID_ATTRIBUTE = "groupId";
|
||||
public static final String ARTIFACT_ID_ATTRIBUTE = "artifactId";
|
||||
public static final String VERSION_ATTRIBUTE = "version";
|
||||
public static final String TYPE_ATTRIBUTE = "type";
|
||||
|
||||
protected void doExecute( MarmaladeExecutionContext context )
|
||||
throws MarmaladeExecutionException
|
||||
{
|
||||
String groupId = (String)requireTagAttribute(GROUP_ID_ATTRIBUTE, String.class, context);
|
||||
String artifactId = (String)requireTagAttribute(ARTIFACT_ID_ATTRIBUTE, String.class, context);
|
||||
String version = (String)requireTagAttribute(VERSION_ATTRIBUTE, String.class, context);
|
||||
|
||||
String type = (String)getAttributes().getValue(TYPE_ATTRIBUTE, String.class, context);
|
||||
|
||||
MojoDependenciesTag parent = (MojoDependenciesTag)requireParent(MojoDependenciesTag.class);
|
||||
|
||||
Dependency dep = new Dependency();
|
||||
dep.setGroupId(groupId);
|
||||
dep.setArtifactId(artifactId);
|
||||
dep.setVersion(version);
|
||||
|
||||
if(type != null) {
|
||||
dep.setType(type);
|
||||
}
|
||||
|
||||
parent.addDependency(dep);
|
||||
}
|
||||
|
||||
protected boolean alwaysProcessChildren() {
|
||||
// never process children...shouldn't even have any!
|
||||
return false;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,98 @@
|
|||
/* Created on Aug 7, 2004 */
|
||||
package org.apache.maven.plugin.loader.marmalade.tags;
|
||||
|
||||
import org.apache.maven.model.Dependency;
|
||||
import org.apache.maven.plugin.descriptor.MojoDescriptor;
|
||||
import org.apache.maven.plugin.descriptor.Parameter;
|
||||
import org.codehaus.marmalade.model.AbstractMarmaladeTag;
|
||||
import org.codehaus.marmalade.model.MarmaladeAttributes;
|
||||
import org.codehaus.marmalade.runtime.MarmaladeExecutionContext;
|
||||
import org.codehaus.marmalade.runtime.MarmaladeExecutionException;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author jdcasey
|
||||
*/
|
||||
public class MojoDescriptorTag extends AbstractMarmaladeTag
|
||||
implements DescriptionOwner
|
||||
{
|
||||
public static final String ID_ATTRIBUTE = "id";
|
||||
public static final String INSTANTIATION_STRATEGY_ATTRIBUTE = "instantiation-strategy";
|
||||
public static final String EXECUTION_STRATEGY_ATTRIBUTE = "execution-strategy";
|
||||
public static final String GOAL_ATTRIBUTE = "goal";
|
||||
|
||||
private String id;
|
||||
private String instantiationStrategy;
|
||||
private String executionStrategy;
|
||||
private String goal;
|
||||
private String description;
|
||||
private List prereqs = new LinkedList( );
|
||||
private List dependencies = new LinkedList( );
|
||||
private List parameters = new LinkedList();
|
||||
|
||||
protected void doExecute( MarmaladeExecutionContext context )
|
||||
throws MarmaladeExecutionException
|
||||
{
|
||||
this.id = ( String ) requireTagAttribute( ID_ATTRIBUTE, String.class,
|
||||
context );
|
||||
|
||||
MarmaladeAttributes attributes = getAttributes();
|
||||
this.instantiationStrategy = ( String ) attributes.getValue( INSTANTIATION_STRATEGY_ATTRIBUTE,
|
||||
String.class, context );
|
||||
|
||||
this.executionStrategy = ( String ) attributes.getValue( EXECUTION_STRATEGY_ATTRIBUTE,
|
||||
String.class, context );
|
||||
|
||||
this.goal = (String)attributes.getValue(GOAL_ATTRIBUTE, String.class, context);
|
||||
}
|
||||
|
||||
public List getDependencies( )
|
||||
{
|
||||
return dependencies;
|
||||
}
|
||||
|
||||
public MojoDescriptor getMojoDescriptor( )
|
||||
{
|
||||
MojoDescriptor descriptor = new MojoDescriptor( );
|
||||
|
||||
descriptor.setId( id );
|
||||
|
||||
if(instantiationStrategy != null) {
|
||||
descriptor.setInstantiationStrategy( instantiationStrategy );
|
||||
}
|
||||
|
||||
if(executionStrategy != null) {
|
||||
descriptor.setExecutionStrategy( executionStrategy );
|
||||
}
|
||||
|
||||
if(goal != null) {
|
||||
descriptor.setGoal(goal);
|
||||
}
|
||||
|
||||
descriptor.setPrereqs(prereqs);
|
||||
descriptor.setParameters(parameters);
|
||||
|
||||
return descriptor;
|
||||
}
|
||||
|
||||
public void setPrereqs( List prereqs )
|
||||
{
|
||||
this.prereqs = prereqs;
|
||||
}
|
||||
|
||||
public void setDependencies( List dependencies)
|
||||
{
|
||||
this.dependencies = dependencies;
|
||||
}
|
||||
|
||||
public void setDescription( String description )
|
||||
{
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public void setParameters(List parameters) {
|
||||
this.parameters = parameters;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,54 @@
|
|||
/* Created on Aug 7, 2004 */
|
||||
package org.apache.maven.plugin.loader.marmalade.tags;
|
||||
|
||||
import org.apache.maven.plugin.descriptor.Parameter;
|
||||
import org.codehaus.marmalade.model.AbstractMarmaladeTag;
|
||||
import org.codehaus.marmalade.model.MarmaladeAttribute;
|
||||
import org.codehaus.marmalade.model.MarmaladeAttributes;
|
||||
import org.codehaus.marmalade.runtime.MarmaladeExecutionContext;
|
||||
import org.codehaus.marmalade.runtime.MarmaladeExecutionException;
|
||||
|
||||
public class MojoParameterTag extends AbstractMarmaladeTag
|
||||
implements DescriptionOwner
|
||||
{
|
||||
public static final String NAME_ATTRIBUTE = "name";
|
||||
public static final String TYPE_ATTRIBUTE = "type";
|
||||
public static final String REQUIRED_ATTRIBUTE = "required";
|
||||
public static final String VALIDATOR_ATTRIBUTE = "validator";
|
||||
public static final String EXPRESSION_ATTRIBUTE = "expression";
|
||||
private Parameter param = new Parameter( );
|
||||
|
||||
protected void doExecute( MarmaladeExecutionContext context )
|
||||
throws MarmaladeExecutionException
|
||||
{
|
||||
MarmaladeAttributes attributes = getAttributes( );
|
||||
|
||||
param.setName( ( String ) requireTagAttribute( NAME_ATTRIBUTE,
|
||||
String.class, context ) );
|
||||
param.setRequired( ( ( Boolean ) requireTagAttribute(
|
||||
REQUIRED_ATTRIBUTE, Boolean.class, context ) ).booleanValue( ) );
|
||||
param.setType( ( String ) requireTagAttribute( TYPE_ATTRIBUTE,
|
||||
String.class, context ) );
|
||||
param.setValidator( ( String ) requireTagAttribute(
|
||||
VALIDATOR_ATTRIBUTE, String.class, context ) );
|
||||
param.setExpression( String.valueOf(requireTagAttribute(
|
||||
EXPRESSION_ATTRIBUTE, context ) ) );
|
||||
|
||||
processChildren( context );
|
||||
|
||||
if ( param.getDescription( ) == null )
|
||||
{
|
||||
throw new MarmaladeExecutionException(
|
||||
"mojo parameters require a description" );
|
||||
}
|
||||
|
||||
MojoParametersTag parent = ( MojoParametersTag ) requireParent( MojoParametersTag.class );
|
||||
|
||||
parent.addParameter( param );
|
||||
}
|
||||
|
||||
public void setDescription( String description )
|
||||
{
|
||||
param.setDescription( description );
|
||||
}
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
/* Created on Aug 7, 2004 */
|
||||
package org.apache.maven.plugin.loader.marmalade.tags;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.maven.plugin.descriptor.Parameter;
|
||||
import org.codehaus.marmalade.model.AbstractMarmaladeTag;
|
||||
import org.codehaus.marmalade.runtime.MarmaladeExecutionContext;
|
||||
import org.codehaus.marmalade.runtime.MarmaladeExecutionException;
|
||||
|
||||
/**
|
||||
* @author jdcasey
|
||||
*/
|
||||
public class MojoParametersTag extends AbstractMarmaladeTag {
|
||||
|
||||
private List parameters = new LinkedList();
|
||||
|
||||
protected void doExecute(MarmaladeExecutionContext context)
|
||||
throws MarmaladeExecutionException {
|
||||
|
||||
processChildren(context);
|
||||
|
||||
MojoDescriptorTag parent = (MojoDescriptorTag)requireParent(MojoDescriptorTag.class);
|
||||
parent.setParameters(parameters);
|
||||
}
|
||||
|
||||
public void addParameter(Parameter param) {
|
||||
parameters.add(param);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
/* Created on Aug 7, 2004 */
|
||||
package org.apache.maven.plugin.loader.marmalade.tags;
|
||||
|
||||
import org.codehaus.marmalade.el.ExpressionEvaluationException;
|
||||
import org.codehaus.marmalade.model.AbstractMarmaladeTag;
|
||||
import org.codehaus.marmalade.runtime.MarmaladeExecutionContext;
|
||||
import org.codehaus.marmalade.runtime.MarmaladeExecutionException;
|
||||
|
||||
public class MojoPrereqTag extends AbstractMarmaladeTag {
|
||||
|
||||
private static final String NAME_ATTRIBUTE = "name";
|
||||
|
||||
protected boolean alwaysProcessChildren() {
|
||||
// shouldn't even have children...
|
||||
return false;
|
||||
}
|
||||
|
||||
protected void doExecute(MarmaladeExecutionContext context)
|
||||
throws MarmaladeExecutionException {
|
||||
|
||||
String prereq = (String)requireTagAttribute(NAME_ATTRIBUTE, String.class, context);
|
||||
|
||||
MojoPrereqsTag parent = (MojoPrereqsTag)requireParent(MojoPrereqsTag.class);
|
||||
parent.addPrereq(prereq);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
/* Created on Aug 7, 2004 */
|
||||
package org.apache.maven.plugin.loader.marmalade.tags;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import org.codehaus.marmalade.model.AbstractMarmaladeTag;
|
||||
import org.codehaus.marmalade.runtime.MarmaladeExecutionContext;
|
||||
import org.codehaus.marmalade.runtime.MarmaladeExecutionException;
|
||||
|
||||
/**
|
||||
* @author jdcasey
|
||||
*/
|
||||
public class MojoPrereqsTag extends AbstractMarmaladeTag {
|
||||
|
||||
private List prereqs = new LinkedList();
|
||||
|
||||
protected void doExecute(MarmaladeExecutionContext context)
|
||||
throws MarmaladeExecutionException {
|
||||
|
||||
processChildren(context);
|
||||
|
||||
MojoDescriptorTag parent = (MojoDescriptorTag)requireParent(MojoDescriptorTag.class);
|
||||
parent.setPrereqs(prereqs);
|
||||
}
|
||||
|
||||
public void addPrereq(String prereq) {
|
||||
prereqs.add(prereq);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,69 @@
|
|||
/* Created on Aug 7, 2004 */
|
||||
package org.apache.maven.plugin.loader.marmalade.tags;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import org.apache.maven.plugin.descriptor.MojoDescriptor;
|
||||
import org.codehaus.marmalade.model.AbstractMarmaladeTag;
|
||||
import org.codehaus.marmalade.model.MarmaladeTag;
|
||||
import org.codehaus.marmalade.runtime.MarmaladeExecutionContext;
|
||||
import org.codehaus.marmalade.runtime.MarmaladeExecutionException;
|
||||
|
||||
/**
|
||||
* @author jdcasey
|
||||
*/
|
||||
public class MojoTag extends AbstractMarmaladeTag
|
||||
{
|
||||
private boolean describeOnly = false;
|
||||
|
||||
private List dependencies;
|
||||
private MojoDescriptor descriptor;
|
||||
|
||||
protected boolean alwaysProcessChildren( )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
protected void doExecute( MarmaladeExecutionContext context )
|
||||
throws MarmaladeExecutionException
|
||||
{
|
||||
boolean describeOnly = describeOnly();
|
||||
for (Iterator it = children().iterator(); it.hasNext();) {
|
||||
MarmaladeTag child = (MarmaladeTag) it.next();
|
||||
|
||||
if(describeOnly && (child instanceof MojoDescriptorTag)) {
|
||||
MojoDescriptorTag headerTag = (MojoDescriptorTag)child;
|
||||
child.execute(context);
|
||||
|
||||
this.descriptor = headerTag.getMojoDescriptor();
|
||||
this.dependencies = headerTag.getDependencies();
|
||||
|
||||
// we're done with the description phase.
|
||||
break;
|
||||
}
|
||||
else if(!describeOnly) {
|
||||
child.execute(context);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public MojoDescriptor getMojoDescriptor() {
|
||||
return descriptor;
|
||||
}
|
||||
|
||||
public List addDependencies(List accumulatedDependencies) {
|
||||
accumulatedDependencies.addAll(dependencies);
|
||||
return accumulatedDependencies;
|
||||
}
|
||||
|
||||
public void describeOnly(boolean describeOnly) {
|
||||
this.describeOnly = describeOnly;
|
||||
}
|
||||
|
||||
public boolean describeOnly() {
|
||||
return describeOnly;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
/* Created on Aug 7, 2004 */
|
||||
package org.apache.maven.plugin.loader.marmalade.tags;
|
||||
|
||||
import org.codehaus.marmalade.model.AbstractMarmaladeTagLibrary;
|
||||
|
||||
public class MojoTagLibrary extends AbstractMarmaladeTagLibrary {
|
||||
|
||||
public static final String MOJO_TAG = "mojo";
|
||||
|
||||
public static final String DESCRIPTOR_TAG = "descriptor";
|
||||
|
||||
public static final String PREREQS_TAG = "prereqs";
|
||||
public static final String PREREQ_TAG = "prereq";
|
||||
|
||||
public static final String PARAMETERS_TAG = "parameters";
|
||||
public static final String PARAMETER_TAG = "parameter";
|
||||
|
||||
public static final String DESCRIPTION_TAG = "description";
|
||||
|
||||
public static final String DEPENDENCIES_TAG = "dependencies";
|
||||
public static final String DEPENDENCY_TAG = "dependency";
|
||||
|
||||
public MojoTagLibrary() {
|
||||
registerTag(MOJO_TAG, MojoTag.class);
|
||||
|
||||
registerTag(DESCRIPTOR_TAG, MojoDescriptorTag.class);
|
||||
|
||||
registerTag(PREREQS_TAG, MojoPrereqsTag.class);
|
||||
registerTag(PREREQ_TAG, MojoPrereqTag.class);
|
||||
|
||||
registerTag(PARAMETERS_TAG, MojoParametersTag.class);
|
||||
registerTag(PARAMETER_TAG, MojoParameterTag.class);
|
||||
|
||||
registerTag(DESCRIPTION_TAG, DescriptionTag.class);
|
||||
|
||||
registerTag(DEPENDENCIES_TAG, MojoDependenciesTag.class);
|
||||
registerTag(DEPENDENCY_TAG, MojoDependencyTag.class);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
org.apache.maven.plugin.loader.marmalade.tags.MojoTagLibrary
|
|
@ -0,0 +1,157 @@
|
|||
/* Created on Aug 7, 2004 */
|
||||
package org.apache.maven.plugin.loader.marmalade;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
import java.util.List;
|
||||
import java.util.jar.JarEntry;
|
||||
import java.util.jar.JarOutputStream;
|
||||
|
||||
import org.apache.maven.plugin.MavenMojoDescriptor;
|
||||
import org.apache.maven.plugin.MavenPluginDescriptor;
|
||||
import org.apache.maven.plugin.descriptor.MojoDescriptor;
|
||||
import org.apache.maven.plugin.descriptor.Parameter;
|
||||
import org.codehaus.classworlds.ClassRealm;
|
||||
import org.codehaus.classworlds.ClassWorld;
|
||||
import org.codehaus.classworlds.DefaultClassRealm;
|
||||
import org.codehaus.plexus.component.repository.ComponentDependency;
|
||||
import org.codehaus.plexus.component.repository.ComponentDescriptor;
|
||||
import org.codehaus.plexus.component.repository.ComponentSetDescriptor;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
/**
|
||||
* @author jdcasey
|
||||
*/
|
||||
public class MarmaladePluginDiscovererTest extends TestCase {
|
||||
|
||||
private static final String TEST_MOJO_SCRIPT = "<?xml version=\"1.0\"?>" +
|
||||
"<mojo xmlns=\"marmalade:mojo\">" +
|
||||
"<descriptor id=\"test\" goal=\"myTest\" instantiation-strategy=\"singleton\" execution-strategy=\"always\">" +
|
||||
"<description>This is a test mojo script.</description>" +
|
||||
"<prereqs><prereq name=\"test-prereq\"/></prereqs>" +
|
||||
"<parameters>" +
|
||||
"<parameter name=\"param\" type=\"String\" required=\"true\" validator=\"something\" expression=\"false\">" +
|
||||
"<description>Parameter to test</description>" +
|
||||
"</parameter>" +
|
||||
"</parameters>" +
|
||||
"<dependencies>" +
|
||||
"<dependency groupId=\"marmalade\" artifactId=\"marmalade-core\" version=\"0.2\"/>" +
|
||||
"</dependencies>" +
|
||||
"</descriptor>" +
|
||||
"<c:set xmlns:c=\"marmalade:jstl-core\" var=\"testVar\" value=\"testValue\"/>" +
|
||||
"</mojo>";
|
||||
|
||||
public void testShouldFindPluginWithOneMojoFromClassDirectory() throws IOException {
|
||||
File directory = new File("test-dir");
|
||||
File scriptFile = new File(directory, "test.mmld");
|
||||
|
||||
try {
|
||||
if (!directory.exists()) {
|
||||
directory.mkdirs();
|
||||
}
|
||||
|
||||
BufferedWriter writer = new BufferedWriter(new FileWriter(
|
||||
scriptFile));
|
||||
writer.write(TEST_MOJO_SCRIPT);
|
||||
writer.flush();
|
||||
writer.close();
|
||||
|
||||
ClassRealm realm = new DefaultClassRealm(new ClassWorld(), "root");
|
||||
realm.addConstituent(directory.toURL());
|
||||
|
||||
MarmaladePluginDiscoverer discoverer = new MarmaladePluginDiscoverer();
|
||||
List componentSets = discoverer.findComponents(realm);
|
||||
|
||||
verifyComponentSets(componentSets);
|
||||
}
|
||||
finally {
|
||||
if(scriptFile.exists()) {
|
||||
scriptFile.delete();
|
||||
}
|
||||
|
||||
if(directory.exists()) {
|
||||
directory.delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void testShouldFindPluginWithOneMojoFromJar() throws IOException {
|
||||
File jar = new File("test-plugin.jar");
|
||||
|
||||
try {
|
||||
JarOutputStream jarOut = new JarOutputStream(new FileOutputStream(jar));
|
||||
jarOut.putNextEntry(new JarEntry("test.mmld"));
|
||||
jarOut.write(TEST_MOJO_SCRIPT.getBytes());
|
||||
jarOut.flush();
|
||||
jarOut.closeEntry();
|
||||
jarOut.close();
|
||||
|
||||
ClassRealm realm = new DefaultClassRealm(new ClassWorld(), "root");
|
||||
realm.addConstituent(new URL("jar:" + jar.toURL().toExternalForm() + "!/"));
|
||||
|
||||
MarmaladePluginDiscoverer discoverer = new MarmaladePluginDiscoverer();
|
||||
List componentSets = discoverer.findComponents(realm);
|
||||
|
||||
verifyComponentSets(componentSets);
|
||||
}
|
||||
finally {
|
||||
if(jar.exists()) {
|
||||
jar.delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
private void verifyComponentSets(List componentSets) {
|
||||
assertNotNull(componentSets);
|
||||
assertEquals(1, componentSets.size());
|
||||
|
||||
ComponentSetDescriptor setDescriptor = (ComponentSetDescriptor) componentSets
|
||||
.get(0);
|
||||
List components = setDescriptor.getComponents();
|
||||
|
||||
assertNotNull(components);
|
||||
assertEquals(1, components.size());
|
||||
|
||||
ComponentDescriptor descriptor = (ComponentDescriptor) components
|
||||
.get(0);
|
||||
|
||||
assertEquals("marmalade", descriptor.getComponentFactory());
|
||||
|
||||
MavenMojoDescriptor mojoDesc = (MavenMojoDescriptor) descriptor;
|
||||
|
||||
MojoDescriptor mojo = mojoDesc.getMojoDescriptor();
|
||||
assertEquals("test", mojo.getId());
|
||||
assertEquals("myTest", mojo.getGoal());
|
||||
assertEquals("singleton", mojo.getInstantiationStrategy());
|
||||
assertEquals("always", mojo.getExecutionStrategy());
|
||||
|
||||
List prereqs = mojoDesc.getPrereqs();
|
||||
|
||||
assertEquals(1, prereqs.size());
|
||||
assertEquals("test-prereq", prereqs.get(0));
|
||||
|
||||
List dependencies = setDescriptor.getDependencies();
|
||||
|
||||
assertEquals(1, dependencies.size());
|
||||
|
||||
ComponentDependency dep = (ComponentDependency)dependencies.get(0);
|
||||
assertEquals("marmalade", dep.getGroupId());
|
||||
assertEquals("marmalade-core", dep.getArtifactId());
|
||||
assertEquals("0.2", dep.getVersion());
|
||||
|
||||
List parameters = mojo.getParameters();
|
||||
assertEquals(1, parameters.size());
|
||||
|
||||
Parameter param = (Parameter)parameters.get(0);
|
||||
assertEquals("param", param.getName());
|
||||
assertEquals("String", param.getType());
|
||||
assertEquals(true, param.isRequired());
|
||||
assertEquals("something", param.getValidator());
|
||||
assertEquals("false", param.getExpression());
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue