diff --git a/maven-plugin-tools-ant/src/main/java/org/apache/maven/tools/plugin/extractor/ant/AntMojoDescriptorExtractor.java b/maven-plugin-tools-ant/src/main/java/org/apache/maven/tools/plugin/extractor/ant/AntMojoDescriptorExtractor.java index 6958900..c244cf4 100644 --- a/maven-plugin-tools-ant/src/main/java/org/apache/maven/tools/plugin/extractor/ant/AntMojoDescriptorExtractor.java +++ b/maven-plugin-tools-ant/src/main/java/org/apache/maven/tools/plugin/extractor/ant/AntMojoDescriptorExtractor.java @@ -25,12 +25,16 @@ import org.apache.maven.plugin.descriptor.Parameter; 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.project.MavenProject; +import org.apache.maven.project.path.PathTranslator; import org.apache.maven.tools.plugin.extractor.AbstractScriptedMojoDescriptorExtractor; import org.apache.maven.tools.plugin.extractor.ExtractionException; +import org.codehaus.plexus.component.repository.ComponentRequirement; import org.codehaus.plexus.util.StringUtils; import java.io.File; import java.util.ArrayList; +import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; @@ -44,12 +48,12 @@ import java.util.Set; public class AntMojoDescriptorExtractor extends AbstractScriptedMojoDescriptorExtractor { - /** Default metada file extension */ + /** Default metadata file extension */ private static final String METADATA_FILE_EXTENSION = ".mojos.xml"; /** Default Ant build file extension */ private static final String SCRIPT_FILE_EXTENSION = ".build.xml"; - + /** {@inheritDoc} */ protected List extractMojoDescriptorsFromMetadata( Map metadataFilesKeyedByBasedir, PluginDescriptor pluginDescriptor ) @@ -83,14 +87,13 @@ public class AntMojoDescriptorExtractor String relativePath = null; - if ( basedir.endsWith( "/" ) ) - { - basedir = basedir.substring( 0, basedir.length() - 2 ); - } - relativePath = scriptFile.getPath().substring( basedir.length() ); - relativePath = relativePath.replace( '\\', '/' ); + + if ( relativePath.startsWith( "/" ) ) + { + relativePath = relativePath.substring( 1 ); + } try { @@ -132,14 +135,81 @@ public class AntMojoDescriptorExtractor descriptor.addParameter( param ); } + + if ( !paramMap.containsKey( "project" ) ) + { + Parameter param = new Parameter(); + param.setName( "project" ); + param.setDefaultValue( "${project}" ); + param.setType( MavenProject.class.getName() ); + param.setDescription( "The current MavenProject instance, which contains classpath elements." ); + param.setEditable( false ); + param.setRequired( true ); + + descriptor.addParameter( param ); + } + + if ( !paramMap.containsKey( "session" ) ) + { + Parameter param = new Parameter(); + param.setName( "session" ); + param.setDefaultValue( "${session}" ); + param.setType( "org.apache.maven.execution.MavenSession" ); + param.setDescription( "The current MavenSession instance, which is used for plugin-style expression resolution." ); + param.setEditable( false ); + param.setRequired( true ); + + descriptor.addParameter( param ); + } + + if ( !paramMap.containsKey( "mojoExecution" ) ) + { + Parameter param = new Parameter(); + param.setName( "mojoExecution" ); + param.setDefaultValue( "${mojoExecution}" ); + param.setType( "org.apache.maven.plugin.MojoExecution" ); + param.setDescription( "The current Maven MojoExecution instance, which contains information about the mojo currently executing." ); + param.setEditable( false ); + param.setRequired( true ); + + descriptor.addParameter( param ); + } + + List requirements = descriptor.getRequirements(); + Map reqMap = new HashMap(); + + if ( requirements != null ) + { + for ( Iterator reqIterator = requirements.iterator(); reqIterator.hasNext(); ) + { + ComponentRequirement req = (ComponentRequirement) reqIterator.next(); + + reqMap.put( req.getRole(), req ); + } + } + + if ( !reqMap.containsKey( PathTranslator.class.getName() ) ) + { + ComponentRequirement req = new ComponentRequirement(); + req.setRole( PathTranslator.class.getName() ); + + descriptor.addRequirement( req ); + } String implementation = relativePath; String dImpl = descriptor.getImplementation(); if ( StringUtils.isNotEmpty( dImpl ) ) { - implementation = - relativePath + dImpl.substring( PluginMetadataParser.IMPL_BASE_PLACEHOLDER.length() ); + if ( PluginMetadataParser.IMPL_BASE_PLACEHOLDER.equals( dImpl ) ) + { + implementation = relativePath; + } + else + { + implementation = + relativePath + dImpl.substring( PluginMetadataParser.IMPL_BASE_PLACEHOLDER.length() ); + } } descriptor.setImplementation( implementation ); diff --git a/maven-plugin-tools-ant/src/test/java/org/apache/maven/tools/plugin/extractor/ant/AntMojoDescriptorExtractorTest.java b/maven-plugin-tools-ant/src/test/java/org/apache/maven/tools/plugin/extractor/ant/AntMojoDescriptorExtractorTest.java new file mode 100644 index 0000000..92d5bf3 --- /dev/null +++ b/maven-plugin-tools-ant/src/test/java/org/apache/maven/tools/plugin/extractor/ant/AntMojoDescriptorExtractorTest.java @@ -0,0 +1,114 @@ +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.Parameter; +import org.apache.maven.plugin.descriptor.PluginDescriptor; +import org.apache.maven.project.path.PathTranslator; +import org.apache.maven.tools.plugin.extractor.ExtractionException; +import org.codehaus.plexus.component.repository.ComponentRequirement; +import org.codehaus.plexus.util.StringUtils; + +import java.io.File; +import java.net.URL; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import junit.framework.TestCase; + +public class AntMojoDescriptorExtractorTest + extends TestCase +{ + + public void testBasicMojoExtraction_CheckInjectedParametersAndRequirements() + throws InvalidPluginDescriptorException, ExtractionException + { + Map scriptMap = buildTestMap( "basic" ); + + PluginDescriptor pd = new PluginDescriptor(); + + pd.setArtifactId( "test-plugin" ); + pd.setGroupId( "org.mytest" ); + pd.setVersion( "1" ); + pd.setGoalPrefix( "mytest" ); + + List metadata = new AntMojoDescriptorExtractor().extractMojoDescriptorsFromMetadata( scriptMap, pd ); + + assertEquals( 2, metadata.size() ); + + for ( Iterator it = metadata.iterator(); it.hasNext(); ) + { + MojoDescriptor desc = (MojoDescriptor) it.next(); + + if ( "test".equals( desc.getGoal() ) ) + { + assertTrue( desc.getImplementation().indexOf( ":" ) < 0 ); + } + else if ( "test2".equals( desc.getGoal() ) ) + { + assertTrue( desc.getImplementation().endsWith( ":test2" ) ); + } + + List params = desc.getParameters(); + Map paramMap = new HashMap(); + for ( Iterator paramIterator = params.iterator(); paramIterator.hasNext(); ) + { + Parameter param = (Parameter) paramIterator.next(); + paramMap.put( param.getName(), param ); + } + + assertNotNull( "Mojo descriptor: " + desc.getGoal() + " is missing 'basedir' parameter.", paramMap.get( "basedir" ) ); + assertNotNull( "Mojo descriptor: " + desc.getGoal() + " is missing 'messageLevel' parameter.", paramMap.get( "messageLevel" ) ); + assertNotNull( "Mojo descriptor: " + desc.getGoal() + " is missing 'project' parameter.", paramMap.get( "project" ) ); + assertNotNull( "Mojo descriptor: " + desc.getGoal() + " is missing 'session' parameter.", paramMap.get( "session" ) ); + assertNotNull( "Mojo descriptor: " + desc.getGoal() + " is missing 'mojoExecution' parameter.", paramMap.get( "mojoExecution" ) ); + + List components = desc.getRequirements(); + + assertNotNull( components ); + assertEquals( 1, components.size() ); + + ComponentRequirement req = (ComponentRequirement) components.get( 0 ); + assertEquals( "Mojo descriptor: " + desc.getGoal() + " is missing 'PathTranslator' component requirement.", PathTranslator.class.getName(), req.getRole() ); + } + } + + private Map buildTestMap( String resourceDirName ) + { + Map result = new HashMap(); + + ClassLoader cloader = Thread.currentThread().getContextClassLoader(); + URL mojosXmlUrl = cloader.getResource( resourceDirName + "/test.mojos.xml" ); + + if ( mojosXmlUrl == null ) + { + fail( "No classpath resource named: '" + resourceDirName + "/test.mojos.xml' could be found." ); + } + + File mojosXml = new File( StringUtils.replace( mojosXmlUrl.getPath(), "%20", " " ) ); + File dir = mojosXml.getParentFile(); + + Set scripts = new HashSet(); + String[] listing = dir.list(); + for ( int i = 0; listing != null && i < listing.length; i++ ) + { + if ( listing[i].endsWith( ".mojos.xml" ) ) + { + File f = new File( dir, listing[i] ).getAbsoluteFile(); + + scripts.add( f ); + } + } + + result.put( dir.getAbsolutePath(), scripts ); + + return result; + } + + // TODO + +} diff --git a/maven-plugin-tools-ant/src/test/resources/basic/test.build.xml b/maven-plugin-tools-ant/src/test/resources/basic/test.build.xml new file mode 100644 index 0000000..b9aa0aa --- /dev/null +++ b/maven-plugin-tools-ant/src/test/resources/basic/test.build.xml @@ -0,0 +1,9 @@ + + + ${testDir} + + + + Another test target + + \ No newline at end of file diff --git a/maven-plugin-tools-ant/src/test/resources/basic/test.mojos.xml b/maven-plugin-tools-ant/src/test/resources/basic/test.mojos.xml new file mode 100644 index 0000000..cab908d --- /dev/null +++ b/maven-plugin-tools-ant/src/test/resources/basic/test.mojos.xml @@ -0,0 +1,54 @@ + + + + test + + + testDir + testDir + true + java.lang.String + Test directory location. + + + Runs the default ("test") goal of the build script. + + + test2 + test2 + + + org.apache.maven.project.path.PathTranslator + + + + + testDir + testDir + true + java.lang.String + Test directory location. + + + project + project + true + org.apache.maven.project.MavenProject + + + session + session + true + org.apache.maven.execution.MavenSession + + + mojoExecution + mojoExecution + true + org.apache.maven.plugin.MojoExecution + + + Runs the default ("test2") goal of the build script. + + + \ No newline at end of file diff --git a/maven-plugin-tools-model/src/main/java/org/apache/maven/plugin/tools/model/PluginMetadataParser.java b/maven-plugin-tools-model/src/main/java/org/apache/maven/plugin/tools/model/PluginMetadataParser.java index 7b1d494..8a91f55 100644 --- a/maven-plugin-tools-model/src/main/java/org/apache/maven/plugin/tools/model/PluginMetadataParser.java +++ b/maven-plugin-tools-model/src/main/java/org/apache/maven/plugin/tools/model/PluginMetadataParser.java @@ -109,7 +109,14 @@ public class PluginMetadataParser { MojoDescriptor descriptor = new MojoDescriptor(); - descriptor.setImplementation( IMPL_BASE_PLACEHOLDER + ":" + mojo.getCall() ); + if ( mojo.getCall() != null ) + { + descriptor.setImplementation( IMPL_BASE_PLACEHOLDER + ":" + mojo.getCall() ); + } + else + { + descriptor.setImplementation( IMPL_BASE_PLACEHOLDER ); + } descriptor.setGoal( mojo.getGoal() ); descriptor.setPhase( mojo.getPhase() ); diff --git a/maven-plugin-tools-model/src/test/java/org/apache/maven/plugin/tools/model/PluginMetadataParserTest.java b/maven-plugin-tools-model/src/test/java/org/apache/maven/plugin/tools/model/PluginMetadataParserTest.java new file mode 100644 index 0000000..b8cafb3 --- /dev/null +++ b/maven-plugin-tools-model/src/test/java/org/apache/maven/plugin/tools/model/PluginMetadataParserTest.java @@ -0,0 +1,53 @@ +package org.apache.maven.plugin.tools.model; + +import org.apache.maven.plugin.descriptor.MojoDescriptor; +import org.codehaus.plexus.util.StringUtils; + +import java.io.File; +import java.net.URL; +import java.util.Set; + +import junit.framework.TestCase; + +public class PluginMetadataParserTest + extends TestCase +{ + + public void testBasicDeclarationWithoutCall() + throws PluginMetadataParseException + { + File metadataFile = getMetadataFile( "test.mojos.xml" ); + Set descriptors = new PluginMetadataParser().parseMojoDescriptors( metadataFile ); + + assertEquals( 1, descriptors.size() ); + + MojoDescriptor desc = (MojoDescriptor) descriptors.iterator().next(); + assertTrue( desc.getImplementation().indexOf( ":" ) < 0 ); + assertEquals( "test", desc.getGoal() ); + } + + public void testBasicDeclarationWithCall() + throws PluginMetadataParseException + { + File metadataFile = getMetadataFile( "test2.mojos.xml" ); + Set descriptors = new PluginMetadataParser().parseMojoDescriptors( metadataFile ); + + assertEquals( 1, descriptors.size() ); + + MojoDescriptor desc = (MojoDescriptor) descriptors.iterator().next(); + assertTrue( desc.getImplementation().endsWith( ":test2" ) ); + assertEquals( "test2", desc.getGoal() ); + } + + private File getMetadataFile( String name ) + { + URL resource = Thread.currentThread().getContextClassLoader().getResource( name ); + if ( resource == null ) + { + fail( "Cannot find classpath resource: '" + name + "'." ); + } + + return new File( StringUtils.replace( resource.getPath(), "%20", " " ) ); + } + +} diff --git a/maven-plugin-tools-model/src/test/resources/test.mojos.xml b/maven-plugin-tools-model/src/test/resources/test.mojos.xml new file mode 100644 index 0000000..1becbe7 --- /dev/null +++ b/maven-plugin-tools-model/src/test/resources/test.mojos.xml @@ -0,0 +1,17 @@ + + + + test + + + testDir + testDir + true + java.lang.String + Test directory location. + + + Runs the default ("test") goal of the build script. + + + \ No newline at end of file diff --git a/maven-plugin-tools-model/src/test/resources/test2.mojos.xml b/maven-plugin-tools-model/src/test/resources/test2.mojos.xml new file mode 100644 index 0000000..495ac04 --- /dev/null +++ b/maven-plugin-tools-model/src/test/resources/test2.mojos.xml @@ -0,0 +1,18 @@ + + + + test2 + test2 + + + testDir + testDir + true + java.lang.String + Test directory location. + + + Runs the default ("test2") goal of the build script. + + + \ No newline at end of file