title: How to fork a Maven lifecycle (in the proper sense) from a plugin?
body: <p><strong>General Problem:</strong>  I'm testing a web application at a large company with a service oriented architecture.  External services often fail in our test environment due to background noise.  This prevents integration tests for our service from running properly since our service won't work unless calls to these external services are succeeding.  For this reason we'd like the ability to mock responses from external services so that we don't have to depend on them and can test our own service in isolation.</p>
  
  <p>There's a tool for this called <a href="https://github.com/clafonta/Mockey" rel="nofollow">Mockey</a> which we are hoping to use.  It's a Java program that runs through an embedded Jetty server and acts as a proxy for service calls.  Our web application is re-configured to call Mockey instead of the external services.  Mockey is then configured to provide dynamically mocked responses to these calls depending on the URL and header data that get passed in.</p>
  
  <p>In order to utilize this tool we'd like the ability to start Mockey during the pre-integration-test phase of a Maven lifecycle so that it will be available for use during the integration-test phase.</p>
  
  <p><strong>Specific Problem:</strong>  In order to start and shutdown Mockey during the pre-integration-test and post-integration-test phases of the Maven lifecycle I've written a Maven 3 plugin called mockey-maven-plugin:</p>
  
  <p>The mockey-maven-plugin pom.xml file:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;project xmlns="http://maven.apache.org/POM/4.0.0"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  
      &lt;groupId&gt;com.mycompany.mockey&lt;/groupId&gt;
      &lt;artifactId&gt;mockey-maven-plugin&lt;/artifactId&gt;
      &lt;packaging&gt;maven-plugin&lt;/packaging&gt;
      &lt;version&gt;1.3&lt;/version&gt;
  
      &lt;dependencies&gt;
  
          &lt;!-- Maven plugin dependencies --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.apache.maven&lt;/groupId&gt;
              &lt;artifactId&gt;maven-plugin-api&lt;/artifactId&gt;
              &lt;version&gt;3.2.5&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.apache.maven.plugin-tools&lt;/groupId&gt;
              &lt;artifactId&gt;maven-plugin-annotations&lt;/artifactId&gt;
              &lt;version&gt;3.4&lt;/version&gt;
              &lt;scope&gt;provided&lt;/scope&gt;
          &lt;/dependency&gt;
  
          &lt;!-- Mockey dependency --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;com.mycompany.mockey&lt;/groupId&gt;
              &lt;artifactId&gt;Mockey&lt;/artifactId&gt;
              &lt;version&gt;1.16.2015&lt;/version&gt;
          &lt;/dependency&gt;
  
      &lt;/dependencies&gt;
  
      &lt;build&gt;
  
          &lt;plugins&gt;
  
              &lt;!-- This plugin is used to generate a plugin descriptor
                   xml file which will be packaged with the plugin --&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                  &lt;artifactId&gt;maven-plugin-plugin&lt;/artifactId&gt;
                  &lt;version&gt;3.4&lt;/version&gt;
              &lt;/plugin&gt;
  
          &lt;/plugins&gt;
  
      &lt;/build&gt;
  
  &lt;/project&gt;
  </code></pre>
  
  <p>The mockey-maven-plugin StartMockey class:</p>
  
  <pre class="lang-java prettyprint-override"><code>@Mojo(name="start-mockey")
  @Execute(phase= LifecyclePhase.PACKAGE) // Not sure about this annotation
  public class StartMockey extends AbstractMojo
  {
      /**
       * Flag which controls Mockey startup.
       */
      @Parameter(property="mockey.skipStartup", defaultValue="false", required=true)
      private Boolean skipStartup;
  
      // Do I need these getters and setters or does Maven ignore them?
  
      public Boolean getSkipStartup()
      {
          return skipStartup;
      }
  
      public void setSkipStartup(Boolean skipStartup)
      {
          this.skipStartup = skipStartup;
      }
  
      // *SNIP* Defining Mockey parameters...
  
      // Maven will call this method to start the mockey-maven-plugin
      public void execute()
      {
          if(skipStartup)
          {
              getLog().info("Skipping Mockey startup");
              return;
          }
  
          getLog().info("Starting Mockey");
  
          // Load specified parameters into array
          List&lt;String&gt; argsList = new ArrayList&lt;&gt;();
  
          // *SNIP* Adding Mockey parameters to argList...
  
          String[] args = new String[argsList.size()];
          argsList.toArray(args);
  
          // Start Mockey with specified parameters and wait for it to return
          try
          {
              JettyRunner.main(args);
          }
          catch(Exception e)
          {
              getLog().error("Mockey died... :(");
          }
          getLog().info("mockey-maven-plugin now exiting");
      }
  }
  </code></pre>
  
  <p>The mockey-maven-plugin ShutdownMockey class:</p>
  
  <pre class="lang-java prettyprint-override"><code>@Mojo(name="shutdown-mockey")
  public class ShutdownMockey extends AbstractMojo
  {
      /**
       * Flag which controls Mockey shutdown.
       */
      @Parameter(property="mockey.skipShutdown")
      private Boolean skipShutdown;
  
      // Again, Do I need these getters and setters or does Maven ignore them?
  
      public Boolean getSkipShutdown()
      {
          return skipShutdown;
      }
  
      public void setSkipShutdown(Boolean skipShutdown)
      {
          this.skipShutdown = skipShutdown;
      }
  
      public void execute()
      {
          if(skipShutdown)
          {
              getLog().info("Skipping Mockey shutdown");
              return;
          }
          getLog().info("Shutting down Mockey");
          JettyRunner.stopServer();
          getLog().info("mockey-maven-plugin now exiting");
      }
  }
  </code></pre>
  
  <p>Plugin entry for mockey-maven-plugin in my team project's pom.xml file:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;plugin&gt;
      &lt;groupId&gt;com.mycompany.mockey&lt;/groupId&gt;
      &lt;artifactId&gt;mockey-maven-plugin&lt;/artifactId&gt;
      &lt;version&gt;1.3&lt;/version&gt;
      &lt;configuration&gt;
          &lt;skipShutdown&gt;${keepMockeyRunning}&lt;/skipShutdown&gt;
          &lt;skipStartup&gt;${skipMockey}&lt;/skipStartup&gt;
  
          &lt;!-- *SNIP* Other Mockey parameters... --&gt;
  
      &lt;/configuration&gt;
      &lt;executions&gt;
          &lt;execution&gt;
              &lt;id&gt;start-mockey&lt;/id&gt;
              &lt;goals&gt;
                  &lt;goal&gt;start-mockey&lt;/goal&gt;
              &lt;/goals&gt;
              &lt;phase&gt;pre-integration-test&lt;/phase&gt;
          &lt;/execution&gt;
          &lt;execution&gt;
              &lt;id&gt;shutdown-mockey&lt;/id&gt;
              &lt;goals&gt;
                  &lt;goal&gt;shutdown-mockey&lt;/goal&gt;
              &lt;/goals&gt;
              &lt;phase&gt;post-integration-test&lt;/phase&gt;
          &lt;/execution&gt;
      &lt;/executions&gt;
  &lt;/plugin&gt;
  </code></pre>
  
  <p>This plugin works fine for starting Mockey in the pre-integration-test phase, but blocks the build until Mockey has exited.  I'm not sure why this is occurring since I added this annotation specifically to prevent that issue:</p>
  
  <pre><code> @Execute(phase= LifecyclePhase.PACKAGE)
  </code></pre>
  
  <p>I actually copied this annotation from another plugin which does exactly what I'm trying to do here (We use the maven-tomcat7-plugin for launching our web application locally in the pre-integration-test phase and shutting it down in the post-integration-test phase).  I thought this would work the same way, but I'm seeing a different behavior.</p>
  
  <p><strong>Here's what I want to see happen:</strong></p>
  
  <ol>
  <li>Maven build begins on a single thread.</li>
  <li>This thread runs through all of the lifecycle phases from validate to package (<a href="http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html" rel="nofollow">reference</a>) and executes all of the plugins with goals that are bound to those phases.</li>
  <li>The thread gets to the pre-integration-test phase, sees that the mockey-maven-plugin's start-mockey goal is bound to the pre-integration-test phase, and attempts to execute the start-mockey goal.</li>
  <li>The start-mockey goal is annotated to execute on a second thread (<em>not</em> the first thread) without running <em>any</em> other goals for <em>any</em> other lifecycle phases beforehand or afterwards on the new thread.  The second thread starts Mockey's Jetty server by calling JettyRunner.main(args) and blocks on that method for the time being (it's running Mockey).</li>
  <li>The first thread continues on to other goals and phases (IE: run integration tests).</li>
  <li>The first thread gets to the post-integration test phase, sees that the mockey-maven-plugin's shutdown-mockey goal is bound to the post-integration-test phase, and executes the shutdown-mockey goal.</li>
  <li>The shutdown-mockey goal calls JettyRunner.stopServer() which hooks into a static object inside the JettyRunner class and signals the first thread to shutdown Jetty.  Meanwhile the first thread waits for a signal from the second thread (or maybe it's polling, I don't really know) that Jetty has shut down.</li>
  <li>The second thread finishes shutting down Jetty, signals to the first thread that it can continue, and kills itself.</li>
  <li>The first thread continues on to any additional goals and Maven lifecycle phases.</li>
  </ol>
  
  <p><strong>Here's what I'm actually seeing happen:</strong></p>
  
  <ol>
  <li>Maven build begins on a single thread.</li>
  <li>This thread runs through all of the lifecycle phases from validate to package (<a href="http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html" rel="nofollow">reference</a>) and executes all of the plugins with goals that are bound to those phases.</li>
  <li>The thread gets to the pre-integration-test phase, sees that the mockey-maven-plugin's start-mockey goal is bound to the pre-integration-test phase, and attempts to execute the start-mockey goal.</li>
  <li>The start-mockey goal is annotated to execute on a second thread.  The second thread starts the entire Maven lifecycle over again beginning in the validate phase.</li>
  <li>The first thread blocks while waiting for the second thread to exit.</li>
  <li>The second thread runs all the way through the package phase and then kills itself.</li>
  <li>The first thread is unblocked and picks up where it left off.  It executes the start-mockey goal on its own (never run by the second thread).  This calls JettyRunner.main(args) and the thread then blocks while running Mockey's Jetty server.</li>
  <li>The thread remains blocked until the Jetty server is <em>manually</em> killed (along with the rest of the Maven lifecycle).</li>
  </ol>
  
  <p>This confuses me primarily because Maven seems to have a different concept of forking than what I'm familiar with.  To me forking means to diverge at a particular point, not to start over, and not to affect the original process.  When we fork a process in Unix it copies the stack and function pointer of the first process.  It does not start over from the beginning of the program.  Similarly, when we fork a code repository we start with all of the files and directories that are currently in the original repository.  We don't start over with a blank slate.  So why, when we "fork" a Maven lifecycle does it abandon everything, start over, and block the original thread?  That doesn't seem at all like forking to me.  Here's some of the documentation I've read that describes "forking" in Maven:</p>
  
  <ul>
  <li>"<a href="http://books.sonatype.com/mvnref-book/reference/writing-plugins-sect-plugins-lifecycle.html" rel="nofollow">Running the ZipForkMojo will fork the lifecycle</a>"</li>
  <li>"<a href="http://mail-archives.apache.org/mod_mbox/maven-users/200801.mbox/%3C2BABBE7D2A66E04DB8A66A527D29927E35D96A@intrepid.infinity.nu%3E" rel="nofollow">Any plugin that declares @execute [phase] will cause the build to fork</a>"</li>
  <li>"<a href="http://maven.apache.org/plugin-tools/maven-plugin-annotations/apidocs/org/apache/maven/plugins/annotations/Execute.html" rel="nofollow">goal=goal to fork... lifecycle=lifecycle id to fork...  phase=lifecycle phase to fork...</a>"</li>
  </ul>
  
  <p><strong>Remaining Questions:</strong></p>
  
  <ul>
  <li>How can I get Maven to fork in the sense that I'm familiar with?</li>
  <li>What does it mean to fork a Maven lifecycle to a phase that takes place before the one that you're forking from?  For instance, what does it mean to fork to the package phase from the pre-integration-test phase?</li>
  <li>Why do you think the Tomcat7 plugin is doing this (forking to the package phase from the pre-integration test phase)?</li>
  <li>What could be different about the Tomcat7 plugin that causes the same annotation to behave differently in my plugin?</li>
  </ul>
  
  <p><strong>Answered Question (see below):</strong>
   - Is there another phase which I should specify in the annotation for my plugin to get it to behave as desired or should I be using the execute annotation in a fundamentally different manner?</p>