[MNG-1903] Adding support for optional mojos within a lifecycle mapping.

git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@367962 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
John Dennis Casey 2006-01-11 07:57:32 +00:00
parent 98a4dc3cb3
commit 88f3b941d4
5 changed files with 203 additions and 85 deletions

View File

@ -375,7 +375,7 @@ public class DefaultLifecycleExecutor
try try
{ {
// definitely a CLI goal, can use prefix // definitely a CLI goal, can use prefix
mojo = getMojoDescriptor( task, session, project, task, true ); mojo = getMojoDescriptor( task, session, project, task, true, false );
} }
catch ( PluginNotFoundException e ) catch ( PluginNotFoundException e )
{ {
@ -481,7 +481,7 @@ public class DefaultLifecycleExecutor
throws LifecycleExecutionException, BuildFailureException, PluginNotFoundException throws LifecycleExecutionException, BuildFailureException, PluginNotFoundException
{ {
// guaranteed to come from the CLI and not be part of a phase // guaranteed to come from the CLI and not be part of a phase
MojoDescriptor mojoDescriptor = getMojoDescriptor( task, session, project, task, true ); MojoDescriptor mojoDescriptor = getMojoDescriptor( task, session, project, task, true, false );
executeGoals( Collections.singletonList( new MojoExecution( mojoDescriptor ) ), forkEntryPoints, session, executeGoals( Collections.singletonList( new MojoExecution( mojoDescriptor ) ), forkEntryPoints, session,
project ); project );
} }
@ -959,6 +959,8 @@ public class DefaultLifecycleExecutor
{ {
Map mappings = findMappingsForLifecycle( session, project, lifecycle ); Map mappings = findMappingsForLifecycle( session, project, lifecycle );
List optionalMojos = findOptionalMojosForLifecycle( session, project, lifecycle );
Map lifecycleMappings = new HashMap(); Map lifecycleMappings = new HashMap();
for ( Iterator i = lifecycle.getPhases().iterator(); i.hasNext(); ) for ( Iterator i = lifecycle.getPhases().iterator(); i.hasNext(); )
@ -974,7 +976,12 @@ public class DefaultLifecycleExecutor
String goal = tok.nextToken().trim(); String goal = tok.nextToken().trim();
// Not from the CLI, don't use prefix // Not from the CLI, don't use prefix
MojoDescriptor mojoDescriptor = getMojoDescriptor( goal, session, project, selectedPhase, false ); MojoDescriptor mojoDescriptor = getMojoDescriptor( goal, session, project, selectedPhase, false, optionalMojos.contains( goal ) );
if ( mojoDescriptor == null )
{
continue;
}
if ( mojoDescriptor.isDirectInvocationOnly() ) if ( mojoDescriptor.isDirectInvocationOnly() )
{ {
@ -1045,6 +1052,41 @@ public class DefaultLifecycleExecutor
return mappings; return mappings;
} }
private List findOptionalMojosForLifecycle( MavenSession session, MavenProject project, Lifecycle lifecycle )
throws LifecycleExecutionException, PluginNotFoundException
{
String packaging = project.getPackaging();
List optionalMojos = null;
LifecycleMapping m = (LifecycleMapping) findExtension( project, LifecycleMapping.ROLE, packaging, session
.getSettings(), session.getLocalRepository() );
if ( m != null )
{
optionalMojos = m.getOptionalMojos( lifecycle.getId() );
}
if ( optionalMojos == null )
{
try
{
m = (LifecycleMapping) session.lookup( LifecycleMapping.ROLE, packaging );
optionalMojos = m.getOptionalMojos( lifecycle.getId() );
}
catch ( ComponentLookupException e )
{
getLogger().debug( "Error looking up lifecycle mapping to retrieve optional mojos. Lifecycle ID: " + lifecycle.getId() + ". Error: " + e.getMessage(), e );
}
}
if ( optionalMojos == null )
{
optionalMojos = Collections.EMPTY_LIST;
}
return optionalMojos;
}
private Object findExtension( MavenProject project, String role, String roleHint, Settings settings, private Object findExtension( MavenProject project, String role, String roleHint, Settings settings,
ArtifactRepository localRepository ) ArtifactRepository localRepository )
throws LifecycleExecutionException, PluginNotFoundException throws LifecycleExecutionException, PluginNotFoundException
@ -1334,7 +1376,7 @@ public class DefaultLifecycleExecutor
} }
private MojoDescriptor getMojoDescriptor( String task, MavenSession session, MavenProject project, private MojoDescriptor getMojoDescriptor( String task, MavenSession session, MavenProject project,
String invokedVia, boolean canUsePrefix ) String invokedVia, boolean canUsePrefix, boolean isOptionalMojo )
throws BuildFailureException, LifecycleExecutionException, PluginNotFoundException throws BuildFailureException, LifecycleExecutionException, PluginNotFoundException
{ {
String goal; String goal;
@ -1342,114 +1384,138 @@ public class DefaultLifecycleExecutor
PluginDescriptor pluginDescriptor = null; PluginDescriptor pluginDescriptor = null;
StringTokenizer tok = new StringTokenizer( task, ":" ); try
int numTokens = tok.countTokens();
if ( numTokens == 2 )
{ {
if ( !canUsePrefix ) StringTokenizer tok = new StringTokenizer( task, ":" );
int numTokens = tok.countTokens();
if ( numTokens == 2 )
{ {
String msg = "Mapped-prefix lookup of mojos are only supported from direct invocation. " + if ( !canUsePrefix )
"Please use specification of the form groupId:artifactId[:version]:goal instead. " + {
"(Offending mojo: \'" + task + "\', invoked via: \'" + invokedVia + "\')"; String msg = "Mapped-prefix lookup of mojos are only supported from direct invocation. "
throw new LifecycleExecutionException( msg ); + "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(); String prefix = tok.nextToken();
goal = tok.nextToken(); goal = tok.nextToken();
// Steps for retrieving the plugin model instance: // Steps for retrieving the plugin model instance:
// 1. request directly from the plugin collector by prefix // 1. request directly from the plugin collector by prefix
pluginDescriptor = pluginManager.getPluginDescriptorForPrefix( prefix ); pluginDescriptor = pluginManager.getPluginDescriptorForPrefix( prefix );
// 2. look in the repository via search groups // 2. look in the repository via search groups
if ( pluginDescriptor == null ) if ( pluginDescriptor == null )
{ {
plugin = pluginManager.getPluginDefinitionForPrefix( prefix, session, project ); plugin = pluginManager.getPluginDefinitionForPrefix( prefix, session, project );
} }
else else
{ {
plugin = new Plugin(); plugin = new Plugin();
plugin.setGroupId( pluginDescriptor.getGroupId() ); plugin.setGroupId( pluginDescriptor.getGroupId() );
plugin.setArtifactId( pluginDescriptor.getArtifactId() ); plugin.setArtifactId( pluginDescriptor.getArtifactId() );
plugin.setVersion( pluginDescriptor.getVersion() ); plugin.setVersion( pluginDescriptor.getVersion() );
} }
// 3. 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;
}
}
}
// 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 ) );
}
// 3. search plugins in the current POM
if ( plugin == null )
{
for ( Iterator i = project.getBuildPlugins().iterator(); i.hasNext(); ) for ( Iterator i = project.getBuildPlugins().iterator(); i.hasNext(); )
{ {
Plugin buildPlugin = (Plugin) i.next(); Plugin buildPlugin = (Plugin) i.next();
PluginDescriptor desc = if ( buildPlugin.getKey().equals( plugin.getKey() ) )
verifyPlugin( buildPlugin, project, session.getSettings(), session.getLocalRepository() );
if ( prefix.equals( desc.getGoalPrefix() ) )
{ {
plugin = buildPlugin; plugin = buildPlugin;
break;
} }
} }
} }
else if ( numTokens == 3 || numTokens == 4 )
// 4. default to o.a.m.plugins and maven-<prefix>-plugin
if ( plugin == null )
{ {
plugin = new Plugin(); plugin = new Plugin();
plugin.setGroupId( PluginDescriptor.getDefaultPluginGroupId() );
plugin.setArtifactId( PluginDescriptor.getDefaultPluginArtifactId( prefix ) ); 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 );
} }
for ( Iterator i = project.getBuildPlugins().iterator(); i.hasNext(); ) project.injectPluginManagementInfo( plugin );
{
Plugin buildPlugin = (Plugin) i.next();
if ( buildPlugin.getKey().equals( plugin.getKey() ) ) if ( pluginDescriptor == null )
{
pluginDescriptor = verifyPlugin( plugin, project, session.getSettings(), session.getLocalRepository() );
}
// this has been simplified from the old code that injected the plugin management stuff, since
// pluginManagement injection is now handled by the project method.
project.addPlugin( plugin );
MojoDescriptor mojoDescriptor = pluginDescriptor.getMojo( goal );
if ( mojoDescriptor == null )
{
if ( isOptionalMojo )
{ {
plugin = buildPlugin; getLogger().info( "Skipping missing optional mojo: " + task );
break; }
else
{
throw new BuildFailureException( "Required goal not found: " + task );
} }
} }
return mojoDescriptor;
} }
else if ( numTokens == 3 || numTokens == 4 ) catch ( PluginNotFoundException e )
{ {
plugin = new Plugin(); if ( isOptionalMojo )
plugin.setGroupId( tok.nextToken() );
plugin.setArtifactId( tok.nextToken() );
if ( numTokens == 4 )
{ {
plugin.setVersion( tok.nextToken() ); getLogger().info( "Skipping missing optional mojo: " + task );
getLogger().debug( "Mojo: " + task + " could not be found. Reason: " + e.getMessage(), e );
}
else
{
throw e;
} }
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 );
} }
project.injectPluginManagementInfo( plugin ); return null;
if ( pluginDescriptor == null )
{
pluginDescriptor = verifyPlugin( plugin, project, session.getSettings(), session.getLocalRepository() );
}
// this has been simplified from the old code that injected the plugin management stuff, since
// pluginManagement injection is now handled by the project method.
project.addPlugin( plugin );
MojoDescriptor mojoDescriptor = pluginDescriptor.getMojo( goal );
if ( mojoDescriptor == null )
{
throw new BuildFailureException( "Required goal not found: " + task );
}
return mojoDescriptor;
} }
protected void line() protected void line()

View File

@ -37,6 +37,33 @@ public class DefaultLifecycleMapping
/** @deprecated use lifecycles instead */ /** @deprecated use lifecycles instead */
private Map phases; private Map phases;
public List getOptionalMojos( String lifecycle )
{
if ( lifecycleMap == null )
{
lifecycleMap = new HashMap();
if ( lifecycles != null )
{
for ( Iterator i = lifecycles.iterator(); i.hasNext(); )
{
Lifecycle l = (Lifecycle) i.next();
lifecycleMap.put( l.getId(), l );
}
}
}
Lifecycle l = (Lifecycle) lifecycleMap.get( lifecycle );
if ( l != null )
{
return l.getOptionalMojos();
}
else
{
return null;
}
}
public Map getPhases( String lifecycle ) public Map getPhases( String lifecycle )
{ {
if ( lifecycleMap == null ) if ( lifecycleMap == null )

View File

@ -16,6 +16,8 @@ package org.apache.maven.lifecycle.mapping;
* limitations under the License. * limitations under the License.
*/ */
import java.util.ArrayList;
import java.util.List;
import java.util.Map; import java.util.Map;
/** /**
@ -33,6 +35,8 @@ public class Lifecycle
*/ */
private Map phases; private Map phases;
private List optionalMojos = new ArrayList();
/** /**
* Method getId * Method getId
*/ */
@ -58,4 +62,19 @@ public class Lifecycle
{ {
this.id = id; this.id = id;
} //-- void setId(String) } //-- void setId(String)
public void addOptionalMojo( String optionalMojo )
{
this.optionalMojos.add( optionalMojo );
}
public void setOptionalMojos( List optionalMojos )
{
this.optionalMojos = optionalMojos;
}
public List getOptionalMojos()
{
return this.optionalMojos;
}
} }

View File

@ -16,6 +16,7 @@ package org.apache.maven.lifecycle.mapping;
* limitations under the License. * limitations under the License.
*/ */
import java.util.List;
import java.util.Map; import java.util.Map;
/** /**
@ -26,5 +27,7 @@ public interface LifecycleMapping
{ {
String ROLE = LifecycleMapping.class.getName(); String ROLE = LifecycleMapping.class.getName();
List getOptionalMojos( String lifecycle );
Map getPhases( String lifecycle ); Map getPhases( String lifecycle );
} }

View File

@ -306,6 +306,9 @@
<install>org.apache.maven.plugins:maven-install-plugin:install</install> <install>org.apache.maven.plugins:maven-install-plugin:install</install>
<deploy>org.apache.maven.plugins:maven-deploy-plugin:deploy</deploy> <deploy>org.apache.maven.plugins:maven-deploy-plugin:deploy</deploy>
</phases> </phases>
<optional-mojos>
<optional-mojo>org.apache.maven.plugins:maven-site-plugin:attach-descriptor</optional-mojo>
</optional-mojos>
<!-- END SNIPPET: pom-lifecycle --> <!-- END SNIPPET: pom-lifecycle -->
</lifecycle> </lifecycle>
</lifecycles> </lifecycles>