mirror of https://github.com/apache/maven.git
[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:
parent
98a4dc3cb3
commit
88f3b941d4
|
@ -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,6 +1384,8 @@ public class DefaultLifecycleExecutor
|
||||||
|
|
||||||
PluginDescriptor pluginDescriptor = null;
|
PluginDescriptor pluginDescriptor = null;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
StringTokenizer tok = new StringTokenizer( task, ":" );
|
StringTokenizer tok = new StringTokenizer( task, ":" );
|
||||||
int numTokens = tok.countTokens();
|
int numTokens = tok.countTokens();
|
||||||
|
|
||||||
|
@ -1349,9 +1393,9 @@ public class DefaultLifecycleExecutor
|
||||||
{
|
{
|
||||||
if ( !canUsePrefix )
|
if ( !canUsePrefix )
|
||||||
{
|
{
|
||||||
String msg = "Mapped-prefix lookup of mojos are only supported from direct invocation. " +
|
String msg = "Mapped-prefix lookup of mojos are only supported from direct invocation. "
|
||||||
"Please use specification of the form groupId:artifactId[:version]:goal instead. " +
|
+ "Please use specification of the form groupId:artifactId[:version]:goal instead. "
|
||||||
"(Offending mojo: \'" + task + "\', invoked via: \'" + invokedVia + "\')";
|
+ "(Offending mojo: \'" + task + "\', invoked via: \'" + invokedVia + "\')";
|
||||||
throw new LifecycleExecutionException( msg );
|
throw new LifecycleExecutionException( msg );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1383,8 +1427,8 @@ public class DefaultLifecycleExecutor
|
||||||
{
|
{
|
||||||
Plugin buildPlugin = (Plugin) i.next();
|
Plugin buildPlugin = (Plugin) i.next();
|
||||||
|
|
||||||
PluginDescriptor desc =
|
PluginDescriptor desc = verifyPlugin( buildPlugin, project, session.getSettings(), session
|
||||||
verifyPlugin( buildPlugin, project, session.getSettings(), session.getLocalRepository() );
|
.getLocalRepository() );
|
||||||
if ( prefix.equals( desc.getGoalPrefix() ) )
|
if ( prefix.equals( desc.getGoalPrefix() ) )
|
||||||
{
|
{
|
||||||
plugin = buildPlugin;
|
plugin = buildPlugin;
|
||||||
|
@ -1427,8 +1471,8 @@ public class DefaultLifecycleExecutor
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
String message = "Invalid task '" + task + "': you must specify a valid lifecycle phase, or" +
|
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";
|
+ " a goal in the format plugin:goal or pluginGroupId:pluginArtifactId:pluginVersion:goal";
|
||||||
throw new BuildFailureException( message );
|
throw new BuildFailureException( message );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1445,12 +1489,34 @@ public class DefaultLifecycleExecutor
|
||||||
|
|
||||||
MojoDescriptor mojoDescriptor = pluginDescriptor.getMojo( goal );
|
MojoDescriptor mojoDescriptor = pluginDescriptor.getMojo( goal );
|
||||||
if ( mojoDescriptor == null )
|
if ( mojoDescriptor == null )
|
||||||
|
{
|
||||||
|
if ( isOptionalMojo )
|
||||||
|
{
|
||||||
|
getLogger().info( "Skipping missing optional mojo: " + task );
|
||||||
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
throw new BuildFailureException( "Required goal not found: " + task );
|
throw new BuildFailureException( "Required goal not found: " + task );
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return mojoDescriptor;
|
return mojoDescriptor;
|
||||||
}
|
}
|
||||||
|
catch ( PluginNotFoundException e )
|
||||||
|
{
|
||||||
|
if ( isOptionalMojo )
|
||||||
|
{
|
||||||
|
getLogger().info( "Skipping missing optional mojo: " + task );
|
||||||
|
getLogger().debug( "Mojo: " + task + " could not be found. Reason: " + e.getMessage(), e );
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
protected void line()
|
protected void line()
|
||||||
{
|
{
|
||||||
|
|
|
@ -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 )
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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 );
|
||||||
}
|
}
|
||||||
|
|
|
@ -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>
|
||||||
|
|
Loading…
Reference in New Issue