You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@royale.apache.org by jo...@apache.org on 2022/04/18 20:38:57 UTC

[royale-compiler] branch develop updated: royale-maven-plugin: fail build when using --watch compiler option, but requesting goals/phases after compile (such as package or install)

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

joshtynjala pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/royale-compiler.git


The following commit(s) were added to refs/heads/develop by this push:
     new 737c61dd2 royale-maven-plugin: fail build when using --watch compiler option, but requesting goals/phases after compile (such as package or install)
737c61dd2 is described below

commit 737c61dd2e6d18508b24d3cecbd71b6ed6fa556a
Author: Josh Tynjala <jo...@apache.org>
AuthorDate: Mon Apr 18 13:38:30 2022 -0700

    royale-maven-plugin: fail build when using --watch compiler option, but requesting goals/phases after compile (such as package or install)
    
    To avoid confusion about why the build pauses after compile, an error message makes clear that later phases are not allowed when using --watch
---
 .../java/org/apache/royale/maven/BaseMojo.java     | 51 +++++++++++++++++++++-
 1 file changed, 49 insertions(+), 2 deletions(-)

diff --git a/royale-maven-plugin/src/main/java/org/apache/royale/maven/BaseMojo.java b/royale-maven-plugin/src/main/java/org/apache/royale/maven/BaseMojo.java
index 70826b782..79e455246 100644
--- a/royale-maven-plugin/src/main/java/org/apache/royale/maven/BaseMojo.java
+++ b/royale-maven-plugin/src/main/java/org/apache/royale/maven/BaseMojo.java
@@ -19,17 +19,23 @@
 
 package org.apache.royale.maven;
 
-import org.apache.royale.maven.utils.DependencyHelper;
 import org.apache.flex.tools.FlexTool;
 import org.apache.flex.tools.FlexToolGroup;
 import org.apache.flex.tools.FlexToolRegistry;
 import org.apache.maven.artifact.Artifact;
+import org.apache.maven.execution.MavenSession;
+import org.apache.maven.lifecycle.MavenExecutionPlan;
+import org.apache.maven.lifecycle.internal.LifecycleExecutionPlanCalculator;
+import org.apache.maven.lifecycle.internal.LifecycleTaskSegmentCalculator;
+import org.apache.maven.lifecycle.internal.TaskSegment;
 import org.apache.maven.plugin.AbstractMojo;
+import org.apache.maven.plugin.MojoExecution;
 import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.plugins.annotations.Component;
 import org.apache.maven.plugins.annotations.Parameter;
 import org.apache.maven.project.MavenProject;
 import org.apache.maven.project.ProjectDependenciesResolver;
+import org.apache.royale.maven.utils.DependencyHelper;
 import org.apache.velocity.Template;
 import org.apache.velocity.VelocityContext;
 import org.apache.velocity.app.VelocityEngine;
@@ -101,6 +107,15 @@ public abstract class BaseMojo
     @Parameter
     private String additionalCompilerOptions = null;
 
+    @Parameter(defaultValue = "${session}", required = true, readonly = true)
+    private MavenSession session;
+
+    @Component
+    private LifecycleTaskSegmentCalculator lifecycleTaskSegmentCalculator;
+
+    @Component
+    private LifecycleExecutionPlanCalculator lifecycleExecutionPlanCalculator;
+
     @Component
     private ProjectDependenciesResolver projectDependenciesResolver;
 
@@ -385,8 +400,12 @@ public abstract class BaseMojo
     }
 
     protected void handleExitCode(int exitCode) throws MojoExecutionException {
-        // if the --watch compiler option was specified, don't continue
         if(exitCode == 1000) {
+            // if the compiler is watching for file changes, make sure that the
+            // user didn't request any goals or lifecycle phases after compile
+            checkForInvalidGoalOrPhaseAfterCompile();
+            // if the compiler is watching for file changes, pause this thread
+            // to prevent Maven from exiting while the watch thread is running
             try {
                 while(true) {
                     Thread.sleep(60000);
@@ -399,6 +418,34 @@ public abstract class BaseMojo
         }
     }
 
+    protected void checkForInvalidGoalOrPhaseAfterCompile() throws MojoExecutionException {
+        List<TaskSegment> segments = null;
+        try {
+            segments = lifecycleTaskSegmentCalculator.calculateTaskSegments(session);
+        } catch(Exception e) {
+            throw new MojoExecutionException("Failed to calculate session task segments for --watch=true");
+        }
+        for (TaskSegment segment : segments) {
+            List<Object> tasks = segment.getTasks();
+            MavenExecutionPlan plan = null;
+            try {
+                plan = lifecycleExecutionPlanCalculator.calculateExecutionPlan(session, project, tasks, false);
+            } catch(Exception e) {
+                throw new MojoExecutionException("Failed to calculate execution plan for --watch=true");
+            }
+            boolean foundCompile = false;
+            for (MojoExecution exec : plan.getMojoExecutions()) {
+                String phase = exec.getLifecyclePhase();
+                if (foundCompile) {
+                    throw new MojoExecutionException("Additional goals or lifecycle phases after 'compile' are not allowed when using the --watch=true compiler option");
+                }
+                else if ("compile".equals(phase)) {
+                    foundCompile = true;
+                }
+            }
+        }
+    }
+
     protected List<Artifact> getFilteredLibraries(List<Artifact> artifacts) {
         List<Artifact> filteredLibraries = new LinkedList<Artifact>();
         if(artifacts != null) {