title: Obtaining a Maven-Instance from Plexus
body: <p>I'm currently attempting to get PlexusContainer to give me an initialized <code>Maven</code>-instance outside of a maven or eclipse execution context.</p>
  
  <p>For that purpose I have following code (simplified):</p>
  
  <pre class="lang-java prettyprint-override"><code>PlexusContainer container = null;
  try {
      container = new DefaultPlexusContainer();
      container.lookup(MavenResolutionTask.class).run();
      container.dispose();
  } catch (Exception e) {
      e.printStackTrace();
  }
  
  // --- MavenResolutionTask.java:
  
  @Component(role = MavenResolutionTask.class)
  // ResolutionTask is an abstract class 
  // implementing Runnable and extending CompletableFuture
  public class MavenResolutionTask extends ResolutionTask {
  
      @Requirement
      private Maven maven;
  }
  </code></pre>
  
  <p>Given that I had the standard <code>plexus-component-metadata:process-classes</code> plugin execution in my pom.xml I would've liked if this had worked like this.</p>
  
  <p>Unfortunately running this code results in a LookupException with the following reason (stacktrace omitted for brevity):</p>
  
  <pre class="lang-none prettyprint-override"><code>WARN Sisu - Error injecting: org.apache.maven.project.DefaultProjectBuildingHelper
  com.google.inject.ProvisionException: Guice provision errors:
  
  1) No implementation for org.apache.maven.repository.RepositorySystem was bound.
    while locating org.apache.maven.project.DefaultProjectBuildingHelper
  
  WARN Sisu - Error injecting: org.apache.maven.project.DefaultProjectBuilder
  com.google.inject.ProvisionException: Guice provision errors:
  
  1) No implementation for org.apache.maven.repository.RepositorySystem was bound.
    while locating org.apache.maven.project.DefaultProjectBuildingHelper
    at ClassRealm[plexus.core, parent: null]
    at ClassRealm[plexus.core, parent: null]
    while locating org.apache.maven.project.ProjectBuildingHelper
    while locating org.apache.maven.project.DefaultProjectBuilder
  
  ...
  </code></pre>
  
  <p>To remediate the missing bindings and prevent myself from having to go through the trouble of manually registering the complete dependency-graph of maven I attempted to merge the component descriptors included in my dependencies with the one generated for my application.</p>
  
  <p>For that purpose I copied all dependencies into <code>target/dependency</code> and ran some shell-scripts to get a list of all dependencies that include a <code>components.xml</code>.<br>
  I pass these dependencies to an execution of <code>dependency:unpack</code> before then passing the extracted locations to the metadata-merger like so:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;plugin&gt;
      &lt;groupId&gt;org.codehaus.plexus&lt;/groupId&gt;
      &lt;artifactId&gt;plexus-component-metadata&lt;/artifactId&gt;
      &lt;version&gt;1.7.1&lt;/version&gt;
      &lt;executions&gt;
          &lt;execution&gt;
              &lt;id&gt;process-classes&lt;/id&gt;
              &lt;goals&gt;
                  &lt;goal&gt;generate-metadata&lt;/goal&gt;
              &lt;/goals&gt;
          &lt;/execution&gt;
          &lt;execution&gt;
              &lt;id&gt;process-test-classes&lt;/id&gt;
              &lt;goals&gt;
                  &lt;goal&gt;generate-test-metadata&lt;/goal&gt;
              &lt;/goals&gt;
          &lt;/execution&gt;
          &lt;execution&gt;
              &lt;id&gt;merge-descriptors&lt;/id&gt;
              &lt;phase&gt;process-classes&lt;/phase&gt;
              &lt;goals&gt;
                  &lt;goal&gt;merge-metadata&lt;/goal&gt;
              &lt;/goals&gt;
          &lt;/execution&gt;
      &lt;/executions&gt;
      &lt;configuration&gt;
          &lt;output&gt;${plexus.outputFile}&lt;/output&gt;
          &lt;descriptors&gt;
              &lt;descriptor&gt;${plexus.outputFile}&lt;/descriptor&gt;
              &lt;!--./aether-transport-wagon-1.1.0.jar.d/META-INF/plexus/components.xml--&gt;
              &lt;descriptor&gt;${project.build.directory}/extracted-sources/aether-transport-wagon/META-INF/plexus/components.xml&lt;/descriptor&gt;
              &lt;!--./maven-aether-provider-3.1.0.jar.d/META-INF/plexus/components.xml--&gt;
              &lt;descriptor&gt;${project.build.directory}/extracted-sources/maven-aether-provider/META-INF/plexus/components.xml&lt;/descriptor&gt;
              &lt;!--./maven-artifact-manager-2.2.1.jar.d/META-INF/plexus/components.xml--&gt;
              &lt;descriptor&gt;${project.build.directory}/extracted-sources/maven-artifact-manager/META-INF/plexus/components.xml&lt;/descriptor&gt;
              &lt;!--./maven-core-3.1.1.jar.d/META-INF/plexus/components.xml--&gt;
              &lt;descriptor&gt;${project.build.directory}/extracted-sources/maven-core/META-INF/plexus/components.xml&lt;/descriptor&gt;
              &lt;!--./maven-model-builder-3.1.0.jar.d/META-INF/plexus/components.xml--&gt;
              &lt;descriptor&gt;${project.build.directory}/extracted-sources/maven-model-builder/META-INF/plexus/components.xml&lt;/descriptor&gt;
              &lt;!--./maven-plugin-registry-2.2.1.jar.d/META-INF/plexus/components.xml--&gt;
              &lt;descriptor&gt;${project.build.directory}/extracted-sources/maven-plugin-registry/META-INF/plexus/components.xml&lt;/descriptor&gt;
              &lt;!--./maven-profile-2.2.1.jar.d/META-INF/plexus/components.xml--&gt;
              &lt;descriptor&gt;${project.build.directory}/extracted-sources/maven-profile/META-INF/plexus/components.xml&lt;/descriptor&gt;
              &lt;!--./maven-project-2.2.1.jar.d/META-INF/plexus/components.xml--&gt;
              &lt;descriptor&gt;${project.build.directory}/extracted-sources/maven-project/META-INF/plexus/components.xml&lt;/descriptor&gt;
              &lt;!--./maven-settings-builder-3.1.1.jar.d/META-INF/plexus/components.xml--&gt;
              &lt;descriptor&gt;${project.build.directory}/extracted-sources/maven-settings-builder/META-INF/plexus/components.xml&lt;/descriptor&gt;
              &lt;!--./org.eclipse.sisu.plexus-0.0.0.M2a.jar.d/META-INF/plexus/components.xml--&gt;
              &lt;descriptor&gt;${project.build.directory}/extracted-sources/sisu-plexus/META-INF/plexus/components.xml&lt;/descriptor&gt;
              &lt;!--./plexus-cipher-1.4.jar.d/META-INF/plexus/components.xml--&gt;
              &lt;descriptor&gt;${project.build.directory}/extracted-sources/plexus-cipher/META-INF/plexus/components.xml&lt;/descriptor&gt;
              &lt;!--./plexus-interactivity-api-1.0-alpha-6.jar.d/META-INF/plexus/components.xml--&gt;
              &lt;descriptor&gt;${project.build.directory}/extracted-sources/plexus-interactivity-api/META-INF/plexus/components.xml&lt;/descriptor&gt;
              &lt;!--./plexus-sec-dispatcher-1.3.jar.d/META-INF/plexus/components.xml--&gt;
              &lt;descriptor&gt;${project.build.directory}/extracted-sources/plexus-sec-dispatcher/META-INF/plexus/components.xml&lt;/descriptor&gt;
              &lt;!--./wagon-ssh-1.0.jar.d/META-INF/plexus/components.xml--&gt;
              &lt;descriptor&gt;${project.build.directory}/extracted-sources/wagon-ssh/META-INF/plexus/components.xml&lt;/descriptor&gt;
              &lt;!--./wagon-ssh-common-1.0.jar.d/META-INF/plexus/components.xml--&gt;
              &lt;descriptor&gt;${project.build.directory}/extracted-sources/wagon-ssh-common/META-INF/plexus/components.xml&lt;/descriptor&gt;
          &lt;/descriptors&gt;
      &lt;/configuration&gt;
  &lt;/plugin&gt;
  </code></pre>
  
  <p>Unfortunately the error message persists
  So I guess my question is: <strong>How do I easily get a <code>Maven</code> instance from Plexus?</strong></p>
  
  <hr>
  
  <p>P.S. for completeness sake, I'm currently using the following dependencies:</p>
  
  <pre class="lang-none prettyprint-override"><code>[INFO] com.github.vogel612:dependency-analyzer:jar:0.0.1
  [INFO] +- org.projectlombok:lombok:jar:1.16.16:compile
  [INFO] +- commons-cli:commons-cli:jar:1.4:compile
  [INFO] +- org.slf4j:slf4j-simple:jar:1.7.16:compile
  [INFO] |  \- org.slf4j:slf4j-api:jar:1.7.16:compile
  [INFO] +- org.codehaus.plexus:plexus-utils:jar:3.0.24:compile
  [INFO] +- org.eclipse.aether:aether-api:jar:1.1.0:compile
  [INFO] +- org.eclipse.aether:aether-util:jar:1.1.0:compile
  [INFO] +- org.eclipse.aether:aether-impl:jar:1.1.0:compile
  [INFO] |  \- org.eclipse.aether:aether-spi:jar:1.1.0:compile
  [INFO] +- org.eclipse.aether:aether-connector-basic:jar:1.1.0:compile
  [INFO] +- org.eclipse.aether:aether-transport-file:jar:1.1.0:compile
  [INFO] +- org.eclipse.aether:aether-transport-http:jar:1.1.0:compile
  [INFO] |  +- org.apache.httpcomponents:httpclient:jar:4.3.5:compile
  [INFO] |  |  +- org.apache.httpcomponents:httpcore:jar:4.3.2:compile
  [INFO] |  |  \- commons-codec:commons-codec:jar:1.6:compile
  [INFO] |  \- org.slf4j:jcl-over-slf4j:jar:1.6.2:compile
  [INFO] +- org.eclipse.aether:aether-transport-wagon:jar:1.1.0:compile
  [INFO] |  \- org.apache.maven.wagon:wagon-provider-api:jar:1.0:compile
  [INFO] +- org.apache.maven:maven-aether-provider:jar:3.1.0:compile
  [INFO] |  +- org.apache.maven:maven-model:jar:3.1.0:compile
  [INFO] |  +- org.apache.maven:maven-model-builder:jar:3.1.0:compile
  [INFO] |  +- org.apache.maven:maven-repository-metadata:jar:3.1.0:compile
  [INFO] |  +- org.eclipse.sisu:org.eclipse.sisu.plexus:jar:0.0.0.M2a:compile
  [INFO] |  |  +- javax.enterprise:cdi-api:jar:1.0:compile
  [INFO] |  |  |  +- javax.annotation:jsr250-api:jar:1.0:compile
  [INFO] |  |  |  \- javax.inject:javax.inject:jar:1:compile
  [INFO] |  |  +- com.google.guava:guava:jar:10.0.1:compile
  [INFO] |  |  |  \- com.google.code.findbugs:jsr305:jar:1.3.9:compile
  [INFO] |  |  +- org.sonatype.sisu:sisu-guice:jar:no_aop:3.1.0:compile
  [INFO] |  |  |  \- aopalliance:aopalliance:jar:1.0:compile
  [INFO] |  |  \- org.eclipse.sisu:org.eclipse.sisu.inject:jar:0.0.0.M2a:compile
  [INFO] |  |     \- asm:asm:jar:3.3.1:compile
  [INFO] |  \- org.codehaus.plexus:plexus-component-annotations:jar:1.5.5:compile
  [INFO] +- org.apache.maven.wagon:wagon-ssh:jar:1.0:compile
  [INFO] |  +- com.jcraft:jsch:jar:0.1.44-1:compile
  [INFO] |  \- org.apache.maven.wagon:wagon-ssh-common:jar:1.0:compile
  [INFO] |     \- org.codehaus.plexus:plexus-interactivity-api:jar:1.0-alpha-6:compile
  [INFO] +- org.apache.maven:maven-core:jar:3.1.1:compile
  [INFO] |  +- org.apache.maven:maven-settings:jar:3.1.1:compile
  [INFO] |  +- org.apache.maven:maven-settings-builder:jar:3.1.1:compile
  [INFO] |  +- org.apache.maven:maven-artifact:jar:3.1.1:compile
  [INFO] |  +- org.apache.maven:maven-plugin-api:jar:3.1.1:compile
  [INFO] |  +- org.codehaus.plexus:plexus-interpolation:jar:1.19:compile
  [INFO] |  +- org.codehaus.plexus:plexus-classworlds:jar:2.5.1:compile
  [INFO] |  \- org.sonatype.plexus:plexus-sec-dispatcher:jar:1.3:compile
  [INFO] |     \- org.sonatype.plexus:plexus-cipher:jar:1.4:compile
  [INFO] \- org.apache.maven:maven-project:jar:2.2.1:compile
  [INFO]    +- org.apache.maven:maven-profile:jar:2.2.1:compile
  [INFO]    +- org.apache.maven:maven-artifact-manager:jar:2.2.1:compile
  [INFO]    |  \- backport-util-concurrent:backport-util-concurrent:jar:3.1:compile
  [INFO]    +- org.apache.maven:maven-plugin-registry:jar:2.2.1:compile
  [INFO]    \- org.codehaus.plexus:plexus-container-default:jar:1.0-alpha-9-stable-1:compile
  [INFO]       +- junit:junit:jar:3.8.1:compile
  [INFO]       \- classworlds:classworlds:jar:1.1-alpha-2:compile
  </code></pre>