title: How to execute a specific plugin/Mojo from a pom.xml programmatically?
body: <p>I'm the author of one of the Maven plugins (not Apache/Codehaus, completely indie). Sometimes I get support requests or test cases where I'd really need to debug the execution of my plugin with an existing <code>pom.xml</code>. Basically the test cases I get are sample/test project (<code>pom.xml</code> with <code>src/main/resoures</code>, <code>src/main/java</code> and so on).</p>
  
  <p>What I need is a way to:</p>
  
  <ul>
  <li>Load an existing <code>pom.xml</code>.</li>
  <li>Find a specific execution of my plugin there (usually it's the only one).</li>
  <li>Get an instance of <code>MyMojo</code> - fully initialized/condigured, with all the components and parameters corectly injected.</li>
  <li>Execute <code>MyMojo</code>.</li>
  <li>What's important is that test projects are separate projects, I don't want to copy them into the Maven module of my plugin.</li>
  <li>I'd like to be able to do this without remote debugging.</li>
  </ul>
  
  <p>By debugging I mean to be able to set and halt on breakpoints (also conditional), step in/out/over on the source code.</p>
  
  <p>Ideally I'd like to be able to <code>executeMyMojoFrom(new File("pom.xml"))</code> - for instance in a JUnit test or a <code>main</code> method of some class. (I can supply <code>groupId</code>, <code>artifactId</code> etc. All other definitions should just be loaded from that <code>pom.xml</code>.)</p>
  
  <p><strong>How can I achieve this?</strong></p>
  
  <hr>
  
  <p>What I've tried so far:</p>
  
  <ul>
  <li><code>Debug As...</code> on <code>pom.xml</code> in Eclipse - does not work well enough (source code not found, breakpoint don't work as its not a Java project context)</li>
  <li>Maven Embedder/Invoker solutions - spawn things in separate processes via CLI. Forget breakpoints, no debugging.</li>
  <li>Remote debugging with <code>mvnDebug</code> and then remote debugging from Eclipse as suggested by Pascal Thivent <a href="https://stackoverflow.com/questions/2935375/debugging-in-maven/19986408#19986408">here</a>. This is so far the best option. However, remote debugging means starting <code>mvnDebug</code> separately, and there's also not guarantee that the JARs I have in Eclipse are exactly the same that <code>mvnDebug</code> is using. So there's a certain distance here.</li>
  <li><a href="http://maven.apache.org/plugin-testing/maven-plugin-testing-harness/getting-started/index.html" rel="nofollow noreferrer"><code>maven-plugin-testing-harness</code></a> - I actually thought this this will do the task. But first I was jumping through hoops for a few hours just to make it start. All of the important dependencies are "provided" so I first had to figure out the right combination of versions of these artifacts. And then - only to discover that <a href="http://maven.apache.org/plugin-testing/maven-plugin-testing-harness/getting-started/index.html" rel="nofollow noreferrer"><code>AbstractMojoTestCase</code></a>  only works within the plugin module you want to test. Probably I was mistaken when I thought that <code>maven-plugin-testing-harness</code> was a testing harness for Maven plugins. It seems that it's a testing harness for the plugin from that plugin's module. Which is not illogical but does not help my case. I'd like to test my plugin in <em>other</em> modules.</li>
  </ul>
  
  <p>So right now I've got the best results with the remote debugging solution. But what I'm looking for is really something like <code>maven-plugin-testing-harness</code> but not hardwired to the plugin module. Does anyone happen to have a hint, if such a method exists somewhere in Maven artifacts?</p>
  
  <p>To be even more specific, I'd like to write something like:</p>
  
  <pre><code> public void testSomething()
          throws Exception
      {
          File pom = getTestFile( "pom.xml" );
          assertNotNull( pom );
          assertTrue( pom.exists() );
  
          MyMojo myMojo = (MyMojo) lookupMojo( "myGroupId", "myArtifactid", ...,
                                               "myGoal", pom );
          assertNotNull( myMojo );
          myMojo.execute();
  
          ...
      }
  </code></pre>
  
  <p>Compare it to the <code>MyMojoTest</code> <a href="http://maven.apache.org/plugin-testing/maven-plugin-testing-harness/getting-started/index.html" rel="nofollow noreferrer">here</a> - it's almost there. Should just not be hardwired into the <code>mymojo</code> Maven module (as it is in <code>maven-plugin-testing-harness</code>).</p>
  
  <hr>
  
  <p><strong>Update</strong></p>
  
  <p>Few answers to the questions in comments:</p>
  
  <blockquote>
    <p>You mean you don't want such a test class, i.e <code>MyMojoTest</code> to reside inside the same project as the <code>MyMojo</code>, i.e your plugin project? Why is that?</p>
  </blockquote>
  
  <p>Exactly. I want to debug the plugin execution in an existing Maven project, I don't want to move that project to my plugin project <em>first</em> to be able to run a test. I want to be able to test/debug an existing project. Ideally, I'd just need to add <code>my-maven-plugin-testing</code> dependency and subclass <code>MyMojoTest</code> in the project's src/test/jaca. This would be a good instrument to debug executions. Dragging the target project into my Mojo project ist just too much overhead - and mostly these aren't really the test cases I want to keep long-term. I hope, this answers, why.</p>
  
  <blockquote>
    <p>Anyway, it's merely a convention to keep the <code>project-to-test/pom.xml</code> inside the <code>src/test/resources</code> of your plugin module, not a rule...</p>
  </blockquote>
  
  <p>My problem is not the location of the <code>pom.xml</code> of the <code>project-to-test</code>, that is easily configurable. My difficulty is that <code>maven-plugin-testing-harness</code> is is somehow hardcoded to be in the Mojo's project. It uses the <code>pom.xml</code> of the Mojo, looks for other special files/descriptors in the containing project. So I somehow can't use it in a non-Mojo project, or can I? This is my question.</p>
  
  <blockquote>
    <p>And I'm not sure why <code>Debug as...</code> didn't help you...</p>
  </blockquote>
  
  <p>Not sure either, but (1) breakpoints did not work and (2) the source code was not "attached" for some reason.</p>