private MojoDescriptor getMojoDescriptor( String task, MavenSession session, MavenProject project,
String invokedVia, boolean canUsePrefix, boolean isOptionalMojo )
throws BuildFailureException, LifecycleExecutionException, PluginNotFoundException
{
String goal;
Plugin plugin = null;
PluginDescriptor pluginDescriptor = null;
try
{
StringTokenizer tok = new StringTokenizer( task, ":" );
int numTokens = tok.countTokens();
if ( numTokens == 2 )
{
if ( !canUsePrefix )
{
String msg = "Mapped-prefix lookup of mojos are only supported from direct invocation. " +
"Please use specification of the form groupId:artifactId[:version]:goal instead. " +
"(Offending mojo: \'" + task + "\', invoked via: \'" + invokedVia + "\')";
throw new LifecycleExecutionException( msg );
}
String prefix = tok.nextToken();
goal = tok.nextToken();
// Steps for retrieving the plugin model instance:
// 1. request directly from the plugin collector by prefix
pluginDescriptor = pluginManager.getPluginDescriptorForPrefix( prefix );
if ( pluginDescriptor != null )
{
plugin = new Plugin();
plugin.setGroupId( pluginDescriptor.getGroupId() );
plugin.setArtifactId( pluginDescriptor.getArtifactId() );
plugin.setVersion( pluginDescriptor.getVersion() );
}
// 2. search plugins in the current POM
if ( plugin == null )
{
for ( Iterator i = project.getBuildPlugins().iterator(); i.hasNext(); )
{
Plugin buildPlugin = (Plugin) i.next();
PluginDescriptor desc =
verifyPlugin( buildPlugin, project, session.getSettings(), session.getLocalRepository() );
if ( prefix.equals( desc.getGoalPrefix() ) )
{
plugin = buildPlugin;
pluginDescriptor = desc;
break;
}
}
}
// 3. look in the repository via search groups
if ( plugin == null )
{
plugin = pluginManager.getPluginDefinitionForPrefix( prefix, session, project );
}
// 4. default to o.a.m.plugins and maven-<prefix>-plugin
if ( plugin == null )
{
plugin = new Plugin();
plugin.setGroupId( PluginDescriptor.getDefaultPluginGroupId() );
plugin.setArtifactId( PluginDescriptor.getDefaultPluginArtifactId( prefix ) );
}
}
else if ( numTokens == 3 || numTokens == 4 )
{
plugin = new Plugin();
plugin.setGroupId( tok.nextToken() );
plugin.setArtifactId( tok.nextToken() );
if ( numTokens == 4 )
{
plugin.setVersion( tok.nextToken() );
}
goal = tok.nextToken();
}
else
{
String message = "Invalid task '" + task + "': you must specify a valid lifecycle phase, or" +
" a goal in the format plugin:goal or pluginGroupId:pluginArtifactId:pluginVersion:goal";
throw new BuildFailureException( message );
}
if ( plugin.getVersion() == null )
{
for ( Iterator i = project.getBuildPlugins().iterator(); i.hasNext(); )
{
Plugin buildPlugin = (Plugin) i.next();
if ( buildPlugin.getKey().equals( plugin.getKey() ) )
{
plugin = buildPlugin;
break;
}
}