From 88f3b941d4c2619bb12e858552ff8484f958dc2f Mon Sep 17 00:00:00 2001 From: John Dennis Casey <jdcasey@apache.org> Date: Wed, 11 Jan 2006 07:57:32 +0000 Subject: [PATCH] [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 --- .../lifecycle/DefaultLifecycleExecutor.java | 236 +++++++++++------- .../mapping/DefaultLifecycleMapping.java | 27 ++ .../maven/lifecycle/mapping/Lifecycle.java | 19 ++ .../lifecycle/mapping/LifecycleMapping.java | 3 + .../resources/META-INF/plexus/components.xml | 3 + 5 files changed, 203 insertions(+), 85 deletions(-) diff --git a/maven-core/src/main/java/org/apache/maven/lifecycle/DefaultLifecycleExecutor.java b/maven-core/src/main/java/org/apache/maven/lifecycle/DefaultLifecycleExecutor.java index 9405579c1f..8625eec53b 100644 --- a/maven-core/src/main/java/org/apache/maven/lifecycle/DefaultLifecycleExecutor.java +++ b/maven-core/src/main/java/org/apache/maven/lifecycle/DefaultLifecycleExecutor.java @@ -95,7 +95,7 @@ public class DefaultLifecycleExecutor private List defaultReports; private Map phaseToLifecycleMap; - + // ---------------------------------------------------------------------- // // ---------------------------------------------------------------------- @@ -375,7 +375,7 @@ public class DefaultLifecycleExecutor try { // 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 ) { @@ -481,7 +481,7 @@ public class DefaultLifecycleExecutor throws LifecycleExecutionException, BuildFailureException, PluginNotFoundException { // 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, project ); } @@ -958,7 +958,9 @@ public class DefaultLifecycleExecutor throws LifecycleExecutionException, BuildFailureException, PluginNotFoundException { Map mappings = findMappingsForLifecycle( session, project, lifecycle ); - + + List optionalMojos = findOptionalMojosForLifecycle( session, project, lifecycle ); + Map lifecycleMappings = new HashMap(); for ( Iterator i = lifecycle.getPhases().iterator(); i.hasNext(); ) @@ -974,7 +976,12 @@ public class DefaultLifecycleExecutor String goal = tok.nextToken().trim(); // 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() ) { @@ -1045,6 +1052,41 @@ public class DefaultLifecycleExecutor 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, ArtifactRepository localRepository ) throws LifecycleExecutionException, PluginNotFoundException @@ -1334,7 +1376,7 @@ public class DefaultLifecycleExecutor } private MojoDescriptor getMojoDescriptor( String task, MavenSession session, MavenProject project, - String invokedVia, boolean canUsePrefix ) + String invokedVia, boolean canUsePrefix, boolean isOptionalMojo ) throws BuildFailureException, LifecycleExecutionException, PluginNotFoundException { String goal; @@ -1342,114 +1384,138 @@ public class DefaultLifecycleExecutor PluginDescriptor pluginDescriptor = null; - StringTokenizer tok = new StringTokenizer( task, ":" ); - int numTokens = tok.countTokens(); - - if ( numTokens == 2 ) + try { - 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. " + - "Please use specification of the form groupId:artifactId[:version]:goal instead. " + - "(Offending mojo: \'" + task + "\', invoked via: \'" + invokedVia + "\')"; - throw new LifecycleExecutionException( msg ); - } + 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(); + 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 ); + // Steps for retrieving the plugin model instance: + // 1. request directly from the plugin collector by prefix + pluginDescriptor = pluginManager.getPluginDescriptorForPrefix( prefix ); - // 2. look in the repository via search groups - if ( pluginDescriptor == null ) - { - plugin = pluginManager.getPluginDefinitionForPrefix( prefix, session, project ); - } - else - { - plugin = new Plugin(); + // 2. look in the repository via search groups + if ( pluginDescriptor == null ) + { + plugin = pluginManager.getPluginDefinitionForPrefix( prefix, session, project ); + } + else + { + plugin = new Plugin(); - plugin.setGroupId( pluginDescriptor.getGroupId() ); - plugin.setArtifactId( pluginDescriptor.getArtifactId() ); - plugin.setVersion( pluginDescriptor.getVersion() ); - } + plugin.setGroupId( pluginDescriptor.getGroupId() ); + plugin.setArtifactId( pluginDescriptor.getArtifactId() ); + 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(); ) { Plugin buildPlugin = (Plugin) i.next(); - PluginDescriptor desc = - verifyPlugin( buildPlugin, project, session.getSettings(), session.getLocalRepository() ); - if ( prefix.equals( desc.getGoalPrefix() ) ) + if ( buildPlugin.getKey().equals( plugin.getKey() ) ) { plugin = buildPlugin; + break; } } } - - // 4. default to o.a.m.plugins and maven-<prefix>-plugin - if ( plugin == null ) + else if ( numTokens == 3 || numTokens == 4 ) { 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(); ) - { - Plugin buildPlugin = (Plugin) i.next(); + project.injectPluginManagementInfo( plugin ); - 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; - break; + getLogger().info( "Skipping missing optional mojo: " + task ); + } + else + { + throw new BuildFailureException( "Required goal not found: " + task ); } } + + return mojoDescriptor; } - else if ( numTokens == 3 || numTokens == 4 ) + catch ( PluginNotFoundException e ) { - plugin = new Plugin(); - - plugin.setGroupId( tok.nextToken() ); - plugin.setArtifactId( tok.nextToken() ); - - if ( numTokens == 4 ) + if ( isOptionalMojo ) { - 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 ); - - 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; + + return null; } protected void line() diff --git a/maven-core/src/main/java/org/apache/maven/lifecycle/mapping/DefaultLifecycleMapping.java b/maven-core/src/main/java/org/apache/maven/lifecycle/mapping/DefaultLifecycleMapping.java index 098385dfcf..ade7d3f0ef 100644 --- a/maven-core/src/main/java/org/apache/maven/lifecycle/mapping/DefaultLifecycleMapping.java +++ b/maven-core/src/main/java/org/apache/maven/lifecycle/mapping/DefaultLifecycleMapping.java @@ -36,6 +36,33 @@ public class DefaultLifecycleMapping /** @deprecated use lifecycles instead */ 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 ) { diff --git a/maven-core/src/main/java/org/apache/maven/lifecycle/mapping/Lifecycle.java b/maven-core/src/main/java/org/apache/maven/lifecycle/mapping/Lifecycle.java index f05b4b49aa..f342ecb3f1 100644 --- a/maven-core/src/main/java/org/apache/maven/lifecycle/mapping/Lifecycle.java +++ b/maven-core/src/main/java/org/apache/maven/lifecycle/mapping/Lifecycle.java @@ -16,6 +16,8 @@ package org.apache.maven.lifecycle.mapping; * limitations under the License. */ +import java.util.ArrayList; +import java.util.List; import java.util.Map; /** @@ -32,6 +34,8 @@ public class Lifecycle * Field phases */ private Map phases; + + private List optionalMojos = new ArrayList(); /** * Method getId @@ -58,4 +62,19 @@ public class Lifecycle { this.id = id; } //-- 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; + } } diff --git a/maven-core/src/main/java/org/apache/maven/lifecycle/mapping/LifecycleMapping.java b/maven-core/src/main/java/org/apache/maven/lifecycle/mapping/LifecycleMapping.java index 4bc1457f9d..286cb3a847 100644 --- a/maven-core/src/main/java/org/apache/maven/lifecycle/mapping/LifecycleMapping.java +++ b/maven-core/src/main/java/org/apache/maven/lifecycle/mapping/LifecycleMapping.java @@ -16,6 +16,7 @@ package org.apache.maven.lifecycle.mapping; * limitations under the License. */ +import java.util.List; import java.util.Map; /** @@ -26,5 +27,7 @@ public interface LifecycleMapping { String ROLE = LifecycleMapping.class.getName(); + List getOptionalMojos( String lifecycle ); + Map getPhases( String lifecycle ); } diff --git a/maven-core/src/main/resources/META-INF/plexus/components.xml b/maven-core/src/main/resources/META-INF/plexus/components.xml index b90ee2eed0..d63c646ae2 100644 --- a/maven-core/src/main/resources/META-INF/plexus/components.xml +++ b/maven-core/src/main/resources/META-INF/plexus/components.xml @@ -306,6 +306,9 @@ <install>org.apache.maven.plugins:maven-install-plugin:install</install> <deploy>org.apache.maven.plugins:maven-deploy-plugin:deploy</deploy> </phases> + <optional-mojos> + <optional-mojo>org.apache.maven.plugins:maven-site-plugin:attach-descriptor</optional-mojo> + </optional-mojos> <!-- END SNIPPET: pom-lifecycle --> </lifecycle> </lifecycles>