o Refactored the plugin extractors to accept a MavenProject only, and pull the information they need from it in order to extract the mojos they're responsible for.

git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@163435 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
John Dennis Casey 2005-03-01 02:43:22 +00:00
parent 8fc9a29003
commit 8e82989334
14 changed files with 213 additions and 58 deletions

View File

@ -25,7 +25,7 @@
<resources>
<resource><directory>src/main/resources</directory></resource>
<resource>
<directory>src/main/java</directory>
<directory>src/main/scripts</directory>
<includes><include>**/*.mmld</include></includes>
</resource>
</resources>

View File

@ -0,0 +1,103 @@
package org.apache.maven.tools.plugin.extractor;
import org.apache.maven.model.Build;
import org.apache.maven.model.Resource;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.util.DirectoryScanner;
import java.io.File;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
/**
* @author jdcasey
*/
public abstract class AbstractScriptedMojoDescriptorExtractor
implements MojoDescriptorExtractor
{
public Set execute( MavenProject project ) throws Exception
{
Build buildSection = project.getBuild();
List resources = null;
if(buildSection != null)
{
resources = buildSection.getResources();
}
Map scriptFilesKeyedByBasedir = gatherScriptSourcesByBasedir(resources, getScriptFileExtension());
Set mojoDescriptors = extractMojoDescriptors(scriptFilesKeyedByBasedir);
return mojoDescriptors;
}
protected abstract Set extractMojoDescriptors( Map scriptFilesKeyedByBasedir ) throws Exception;
protected abstract String getScriptFileExtension();
protected Map gatherScriptSourcesByBasedir( List resources, String scriptFileExtension )
{
Map sourcesByBasedir = new TreeMap();
if(resources != null)
{
for ( Iterator it = resources.iterator(); it.hasNext(); )
{
Set sources = new HashSet();
Resource resource = (Resource) it.next();
String resourceDir = resource.getDirectory();
File dir = new File(resourceDir);
if(dir.exists())
{
DirectoryScanner scanner = new DirectoryScanner();
scanner.setBasedir(dir);
List includes = resource.getIncludes();
if(includes != null && !includes.isEmpty())
{
scanner.setIncludes((String[])includes.toArray(new String[includes.size()]));
}
List excludes = resource.getExcludes();
if(excludes != null && !excludes.isEmpty())
{
scanner.setExcludes((String[])excludes.toArray(new String[excludes.size()]));
}
scanner.addDefaultExcludes();
scanner.scan();
String[] relativePaths = scanner.getIncludedFiles();
for ( int i = 0; i < relativePaths.length; i++ )
{
String relativePath = relativePaths[i];
File scriptFile = new File(dir, relativePath);
if(scriptFile.isFile() && relativePath.endsWith(scriptFileExtension))
{
sources.add(scriptFile);
}
}
sourcesByBasedir.put(resourceDir, sources);
}
}
}
return sourcesByBasedir;
}
}

View File

@ -28,6 +28,6 @@ public interface MojoDescriptorExtractor
String ROLE = MojoDescriptorExtractor.class.getName();
Set execute( String sourceDir, MavenProject project ) throws Exception;
Set execute( MavenProject project ) throws Exception;
}

View File

@ -50,22 +50,6 @@ public class DefaultMojoScanner
System.out.println( "Using " + mojoDescriptorExtractors.size() + " extractors." );
String sourceDir = null;
File basedir = project.getBasedir();
Build buildSection = project.getBuild();
if ( buildSection != null )
{
sourceDir = buildSection.getSourceDirectory();
}
if ( sourceDir == null )
{
File src = new File( basedir, "src/main/java" );
sourceDir = src.getPath();
}
for ( Iterator it = mojoDescriptorExtractors.entrySet().iterator(); it.hasNext(); )
{
Map.Entry entry = (Map.Entry) it.next();
@ -74,7 +58,7 @@ public class DefaultMojoScanner
System.out.println( "Applying extractor for language: " + language );
Set extractorDescriptors = extractor.execute( sourceDir, project );
Set extractorDescriptors = extractor.execute( project );
System.out.println( "Extractor for language: " + language + " found " + extractorDescriptors.size()
+ " mojo descriptors." );

View File

@ -15,7 +15,7 @@ public class TestExtractor
implements MojoDescriptorExtractor
{
public Set execute( String sourceDir, MavenProject project ) throws Exception
public Set execute( MavenProject project ) throws Exception
{
MojoDescriptor desc = new MojoDescriptor();
desc.setId( "testPluginId" );

View File

@ -47,4 +47,13 @@
<version>2.0-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<unitTest>
<resources>
<resource>
<directory>src/test/resources</directory>
</resource>
</resources>
</unitTest>
</build>
</project>

View File

@ -20,6 +20,8 @@ import com.thoughtworks.qdox.JavaDocBuilder;
import com.thoughtworks.qdox.model.DocletTag;
import com.thoughtworks.qdox.model.JavaClass;
import com.thoughtworks.qdox.model.JavaSource;
import org.apache.maven.model.Build;
import org.apache.maven.plugin.descriptor.MojoDescriptor;
import org.apache.maven.plugin.descriptor.Parameter;
import org.apache.maven.project.MavenProject;
@ -241,10 +243,30 @@ public class JavaMojoDescriptorExtractor
return javaSource.getClasses()[0];
}
public Set execute( String sourceDir, MavenProject project ) throws Exception
public Set execute( MavenProject project ) throws Exception
{
JavaDocBuilder builder = new JavaDocBuilder();
File basedir = project.getBasedir();
System.out.println("Project basedir: " + basedir);
String sourceDir = null;
Build buildSection = project.getBuild();
if ( buildSection != null )
{
sourceDir = buildSection.getSourceDirectory();
}
if ( sourceDir == null )
{
File src = new File( basedir, "src/main/java" );
sourceDir = src.getPath();
}
System.out.println("Source directory for java mojo extraction: " + sourceDir);
File sourceDirectoryFile = new File( sourceDir );
builder.addSourceTree( sourceDirectoryFile );

View File

@ -16,6 +16,7 @@ package org.apache.maven.tools.plugin.extractor.java;
* limitations under the License.
*/
import org.apache.maven.model.Build;
import org.apache.maven.model.Model;
import org.apache.maven.project.MavenProject;
@ -44,9 +45,16 @@ public class JavaMojoDescriptorExtractorTest
Model model = new Model();
model.setArtifactId( "maven-unitTesting-plugin" );
Build build = new Build();
build.setSourceDirectory(new File(dir, "source").getPath());
model.setBuild(build);
MavenProject project = new MavenProject( model );
Set results = extractor.execute( dir.getAbsolutePath(), project );
project.setFile(new File(dir, "pom.xml"));
Set results = extractor.execute( project );
assertEquals( 2, results.size() );
}

View File

@ -1,5 +1,3 @@
package source;
import org.apache.maven.plugin.AbstractPlugin;
import org.apache.maven.plugin.PluginExecutionRequest;
import org.apache.maven.plugin.PluginExecutionResponse;

View File

@ -1,5 +1,3 @@
package source;
import org.apache.maven.project.MavenProject;
import org.apache.maven.plugin.AbstractPlugin;
import org.apache.maven.plugin.PluginExecutionRequest;

View File

@ -17,11 +17,9 @@ package org.apache.maven.tools.plugin.extractor.marmalade;
*/
import org.apache.maven.plugin.descriptor.MojoDescriptor;
import org.apache.maven.project.MavenProject;
import org.apache.maven.script.marmalade.MarmaladeMojoExecutionDirectives;
import org.apache.maven.script.marmalade.tags.MojoTag;
import org.apache.maven.tools.plugin.extractor.MojoDescriptorExtractor;
import org.apache.maven.tools.plugin.util.PluginUtils;
import org.apache.maven.tools.plugin.extractor.AbstractScriptedMojoDescriptorExtractor;
import org.codehaus.marmalade.metamodel.ScriptBuilder;
import org.codehaus.marmalade.model.MarmaladeScript;
import org.codehaus.marmalade.model.MarmaladeTag;
@ -35,6 +33,7 @@ import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
@ -43,26 +42,33 @@ import java.util.TreeMap;
* @author jdcasey
*/
public class MarmaladeMojoDescriptorExtractor
implements MojoDescriptorExtractor
extends AbstractScriptedMojoDescriptorExtractor
{
public Set execute( String sourceDir, MavenProject project ) throws Exception
protected String getScriptFileExtension()
{
return ".mmld";
}
protected Set extractMojoDescriptors( Map sourceFilesKeyedByBasedir ) throws Exception
{
ClassLoader oldCl = Thread.currentThread().getContextClassLoader();
try
{
Thread.currentThread().setContextClassLoader(MarmaladeMojoDescriptorExtractor.class.getClassLoader());
String[] files = PluginUtils.findSources( sourceDir, "**/*.mmld" );
Set descriptors = new HashSet();
File dir = new File( sourceDir );
for ( int i = 0; i < files.length; i++ )
for ( Iterator mapIterator = sourceFilesKeyedByBasedir.entrySet().iterator(); mapIterator.hasNext(); )
{
String file = files[i];
Map.Entry entry = (Map.Entry) mapIterator.next();
File scriptFile = new File( dir, file );
String basedir = (String)entry.getKey();
Set scriptFiles = (Set)entry.getValue();
for ( Iterator it = scriptFiles.iterator(); it.hasNext(); )
{
File scriptFile = (File) it.next();
MarmaladeScript script = parse(scriptFile);
@ -70,7 +76,7 @@ public class MarmaladeMojoDescriptorExtractor
if(rootTag instanceof MojoTag)
{
Map contextMap = new TreeMap();
contextMap.put( MarmaladeMojoExecutionDirectives.SCRIPT_BASEPATH_INVAR, sourceDir );
contextMap.put( MarmaladeMojoExecutionDirectives.SCRIPT_BASEPATH_INVAR, basedir );
MarmaladeExecutionContext context = new DefaultContext(contextMap);
@ -87,6 +93,7 @@ public class MarmaladeMojoDescriptorExtractor
System.out.println("This script is not a mojo. Its root tag is {element: " + rootTag.getTagInfo().getElement() + ", class: " + rootTag.getClass().getName() + "}");
}
}
}
return descriptors;
}

View File

@ -16,7 +16,9 @@ package org.apache.maven.tools.plugin.extractor.marmalade;
* limitations under the License.
*/
import org.apache.maven.model.Build;
import org.apache.maven.model.Model;
import org.apache.maven.model.Resource;
import org.apache.maven.plugin.descriptor.MojoDescriptor;
import org.apache.maven.project.MavenProject;
import org.apache.maven.tools.plugin.extractor.MojoDescriptorExtractor;
@ -40,11 +42,22 @@ public class MarmaladeMojoDescriptorExtractorTest
Model model = new Model();
model.setArtifactId( "testArtifactId" );
Build build = new Build();
Resource resource = new Resource();
resource.setDirectory(basedir.getPath());
build.addResource(resource);
model.setBuild(build);
MavenProject project = new MavenProject( model );
project.setFile(new File(basedir, "pom.xml"));
MarmaladeMojoDescriptorExtractor extractor = (MarmaladeMojoDescriptorExtractor) lookup(MojoDescriptorExtractor.ROLE, "marmalade");
Set descriptors = extractor.execute( basedir.getPath(), project );
Set descriptors = extractor.execute( project );
assertEquals( 1, descriptors.size() );

View File

@ -0,0 +1,13 @@
package org.apache.maven.plugin.script;
import org.apache.maven.plugin.descriptor.MojoDescriptor;
/**
* @author jdcasey
*/
public interface MojoScript
{
MojoDescriptor getMojoDescriptor();
}