[MNG-3971] Injecting plugin parameters necessary to provide standard classpaths currently available in the antrun plugin, and also plugin-style expression evaluation within the Ant script itself.
git-svn-id: https://svn.apache.org/repos/asf/maven/plugin-tools/trunk@743583 13f79535-47bb-0310-9956-ffa450edef68master
parent
9cc4386b3a
commit
9d329bb771
|
|
@ -25,12 +25,16 @@ import org.apache.maven.plugin.descriptor.Parameter;
|
||||||
import org.apache.maven.plugin.descriptor.PluginDescriptor;
|
import org.apache.maven.plugin.descriptor.PluginDescriptor;
|
||||||
import org.apache.maven.plugin.tools.model.PluginMetadataParseException;
|
import org.apache.maven.plugin.tools.model.PluginMetadataParseException;
|
||||||
import org.apache.maven.plugin.tools.model.PluginMetadataParser;
|
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.AbstractScriptedMojoDescriptorExtractor;
|
||||||
import org.apache.maven.tools.plugin.extractor.ExtractionException;
|
import org.apache.maven.tools.plugin.extractor.ExtractionException;
|
||||||
|
import org.codehaus.plexus.component.repository.ComponentRequirement;
|
||||||
import org.codehaus.plexus.util.StringUtils;
|
import org.codehaus.plexus.util.StringUtils;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
@ -44,7 +48,7 @@ import java.util.Set;
|
||||||
public class AntMojoDescriptorExtractor
|
public class AntMojoDescriptorExtractor
|
||||||
extends AbstractScriptedMojoDescriptorExtractor
|
extends AbstractScriptedMojoDescriptorExtractor
|
||||||
{
|
{
|
||||||
/** Default metada file extension */
|
/** Default metadata file extension */
|
||||||
private static final String METADATA_FILE_EXTENSION = ".mojos.xml";
|
private static final String METADATA_FILE_EXTENSION = ".mojos.xml";
|
||||||
|
|
||||||
/** Default Ant build file extension */
|
/** Default Ant build file extension */
|
||||||
|
|
@ -83,15 +87,14 @@ public class AntMojoDescriptorExtractor
|
||||||
|
|
||||||
String relativePath = null;
|
String relativePath = null;
|
||||||
|
|
||||||
if ( basedir.endsWith( "/" ) )
|
|
||||||
{
|
|
||||||
basedir = basedir.substring( 0, basedir.length() - 2 );
|
|
||||||
}
|
|
||||||
|
|
||||||
relativePath = scriptFile.getPath().substring( basedir.length() );
|
relativePath = scriptFile.getPath().substring( basedir.length() );
|
||||||
|
|
||||||
relativePath = relativePath.replace( '\\', '/' );
|
relativePath = relativePath.replace( '\\', '/' );
|
||||||
|
|
||||||
|
if ( relativePath.startsWith( "/" ) )
|
||||||
|
{
|
||||||
|
relativePath = relativePath.substring( 1 );
|
||||||
|
}
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
Set mojoDescriptors = parser.parseMojoDescriptors( metadataFile );
|
Set mojoDescriptors = parser.parseMojoDescriptors( metadataFile );
|
||||||
|
|
@ -133,14 +136,81 @@ public class AntMojoDescriptorExtractor
|
||||||
descriptor.addParameter( param );
|
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 implementation = relativePath;
|
||||||
|
|
||||||
String dImpl = descriptor.getImplementation();
|
String dImpl = descriptor.getImplementation();
|
||||||
if ( StringUtils.isNotEmpty( dImpl ) )
|
if ( StringUtils.isNotEmpty( dImpl ) )
|
||||||
|
{
|
||||||
|
if ( PluginMetadataParser.IMPL_BASE_PLACEHOLDER.equals( dImpl ) )
|
||||||
|
{
|
||||||
|
implementation = relativePath;
|
||||||
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
implementation =
|
implementation =
|
||||||
relativePath + dImpl.substring( PluginMetadataParser.IMPL_BASE_PLACEHOLDER.length() );
|
relativePath + dImpl.substring( PluginMetadataParser.IMPL_BASE_PLACEHOLDER.length() );
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
descriptor.setImplementation( implementation );
|
descriptor.setImplementation( implementation );
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,9 @@
|
||||||
|
<project default="test">
|
||||||
|
<target name="test">
|
||||||
|
<echo>${testDir}</echo>
|
||||||
|
</target>
|
||||||
|
|
||||||
|
<target name="test2" depends="test">
|
||||||
|
<echo>Another test target</echo>
|
||||||
|
</target>
|
||||||
|
</project>
|
||||||
|
|
@ -0,0 +1,54 @@
|
||||||
|
<pluginMetadata>
|
||||||
|
<mojos>
|
||||||
|
<mojo>
|
||||||
|
<goal>test</goal>
|
||||||
|
<parameters>
|
||||||
|
<parameter>
|
||||||
|
<name>testDir</name>
|
||||||
|
<property>testDir</property>
|
||||||
|
<required>true</required>
|
||||||
|
<type>java.lang.String</type>
|
||||||
|
<description>Test directory location.</description>
|
||||||
|
</parameter>
|
||||||
|
</parameters>
|
||||||
|
<description>Runs the default ("test") goal of the build script.</description>
|
||||||
|
</mojo>
|
||||||
|
<mojo>
|
||||||
|
<goal>test2</goal>
|
||||||
|
<call>test2</call>
|
||||||
|
<components>
|
||||||
|
<component>
|
||||||
|
<role>org.apache.maven.project.path.PathTranslator</role>
|
||||||
|
</component>
|
||||||
|
</components>
|
||||||
|
<parameters>
|
||||||
|
<parameter>
|
||||||
|
<name>testDir</name>
|
||||||
|
<property>testDir</property>
|
||||||
|
<required>true</required>
|
||||||
|
<type>java.lang.String</type>
|
||||||
|
<description>Test directory location.</description>
|
||||||
|
</parameter>
|
||||||
|
<parameter>
|
||||||
|
<name>project</name>
|
||||||
|
<property>project</property>
|
||||||
|
<required>true</required>
|
||||||
|
<type>org.apache.maven.project.MavenProject</type>
|
||||||
|
</parameter>
|
||||||
|
<parameter>
|
||||||
|
<name>session</name>
|
||||||
|
<property>session</property>
|
||||||
|
<required>true</required>
|
||||||
|
<type>org.apache.maven.execution.MavenSession</type>
|
||||||
|
</parameter>
|
||||||
|
<parameter>
|
||||||
|
<name>mojoExecution</name>
|
||||||
|
<property>mojoExecution</property>
|
||||||
|
<required>true</required>
|
||||||
|
<type>org.apache.maven.plugin.MojoExecution</type>
|
||||||
|
</parameter>
|
||||||
|
</parameters>
|
||||||
|
<description>Runs the default ("test2") goal of the build script.</description>
|
||||||
|
</mojo>
|
||||||
|
</mojos>
|
||||||
|
</pluginMetadata>
|
||||||
|
|
@ -109,7 +109,14 @@ public class PluginMetadataParser
|
||||||
{
|
{
|
||||||
MojoDescriptor descriptor = new MojoDescriptor();
|
MojoDescriptor descriptor = new MojoDescriptor();
|
||||||
|
|
||||||
|
if ( mojo.getCall() != null )
|
||||||
|
{
|
||||||
descriptor.setImplementation( IMPL_BASE_PLACEHOLDER + ":" + mojo.getCall() );
|
descriptor.setImplementation( IMPL_BASE_PLACEHOLDER + ":" + mojo.getCall() );
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
descriptor.setImplementation( IMPL_BASE_PLACEHOLDER );
|
||||||
|
}
|
||||||
|
|
||||||
descriptor.setGoal( mojo.getGoal() );
|
descriptor.setGoal( mojo.getGoal() );
|
||||||
descriptor.setPhase( mojo.getPhase() );
|
descriptor.setPhase( mojo.getPhase() );
|
||||||
|
|
|
||||||
|
|
@ -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", " " ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,17 @@
|
||||||
|
<pluginMetadata>
|
||||||
|
<mojos>
|
||||||
|
<mojo>
|
||||||
|
<goal>test</goal>
|
||||||
|
<parameters>
|
||||||
|
<parameter>
|
||||||
|
<name>testDir</name>
|
||||||
|
<property>testDir</property>
|
||||||
|
<required>true</required>
|
||||||
|
<type>java.lang.String</type>
|
||||||
|
<description>Test directory location.</description>
|
||||||
|
</parameter>
|
||||||
|
</parameters>
|
||||||
|
<description>Runs the default ("test") goal of the build script.</description>
|
||||||
|
</mojo>
|
||||||
|
</mojos>
|
||||||
|
</pluginMetadata>
|
||||||
|
|
@ -0,0 +1,18 @@
|
||||||
|
<pluginMetadata>
|
||||||
|
<mojos>
|
||||||
|
<mojo>
|
||||||
|
<goal>test2</goal>
|
||||||
|
<call>test2</call>
|
||||||
|
<parameters>
|
||||||
|
<parameter>
|
||||||
|
<name>testDir</name>
|
||||||
|
<property>testDir</property>
|
||||||
|
<required>true</required>
|
||||||
|
<type>java.lang.String</type>
|
||||||
|
<description>Test directory location.</description>
|
||||||
|
</parameter>
|
||||||
|
</parameters>
|
||||||
|
<description>Runs the default ("test2") goal of the build script.</description>
|
||||||
|
</mojo>
|
||||||
|
</mojos>
|
||||||
|
</pluginMetadata>
|
||||||
Loading…
Reference in New Issue