Working on: MNG-897...added AntMojoDescriptorExtractor, PluginMetadataParser, and made small changes to the plugin metadata model used by scripts like Ant. This should be complete, except for wiring into the tool front-ends and testing with an Ant-based plugin.

git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@291823 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
John Dennis Casey 2005-09-27 04:46:37 +00:00
parent 7ea930ccb1
commit cd60968a2a
7 changed files with 432 additions and 44 deletions

View File

@ -0,0 +1,34 @@
<project>
<parent>
<artifactId>maven-plugin-tools</artifactId>
<groupId>org.apache.maven</groupId>
<version>2.0-beta-3-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-tools-ant</artifactId>
<name>Maven Ant Plugin Tools</name>
<version>2.0-beta-3-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-descriptor</artifactId>
<version>2.0-beta-3-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-tools-api</artifactId>
<version>2.0-beta-3-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-tools-model</artifactId>
<version>2.0-beta-3-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>plexus</groupId>
<artifactId>plexus-utils</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,105 @@
package org.apache.maven.tools.plugin.extractor.ant;
import org.apache.maven.plugin.descriptor.InvalidPluginDescriptorException;
import org.apache.maven.plugin.descriptor.MojoDescriptor;
import org.apache.maven.plugin.descriptor.PluginDescriptor;
import org.apache.maven.plugin.tools.model.PluginMetadataParseException;
import org.apache.maven.plugin.tools.model.PluginMetadataParser;
import org.apache.maven.tools.plugin.extractor.AbstractScriptedMojoDescriptorExtractor;
import org.apache.maven.tools.plugin.extractor.ExtractionException;
import org.codehaus.plexus.util.StringUtils;
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class AntMojoDescriptorExtractor
extends AbstractScriptedMojoDescriptorExtractor
{
private static final String METADATA_FILE_EXTENSION = ".plugin.xml";
private static final String SCRIPT_FILE_EXTENSION = ".xml";
protected List extractMojoDescriptors( Map scriptFilesKeyedByBasedir, PluginDescriptor pluginDescriptor )
throws ExtractionException, InvalidPluginDescriptorException
{
List descriptors = new ArrayList();
PluginMetadataParser parser = new PluginMetadataParser();
for ( Iterator mapIterator = scriptFilesKeyedByBasedir.entrySet().iterator(); mapIterator.hasNext(); )
{
Map.Entry entry = (Map.Entry) mapIterator.next();
String basedir = (String) entry.getKey();
Set scriptFiles = (Set) entry.getValue();
for ( Iterator it = scriptFiles.iterator(); it.hasNext(); )
{
File scriptMetadataFile = (File) it.next();
String basename = scriptMetadataFile.getName();
basename = basename.substring( 0, basename.length() - METADATA_FILE_EXTENSION.length() );
File scriptFile = new File( scriptMetadataFile.getParentFile(), basename + SCRIPT_FILE_EXTENSION );
if ( !scriptFile.exists() )
{
throw new InvalidPluginDescriptorException( "Found orphaned plugin metadata file: " + scriptMetadataFile );
}
String relativePath = null;
if ( basedir.endsWith( "/" ) )
{
basedir = basedir.substring( 0, basedir.length() - 2 );
}
relativePath = scriptFile.getPath().substring( basedir.length() );
relativePath = relativePath.replace( '\\', '/' );
try
{
Set mojoDescriptors = parser.parseMojoDescriptors( scriptMetadataFile );
for ( Iterator discoveredMojoIterator = mojoDescriptors.iterator(); discoveredMojoIterator.hasNext(); )
{
MojoDescriptor descriptor = (MojoDescriptor) discoveredMojoIterator.next();
String implementation = relativePath;
if ( StringUtils.isNotEmpty( descriptor.getImplementation() ) )
{
implementation += ":" + descriptor.getImplementation();
}
descriptor.setImplementation( implementation );
descriptor.setLanguage( "ant" );
descriptor.setComponentComposer( "map-oriented" );
descriptor.setComponentConfigurator( "map-oriented" );
descriptors.add( descriptor );
}
}
catch ( PluginMetadataParseException e )
{
throw new ExtractionException( "Error extracting mojo descriptor from script: " + scriptMetadataFile, e );
}
}
}
return descriptors;
}
protected String getScriptFileExtension()
{
return METADATA_FILE_EXTENSION;
}
}

View File

@ -0,0 +1,17 @@
<component-set>
<components>
<!--
|
| JavaMojoDescriptorExtractor, a MojoDescriptor extractor to read
| descriptors from java sources.
|
-->
<component>
<role>org.apache.maven.tools.plugin.extractor.MojoDescriptorExtractor</role>
<role-hint>ant</role-hint>
<implementation>org.apache.maven.tools.plugin.extractor.ant.AntMojoDescriptorExtractor</implementation>
</component>
</components>
</component-set>

View File

@ -1,20 +1,28 @@
<project>
<parent>
<artifactId>maven</artifactId>
<artifactId>maven-plugin-tools</artifactId>
<groupId>org.apache.maven</groupId>
<version>2.0-beta-2-SNAPSHOT</version>
<version>2.0-beta-3-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-tools-model</artifactId>
<name>Maven Plugin Metadata Model</name>
<version>2.0-beta-2-SNAPSHOT</version>
<version>2.0-beta-3-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>plexus</groupId>
<artifactId>plexus-container-default</artifactId>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-descriptor</artifactId>
<version>2.0-beta-3-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>plexus</groupId>
<artifactId>plexus-utils</artifactId>
<version>1.0.3</version>
</dependency>
</dependencies>
@ -23,7 +31,6 @@
<plugin>
<groupId>org.codehaus.modello</groupId>
<artifactId>modello-maven-plugin</artifactId>
<version>RELEASE</version>
<executions>
<execution>
<goals>
@ -34,7 +41,7 @@
</execution>
</executions>
<configuration>
<model>src/main/mdo/mojo-metadata.mdo</model>
<model>src/main/mdo/plugin-metadata.mdo</model>
<version>1.0.0</version>
</configuration>
</plugin>

View File

@ -0,0 +1,40 @@
package org.apache.maven.plugin.tools.model;
import java.io.File;
public class PluginMetadataParseException
extends Exception
{
static final long serialVersionUID = 1;
private final File metadataFile;
private final String originalMessage;
public PluginMetadataParseException( File metadataFile, String message, Throwable cause )
{
super( "Error parsing file: " + metadataFile + ". Reason: " + message, cause );
this.metadataFile = metadataFile;
this.originalMessage = message;
}
public PluginMetadataParseException( File metadataFile, String message )
{
super( "Error parsing file: " + metadataFile + ". Reason: " + message );
this.metadataFile = metadataFile;
this.originalMessage = message;
}
public File getMetadataFile()
{
return metadataFile;
}
public String getOriginalMessage()
{
return originalMessage;
}
}

View File

@ -0,0 +1,158 @@
package org.apache.maven.plugin.tools.model;
import org.apache.maven.plugin.descriptor.DuplicateParameterException;
import org.apache.maven.plugin.descriptor.MojoDescriptor;
import org.apache.maven.plugin.descriptor.Parameter;
import org.apache.maven.plugin.tools.model.io.xpp3.PluginMetadataXpp3Reader;
import org.codehaus.plexus.component.repository.ComponentRequirement;
import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.StringUtils;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
public class PluginMetadataParser
{
public Set parseMojoDescriptors( File metadataFile )
throws PluginMetadataParseException
{
Set descriptors = new HashSet();
Reader reader = null;
try
{
reader = new FileReader( metadataFile );
PluginMetadataXpp3Reader metadataReader = new PluginMetadataXpp3Reader();
PluginMetadata pluginMetadata = metadataReader.read( reader );
List mojos = pluginMetadata.getMojos();
if ( mojos != null && !mojos.isEmpty() )
{
for ( Iterator it = mojos.iterator(); it.hasNext(); )
{
Mojo mojo = (Mojo) it.next();
MojoDescriptor descriptor = asDescriptor( metadataFile, mojo );
descriptors.add( descriptor );
}
}
}
catch ( IOException e )
{
throw new PluginMetadataParseException( metadataFile, "Cannot parse plugin metadata from file.", e );
}
catch ( XmlPullParserException e )
{
throw new PluginMetadataParseException( metadataFile, "Cannot parse plugin metadata from file.", e );
}
finally
{
IOUtil.close( reader );
}
return descriptors;
}
private MojoDescriptor asDescriptor( File metadataFile, Mojo mojo )
throws PluginMetadataParseException
{
MojoDescriptor descriptor = new MojoDescriptor();
descriptor.setImplementation( mojo.getCall() );
descriptor.setGoal( mojo.getGoal() );
descriptor.setPhase( mojo.getPhase() );
descriptor.setDependencyResolutionRequired( mojo.getRequiresDependencyResolution() );
descriptor.setAggregator( mojo.isAggregator() );
descriptor.setInheritedByDefault( mojo.isInheritByDefault() );
descriptor.setDirectInvocationOnly( mojo.isRequiresDirectInvocation() );
descriptor.setOnlineRequired( mojo.isRequiresOnline() );
descriptor.setProjectRequired( mojo.isRequiresProject() );
descriptor.setRequiresReports( mojo.isRequiresReports() );
descriptor.setDescription( mojo.getDescription() );
descriptor.setDeprecated( mojo.getDeprecation() );
LifecycleExecution le = mojo.getExecution();
if ( le != null )
{
descriptor.setExecuteLifecycle( le.getLifecycle() );
descriptor.setExecutePhase( le.getPhase() );
}
List parameters = mojo.getParameters();
if ( parameters != null && !parameters.isEmpty() )
{
for ( Iterator it = parameters.iterator(); it.hasNext(); )
{
org.apache.maven.plugin.tools.model.Parameter param = (org.apache.maven.plugin.tools.model.Parameter) it.next();
Parameter dParam = new Parameter();
dParam.setAlias( param.getAlias() );
dParam.setDeprecated( param.getDeprecation() );
dParam.setDescription( param.getDescription() );
dParam.setEditable( !param.isReadonly() );
dParam.setExpression( param.getExpression() );
String property = param.getProperty();
if ( StringUtils.isNotEmpty( property ) )
{
dParam.setName( property );
}
else
{
dParam.setName( param.getName() );
}
if ( StringUtils.isEmpty( dParam.getName() ) )
{
throw new PluginMetadataParseException( metadataFile, "Mojo: \'" + mojo.getGoal() + "\' has a parameter without either property or name attributes. Please specify one." );
}
dParam.setRequired( param.isRequired() );
dParam.setType( param.getType() );
try
{
descriptor.addParameter( dParam );
}
catch ( DuplicateParameterException e )
{
throw new PluginMetadataParseException( metadataFile, "Duplicate parameters detected for mojo: " + mojo.getGoal(), e );
}
}
}
List components = mojo.getComponents();
if ( components != null && !components.isEmpty() )
{
for ( Iterator it = components.iterator(); it.hasNext(); )
{
Component component = (Component) it.next();
ComponentRequirement cr = new ComponentRequirement();
cr.setRole( component.getRole() );
cr.setRoleHint( component.getHint() );
descriptor.addRequirement( cr );
}
}
return descriptor;
}
}

View File

@ -1,41 +1,45 @@
<!--
<mojoMetadata goal="myGoal"
phase="compile"
requiresDependencyResolution="compile"
requiresProject="true"
requiresReports="true"
requiresOnline="true"
inheritByDefault="true"
requiresDirectInvocation="true"
aggregator="true">
<pluginMetadata>
<mojos>
<mojo goal="myGoal"
phase="compile"
requiresDependencyResolution="compile"
requiresProject="true"
requiresReports="true"
requiresOnline="true"
inheritByDefault="true"
requiresDirectInvocation="true"
aggregator="true">
<execute phase="initialize" lifecycle="mine"/>
<components>
<component role="stuff" hint="more"/>
</components>
<parameters>
<parameter name="nom"
property="prop"
required="true"
readonly="true"
expression="${project}"
type="org.apache.maven.project.MavenProject"
alias="otherProp">
<description>Test parameter</description>
<deprecated>Use something else</deprecated>
</parameter>
</parameters>
<description>
This is a test.
</description>
<deprecated>Use another mojo</deprecated>
</mojoMetadata>
<execute phase="initialize" lifecycle="mine"/>
<components>
<component role="stuff" hint="more"/>
</components>
<parameters>
<parameter name="nom"
property="prop"
required="true"
readonly="true"
expression="${project}"
type="org.apache.maven.project.MavenProject"
alias="otherProp">
<description>Test parameter</description>
<deprecated>Use something else</deprecated>
</parameter>
</parameters>
<description>
This is a test.
</description>
<deprecated>Use another mojo</deprecated>
</mojo>
</mojos>
</pluginMetadata>
-->
<model>
<id>mojo-metadata</id>
<name>MojoMetadata</name>
<id>plugin-metadata</id>
<name>PluginMetadata</name>
<description><![CDATA[
Mojo descriptor for embedding in scripts.
Plugin descriptor metadata for using with script-based mojos.
]]></description>
<defaults>
<default>
@ -44,10 +48,27 @@
</default>
</defaults>
<classes>
<class rootElement="true" xml.tagName="mojoMetadata">
<name>MojoMetadata</name>
<class rootElement="true" xml.tagName="pluginMetadata">
<name>PluginMetadata</name>
<version>1.0.0</version>
<description>Root element of the XML-based mojo descriptor definition.</description>
<description>Root element of a script-based mojo's plugin metadata bindings.</description>
<fields>
<field>
<name>mojos</name>
<version>1.0.0</version>
<required>true</required>
<description>The list of mojos contained in the accompanying script.</description>
<association>
<type>Mojo</type>
<multiplicity>*</multiplicity>
</association>
</field>
</fields>
</class>
<class>
<name>Mojo</name>
<version>1.0.0</version>
<description>Mojo descriptor definition.</description>
<fields>
<field xml.attribute="true">
<version>1.0.0</version>
@ -136,12 +157,18 @@
<description>The description for this parameter.</description>
<type>String</type>
</field>
<field>
<field xml.tagName="deprecated">
<name>deprecation</name>
<version>1.0.0</version>
<description>A deprecation message for this mojo parameter.</description>
<type>String</type>
</field>
<field xml.attribute="true">
<version>1.0.0</version>
<name>call</name>
<type>String</type>
<description>The target/method within the script to call when this mojo executes.</description>
</field>
</fields>
</class>
<class>
@ -252,7 +279,7 @@
<description>The description for this parameter.</description>
<type>String</type>
</field>
<field>
<field xml.tagName="deprecated">
<name>deprecation</name>
<version>1.0.0</version>
<description>A deprecation message for this mojo parameter.</description>