You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by st...@apache.org on 2019/11/21 20:25:58 UTC

[maven] 04/04: [MNG-5668] Add error reporting if the user tries to invoke a dynamic phase directly

This is an automated email from the ASF dual-hosted git repository.

stephenc pushed a commit to branch mng-5668-poc
in repository https://gitbox.apache.org/repos/asf/maven.git

commit 016a7d61e383e4b8341aa9bbddb8416e04ab57d3
Author: Stephen Connolly <st...@gmail.com>
AuthorDate: Thu Nov 21 20:25:07 2019 +0000

    [MNG-5668] Add error reporting if the user tries to invoke a dynamic phase directly
---
 .../DefaultLifecycleTaskSegmentCalculator.java     | 48 ++++++++++++++++------
 .../internal/LifecycleTaskSegmentCalculator.java   |  2 +-
 .../maven/lifecycle/internal/MojoExecutor.java     |  3 +-
 .../apache/maven/lifecycle/internal/PhaseId.java   |  6 +--
 maven-plugin-api/src/main/mdo/lifecycle.mdo        |  2 +-
 5 files changed, 43 insertions(+), 18 deletions(-)

diff --git a/maven-core/src/main/java/org/apache/maven/lifecycle/internal/DefaultLifecycleTaskSegmentCalculator.java b/maven-core/src/main/java/org/apache/maven/lifecycle/internal/DefaultLifecycleTaskSegmentCalculator.java
index cb49050..c10cbf0 100644
--- a/maven-core/src/main/java/org/apache/maven/lifecycle/internal/DefaultLifecycleTaskSegmentCalculator.java
+++ b/maven-core/src/main/java/org/apache/maven/lifecycle/internal/DefaultLifecycleTaskSegmentCalculator.java
@@ -86,7 +86,7 @@ public class DefaultLifecycleTaskSegmentCalculator
     public List<TaskSegment> calculateTaskSegments( MavenSession session, List<String> tasks )
         throws PluginNotFoundException, PluginResolutionException, PluginDescriptorParsingException,
         MojoNotFoundException, NoPluginFoundForPrefixException, InvalidPluginDescriptorException,
-        PluginVersionResolutionException
+        PluginVersionResolutionException, LifecyclePhaseNotFoundException, LifecycleNotFoundException
     {
         List<TaskSegment> taskSegments = new ArrayList<>( tasks.size() );
 
@@ -94,24 +94,48 @@ public class DefaultLifecycleTaskSegmentCalculator
 
         for ( String task : tasks )
         {
+            PhaseId phaseId = PhaseId.of( task );
+            // if the priority is non-zero then you specified the priority on the CLI
+            if ( phaseId.priority() != 0 )
+            {
+                throw new LifecyclePhaseNotFoundException(
+                    "Dynamic phases such as \"" + task + "\" are only permitted as execution targets specified "
+                        + "inside the pom.xml. Try invoking the whole phase, i.e. \"" + phaseId.phase() + "\".", task );
+            }
             if ( isGoalSpecification( task ) )
             {
-                // "pluginPrefix:goal" or "groupId:artifactId[:version]:goal"
+                try
+                {
+                    // "pluginPrefix:goal" or "groupId:artifactId[:version]:goal"
 
-                lifecyclePluginResolver.resolveMissingPluginVersions( session.getTopLevelProject(), session );
+                    lifecyclePluginResolver.resolveMissingPluginVersions( session.getTopLevelProject(), session );
 
-                MojoDescriptor mojoDescriptor =
-                    mojoDescriptorCreator.getMojoDescriptor( task, session, session.getTopLevelProject() );
+                    MojoDescriptor mojoDescriptor =
+                        mojoDescriptorCreator.getMojoDescriptor( task, session, session.getTopLevelProject() );
 
-                boolean aggregating = mojoDescriptor.isAggregator() || !mojoDescriptor.isProjectRequired();
+                    boolean aggregating = mojoDescriptor.isAggregator() || !mojoDescriptor.isProjectRequired();
 
-                if ( currentSegment == null || currentSegment.isAggregating() != aggregating )
+                    if ( currentSegment == null || currentSegment.isAggregating() != aggregating )
+                    {
+                        currentSegment = new TaskSegment( aggregating );
+                        taskSegments.add( currentSegment );
+                    }
+
+                    currentSegment.getTasks().add( new GoalTask( task ) );
+                }
+                catch ( NoPluginFoundForPrefixException e )
                 {
-                    currentSegment = new TaskSegment( aggregating );
-                    taskSegments.add( currentSegment );
+                    if ( phaseId.executionPoint() != PhaseExecutionPoint.AS && phaseId.phase().indexOf( ':' ) == -1 )
+                    {
+                        LifecyclePhaseNotFoundException lpnfe = new LifecyclePhaseNotFoundException(
+                            "Dynamic phases such as \"" + task + "\" are only permitted as execution targets specified "
+                                + "inside the pom.xml. Try invoking the whole phase, i.e. \"" + phaseId.phase() + "\".",
+                            task );
+                        lpnfe.addSuppressed( e );
+                        throw lpnfe;
+                    }
+                    throw e;
                 }
-
-                currentSegment.getTasks().add( new GoalTask( task ) );
             }
             else
             {
@@ -152,4 +176,4 @@ public class DefaultLifecycleTaskSegmentCalculator
         return task.indexOf( ':' ) >= 0;
     }
 
-}
\ No newline at end of file
+}
diff --git a/maven-core/src/main/java/org/apache/maven/lifecycle/internal/LifecycleTaskSegmentCalculator.java b/maven-core/src/main/java/org/apache/maven/lifecycle/internal/LifecycleTaskSegmentCalculator.java
index 7dd84d8..7c7db5a 100644
--- a/maven-core/src/main/java/org/apache/maven/lifecycle/internal/LifecycleTaskSegmentCalculator.java
+++ b/maven-core/src/main/java/org/apache/maven/lifecycle/internal/LifecycleTaskSegmentCalculator.java
@@ -54,7 +54,7 @@ public interface LifecycleTaskSegmentCalculator
     List<TaskSegment> calculateTaskSegments( MavenSession session, List<String> tasks )
         throws PluginNotFoundException, PluginResolutionException, PluginDescriptorParsingException,
         MojoNotFoundException, NoPluginFoundForPrefixException, InvalidPluginDescriptorException,
-        PluginVersionResolutionException;
+        PluginVersionResolutionException, LifecyclePhaseNotFoundException, LifecycleNotFoundException;
 
     boolean requiresProject( MavenSession session );
 
diff --git a/maven-core/src/main/java/org/apache/maven/lifecycle/internal/MojoExecutor.java b/maven-core/src/main/java/org/apache/maven/lifecycle/internal/MojoExecutor.java
index df3f34c..ae5b03f 100644
--- a/maven-core/src/main/java/org/apache/maven/lifecycle/internal/MojoExecutor.java
+++ b/maven-core/src/main/java/org/apache/maven/lifecycle/internal/MojoExecutor.java
@@ -251,7 +251,8 @@ public class MojoExecutor
             {
                 pluginManager.executeMojo( session, mojoExecution );
             }
-            catch ( MojoFailureException | PluginManagerException | PluginConfigurationException | MojoExecutionException e )
+            catch ( MojoFailureException | PluginManagerException | PluginConfigurationException
+                | MojoExecutionException e )
             {
                 throw new LifecycleExecutionException( mojoExecution, session.getCurrentProject(), e );
             }
diff --git a/maven-core/src/main/java/org/apache/maven/lifecycle/internal/PhaseId.java b/maven-core/src/main/java/org/apache/maven/lifecycle/internal/PhaseId.java
index a225541..da5bece 100644
--- a/maven-core/src/main/java/org/apache/maven/lifecycle/internal/PhaseId.java
+++ b/maven-core/src/main/java/org/apache/maven/lifecycle/internal/PhaseId.java
@@ -30,7 +30,7 @@ public class PhaseId
     /**
      * Interned {@link PhaseId} instances.
      */
-    private static final Map<String, PhaseId> instances = new WeakHashMap<>();
+    private static final Map<String, PhaseId> INSTANCES = new WeakHashMap<>();
 
     /**
      * The execution point of this {@link PhaseId}.
@@ -55,11 +55,11 @@ public class PhaseId
      */
     public static synchronized PhaseId of( String phase )
     {
-        PhaseId result = instances.get( phase );
+        PhaseId result = INSTANCES.get( phase );
         if ( result == null )
         {
             result = new PhaseId( phase );
-            instances.put( phase, result );
+            INSTANCES.put( phase, result );
         }
         return result;
     }
diff --git a/maven-plugin-api/src/main/mdo/lifecycle.mdo b/maven-plugin-api/src/main/mdo/lifecycle.mdo
index 8c6922f..9418758 100644
--- a/maven-plugin-api/src/main/mdo/lifecycle.mdo
+++ b/maven-plugin-api/src/main/mdo/lifecycle.mdo
@@ -137,7 +137,7 @@ under the License.
      * Get the effective ID of this phase, e.g.,
      * <code>generate-sources</code> or <code>after:integration-test[1000]</code>.
      *
-     * @return String
+     * @return the effective ID of this phase
      */
     public String getId()
     {