You are viewing a plain text version of this content. The canonical link for it is here.
Posted to m2-dev@maven.apache.org by jd...@apache.org on 2005/03/01 03:43:22 UTC
cvs commit: maven-components/maven-plugin-tools/maven-plugin-tools-java/src/main/java/org/apache/maven/tools/plugin/extractor/java JavaMojoDescriptorExtractor.java
jdcasey 2005/02/28 18:43:22
Modified: maven-plugin-tools/maven-plugin-tools-marmalade/src/test/java/org/apache/maven/tools/plugin/extractor/marmalade
MarmaladeMojoDescriptorExtractorTest.java
maven-plugin-tools/maven-plugin-tools-java/src/test/resources/source
JavaExtractorTestTwo.java JavaExtractorTestOne.java
maven-core-it/it0015 pom.xml
maven-plugin-tools/maven-plugin-tools-api/src/test/java/org/apache/maven/tools/plugin/scanner
TestExtractor.java
maven-plugin-tools/maven-plugin-tools-java pom.xml
maven-plugin-tools/maven-plugin-tools-api/src/main/java/org/apache/maven/tools/plugin/extractor
MojoDescriptorExtractor.java
maven-plugin-tools/maven-plugin-tools-java/src/test/java/org/apache/maven/tools/plugin/extractor/java
JavaMojoDescriptorExtractorTest.java
maven-plugin-tools/maven-plugin-tools-marmalade/src/main/java/org/apache/maven/tools/plugin/extractor/marmalade
MarmaladeMojoDescriptorExtractor.java
maven-plugin-tools/maven-plugin-tools-api/src/main/java/org/apache/maven/tools/plugin/scanner
DefaultMojoScanner.java
maven-plugin-tools/maven-plugin-tools-java/src/main/java/org/apache/maven/tools/plugin/extractor/java
JavaMojoDescriptorExtractor.java
Added: maven-plugin/src/main/java/org/apache/maven/plugin/script
MojoScript.java
maven-plugin-tools/maven-plugin-tools-api/src/main/java/org/apache/maven/tools/plugin/extractor
AbstractScriptedMojoDescriptorExtractor.java
maven-core-it/it0015/src/main/scripts/org/apache/maven/it0015
it0015.mmld
Removed: maven-core-it/it0015/src/main/java/org/apache/maven/it0015
it0015.mmld
Log:
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.
Revision Changes Path
1.3 +14 -1 maven-components/maven-plugin-tools/maven-plugin-tools-marmalade/src/test/java/org/apache/maven/tools/plugin/extractor/marmalade/MarmaladeMojoDescriptorExtractorTest.java
Index: MarmaladeMojoDescriptorExtractorTest.java
===================================================================
RCS file: /home/cvs/maven-components/maven-plugin-tools/maven-plugin-tools-marmalade/src/test/java/org/apache/maven/tools/plugin/extractor/marmalade/MarmaladeMojoDescriptorExtractorTest.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- MarmaladeMojoDescriptorExtractorTest.java 28 Feb 2005 18:29:47 -0000 1.2
+++ MarmaladeMojoDescriptorExtractorTest.java 1 Mar 2005 02:43:22 -0000 1.3
@@ -16,7 +16,9 @@
* 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;
@@ -39,12 +41,23 @@
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() );
1.2 +0 -2 maven-components/maven-plugin-tools/maven-plugin-tools-java/src/test/resources/source/JavaExtractorTestTwo.java
Index: JavaExtractorTestTwo.java
===================================================================
RCS file: /home/cvs/maven-components/maven-plugin-tools/maven-plugin-tools-java/src/test/resources/source/JavaExtractorTestTwo.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- JavaExtractorTestTwo.java 20 Feb 2005 16:25:22 -0000 1.1
+++ JavaExtractorTestTwo.java 1 Mar 2005 02:43:22 -0000 1.2
@@ -1,5 +1,3 @@
-package source;
-
import org.apache.maven.project.MavenProject;
import org.apache.maven.plugin.AbstractPlugin;
import org.apache.maven.plugin.PluginExecutionRequest;
1.2 +0 -2 maven-components/maven-plugin-tools/maven-plugin-tools-java/src/test/resources/source/JavaExtractorTestOne.java
Index: JavaExtractorTestOne.java
===================================================================
RCS file: /home/cvs/maven-components/maven-plugin-tools/maven-plugin-tools-java/src/test/resources/source/JavaExtractorTestOne.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- JavaExtractorTestOne.java 20 Feb 2005 16:25:22 -0000 1.1
+++ JavaExtractorTestOne.java 1 Mar 2005 02:43:22 -0000 1.2
@@ -1,5 +1,3 @@
-package source;
-
import org.apache.maven.plugin.AbstractPlugin;
import org.apache.maven.plugin.PluginExecutionRequest;
import org.apache.maven.plugin.PluginExecutionResponse;
1.4 +1 -1 maven-components/maven-core-it/it0015/pom.xml
Index: pom.xml
===================================================================
RCS file: /home/cvs/maven-components/maven-core-it/it0015/pom.xml,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- pom.xml 28 Feb 2005 18:29:47 -0000 1.3
+++ pom.xml 1 Mar 2005 02:43:22 -0000 1.4
@@ -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>
1.1 maven-components/maven-plugin/src/main/java/org/apache/maven/plugin/script/MojoScript.java
Index: MojoScript.java
===================================================================
package org.apache.maven.plugin.script;
import org.apache.maven.plugin.descriptor.MojoDescriptor;
/**
* @author jdcasey
*/
public interface MojoScript
{
MojoDescriptor getMojoDescriptor();
}
1.3 +1 -1 maven-components/maven-plugin-tools/maven-plugin-tools-api/src/test/java/org/apache/maven/tools/plugin/scanner/TestExtractor.java
Index: TestExtractor.java
===================================================================
RCS file: /home/cvs/maven-components/maven-plugin-tools/maven-plugin-tools-api/src/test/java/org/apache/maven/tools/plugin/scanner/TestExtractor.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- TestExtractor.java 24 Feb 2005 05:12:29 -0000 1.2
+++ TestExtractor.java 1 Mar 2005 02:43:22 -0000 1.3
@@ -15,7 +15,7 @@
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" );
1.2 +9 -0 maven-components/maven-plugin-tools/maven-plugin-tools-java/pom.xml
Index: pom.xml
===================================================================
RCS file: /home/cvs/maven-components/maven-plugin-tools/maven-plugin-tools-java/pom.xml,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- pom.xml 20 Feb 2005 16:25:21 -0000 1.1
+++ pom.xml 1 Mar 2005 02:43:22 -0000 1.2
@@ -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>
\ No newline at end of file
1.3 +1 -1 maven-components/maven-plugin-tools/maven-plugin-tools-api/src/main/java/org/apache/maven/tools/plugin/extractor/MojoDescriptorExtractor.java
Index: MojoDescriptorExtractor.java
===================================================================
RCS file: /home/cvs/maven-components/maven-plugin-tools/maven-plugin-tools-api/src/main/java/org/apache/maven/tools/plugin/extractor/MojoDescriptorExtractor.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- MojoDescriptorExtractor.java 24 Feb 2005 05:12:30 -0000 1.2
+++ MojoDescriptorExtractor.java 1 Mar 2005 02:43:22 -0000 1.3
@@ -28,6 +28,6 @@
String ROLE = MojoDescriptorExtractor.class.getName();
- Set execute( String sourceDir, MavenProject project ) throws Exception;
+ Set execute( MavenProject project ) throws Exception;
}
\ No newline at end of file
1.1 maven-components/maven-plugin-tools/maven-plugin-tools-api/src/main/java/org/apache/maven/tools/plugin/extractor/AbstractScriptedMojoDescriptorExtractor.java
Index: AbstractScriptedMojoDescriptorExtractor.java
===================================================================
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;
}
}
1.1 maven-components/maven-core-it/it0015/src/main/scripts/org/apache/maven/it0015/it0015.mmld
Index: it0015.mmld
===================================================================
<?xml version="1.0"?>
<mojo xmlns="marmalade:mojo">
<metadata>
<id>it0015</id>
<goal>it0015</goal>
<description>Output a file with specified contents to [outDir]</description>
<parameters>
<parameter>
<name>outDir</name>
<expression>#project.build.directory</expression>
<default>target</default>
<description>Output directory for files.</description>
</parameter>
</parameters>
</metadata>
<execute>
<it0015:writeFile xmlns:it0015="marmalade:it0015" path="${outDir}/out.txt">This is a test.</it0015:writeFile>
</execute>
</mojo>
1.3 +10 -2 maven-components/maven-plugin-tools/maven-plugin-tools-java/src/test/java/org/apache/maven/tools/plugin/extractor/java/JavaMojoDescriptorExtractorTest.java
Index: JavaMojoDescriptorExtractorTest.java
===================================================================
RCS file: /home/cvs/maven-components/maven-plugin-tools/maven-plugin-tools-java/src/test/java/org/apache/maven/tools/plugin/extractor/java/JavaMojoDescriptorExtractorTest.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- JavaMojoDescriptorExtractorTest.java 24 Feb 2005 05:12:28 -0000 1.2
+++ JavaMojoDescriptorExtractorTest.java 1 Mar 2005 02:43:22 -0000 1.3
@@ -16,6 +16,7 @@
* limitations under the License.
*/
+import org.apache.maven.model.Build;
import org.apache.maven.model.Model;
import org.apache.maven.project.MavenProject;
@@ -40,13 +41,20 @@
System.out.println( "found source file: " + sourceFile );
File dir = sourceFile.getParentFile();
-
+
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 );
+
+ project.setFile(new File(dir, "pom.xml"));
- Set results = extractor.execute( dir.getAbsolutePath(), project );
+ Set results = extractor.execute( project );
assertEquals( 2, results.size() );
}
1.4 +37 -30 maven-components/maven-plugin-tools/maven-plugin-tools-marmalade/src/main/java/org/apache/maven/tools/plugin/extractor/marmalade/MarmaladeMojoDescriptorExtractor.java
Index: MarmaladeMojoDescriptorExtractor.java
===================================================================
RCS file: /home/cvs/maven-components/maven-plugin-tools/maven-plugin-tools-marmalade/src/main/java/org/apache/maven/tools/plugin/extractor/marmalade/MarmaladeMojoDescriptorExtractor.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- MarmaladeMojoDescriptorExtractor.java 28 Feb 2005 18:29:47 -0000 1.3
+++ MarmaladeMojoDescriptorExtractor.java 1 Mar 2005 02:43:22 -0000 1.4
@@ -17,11 +17,9 @@
*/
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.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,48 +42,56 @@
* @author jdcasey
*/
public class MarmaladeMojoDescriptorExtractor
- implements MojoDescriptorExtractor
+ extends AbstractScriptedMojoDescriptorExtractor
{
+
+ protected String getScriptFileExtension()
+ {
+ return ".mmld";
+ }
- public Set execute( String sourceDir, MavenProject project ) throws Exception
+ 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];
-
- File scriptFile = new File( dir, file );
+ Map.Entry entry = (Map.Entry) mapIterator.next();
- MarmaladeScript script = parse(scriptFile);
+ String basedir = (String)entry.getKey();
+ Set scriptFiles = (Set)entry.getValue();
- MarmaladeTag rootTag = script.getRoot();
- if(rootTag instanceof MojoTag)
+ for ( Iterator it = scriptFiles.iterator(); it.hasNext(); )
{
- Map contextMap = new TreeMap();
- contextMap.put( MarmaladeMojoExecutionDirectives.SCRIPT_BASEPATH_INVAR, sourceDir );
+ File scriptFile = (File) it.next();
- MarmaladeExecutionContext context = new DefaultContext(contextMap);
+ MarmaladeScript script = parse(scriptFile);
- script.execute(context);
-
- contextMap = context.getExternalizedVariables();
-
- MojoDescriptor descriptor = (MojoDescriptor) contextMap.get( MarmaladeMojoExecutionDirectives.METADATA_OUTVAR );
-
- descriptors.add( descriptor );
- }
- else
- {
- System.out.println("This script is not a mojo. Its root tag is {element: " + rootTag.getTagInfo().getElement() + ", class: " + rootTag.getClass().getName() + "}");
+ MarmaladeTag rootTag = script.getRoot();
+ if(rootTag instanceof MojoTag)
+ {
+ Map contextMap = new TreeMap();
+ contextMap.put( MarmaladeMojoExecutionDirectives.SCRIPT_BASEPATH_INVAR, basedir );
+
+ MarmaladeExecutionContext context = new DefaultContext(contextMap);
+
+ script.execute(context);
+
+ contextMap = context.getExternalizedVariables();
+
+ MojoDescriptor descriptor = (MojoDescriptor) contextMap.get( MarmaladeMojoExecutionDirectives.METADATA_OUTVAR );
+
+ descriptors.add( descriptor );
+ }
+ else
+ {
+ System.out.println("This script is not a mojo. Its root tag is {element: " + rootTag.getTagInfo().getElement() + ", class: " + rootTag.getClass().getName() + "}");
+ }
}
}
1.3 +1 -17 maven-components/maven-plugin-tools/maven-plugin-tools-api/src/main/java/org/apache/maven/tools/plugin/scanner/DefaultMojoScanner.java
Index: DefaultMojoScanner.java
===================================================================
RCS file: /home/cvs/maven-components/maven-plugin-tools/maven-plugin-tools-api/src/main/java/org/apache/maven/tools/plugin/scanner/DefaultMojoScanner.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- DefaultMojoScanner.java 24 Feb 2005 05:12:29 -0000 1.2
+++ DefaultMojoScanner.java 1 Mar 2005 02:43:22 -0000 1.3
@@ -50,22 +50,6 @@
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 @@
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." );
1.4 +23 -1 maven-components/maven-plugin-tools/maven-plugin-tools-java/src/main/java/org/apache/maven/tools/plugin/extractor/java/JavaMojoDescriptorExtractor.java
Index: JavaMojoDescriptorExtractor.java
===================================================================
RCS file: /home/cvs/maven-components/maven-plugin-tools/maven-plugin-tools-java/src/main/java/org/apache/maven/tools/plugin/extractor/java/JavaMojoDescriptorExtractor.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- JavaMojoDescriptorExtractor.java 27 Feb 2005 05:39:55 -0000 1.3
+++ JavaMojoDescriptorExtractor.java 1 Mar 2005 02:43:22 -0000 1.4
@@ -20,6 +20,8 @@
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 @@
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 );