title: Programmatically resolving Maven dependencies outside of a plugin - get RepositorySystemSession and RepositorySystem
body: <p>Maybe this is going to be a larger task than I had originally thought, but regardless, I'm trying to load a <code>MavenProject</code> from a file and then resolve its dependencies. I've got the code for both bits but I'm missing some object references that I need; specifically I need to get instances of  <code>RepositorySystemSession</code> and <code>RepositorySystem</code>. Any tips?</p>
  
  <p><em>Note:</em> I have tagged this question with <a href="/questions/tagged/maven-plugin" class="post-tag" title="show questions tagged 'maven-plugin'" rel="tag">maven-plugin</a>, but this is not a Maven plugin. I am happy to mandate Maven 3 (think I already have anyway..)</p>
  
  <p>Here's the code I have so far:</p>
  
  <p><strong>Constructing the <code>MavenProject</code></strong>:</p>
  
  <pre><code>public static MavenProject loadProject(File pomFile) throws Exception
  {
      MavenProject ret = null;
      MavenXpp3Reader mavenReader = new MavenXpp3Reader();
  
      if (pomFile != null &amp;&amp; pomFile.exists())
      {
          FileReader reader = null;
  
          try
              {
              reader = new FileReader(pomFile);
              Model model = mavenReader.read(reader);
              model.setPomFile(pomFile);
  
              ret = new MavenProject(model);
          }
          finally
          {
              // Close reader
          }
      }
  
      return ret;
  }
  </code></pre>
  
  <p><strong>Resolving dependencies:</strong></p>
  
  <pre><code>public static List&lt;Dependency&gt; getArtifactsDependencies(MavenProject project, String dependencyType, String scope) throws Exception
  {    
      DefaultArtifact pomArtifact = new DefaultArtifact(project.getId());
  
      RepositorySystemSession repoSession = null; // TODO
      RepositorySystem repoSystem = null; // TODO
  
      List&lt;RemoteRepository&gt; remoteRepos = project.getRemoteProjectRepositories();
      List&lt;Dependency&gt; ret = new ArrayList&lt;Dependency&gt;();
  
      Dependency dependency = new Dependency(pomArtifact, scope);
  
      CollectRequest collectRequest = new CollectRequest();
      collectRequest.setRoot(dependency);
      collectRequest.setRepositories(remoteRepos);
  
      DependencyNode node = repoSystem.collectDependencies(repoSession, collectRequest).getRoot();
      DependencyRequest projectDependencyRequest = new DependencyRequest(node, null);
  
      repoSystem.resolveDependencies(repoSession, projectDependencyRequest);
  
      PreorderNodeListGenerator nlg = new PreorderNodeListGenerator();
      node.accept(nlg);
  
      ret.addAll(nlg.getDependencies(true));
  
      return ret;
  }
  </code></pre>
  
  <p>I realise this might be an unusual request, maybe I should just scrap what I was trying to do and wrap it as a plugin...but I kind of just want to finish what I started now! Thanks in advance.</p>