private void resolveIfNecessary(
     MavenArtifactInfo info,
     List<MavenRemoteRepository> repos,
     Map<MavenArtifactInfo, MavenArtifact> resolvedArtifacts)
     throws RemoteException {
   if (resolvedArtifacts.containsKey(info)) return;
   resolvedArtifacts.put(info, doResolve(info, repos));
 }
  @Override
  public Collection<MavenArtifact> resolvePlugin(
      @NotNull MavenPlugin plugin,
      @NotNull List<MavenRemoteRepository> repositories,
      int nativeMavenProjectId,
      boolean transitive)
      throws RemoteException, MavenServerProcessCanceledException {
    try {
      Plugin mavenPlugin = new Plugin();
      mavenPlugin.setGroupId(plugin.getGroupId());
      mavenPlugin.setArtifactId(plugin.getArtifactId());
      mavenPlugin.setVersion(plugin.getVersion());
      MavenProject project = RemoteNativeMavenProjectHolder.findProjectById(nativeMavenProjectId);

      MavenExecutionRequest request =
          createRequest(
              null,
              Collections.<String>emptyList(),
              Collections.<String>emptyList(),
              Collections.<String>emptyList());

      DefaultMaven maven = (DefaultMaven) getComponent(Maven.class);
      RepositorySystemSession repositorySystemSession = maven.newRepositorySession(request);

      if (plugin.getVersion() == null) {
        PluginVersionRequest versionRequest =
            new DefaultPluginVersionRequest(
                mavenPlugin, repositorySystemSession, project.getRemotePluginRepositories());
        mavenPlugin.setVersion(
            getComponent(PluginVersionResolver.class).resolve(versionRequest).getVersion());
      }

      PluginDescriptor result =
          getComponent(MavenPluginManager.class)
              .getPluginDescriptor(
                  mavenPlugin, project.getRemotePluginRepositories(), repositorySystemSession);

      Map<MavenArtifactInfo, MavenArtifact> resolvedArtifacts =
          new THashMap<MavenArtifactInfo, MavenArtifact>();

      Artifact pluginArtifact = result.getPluginArtifact();

      MavenArtifactInfo artifactInfo =
          new MavenArtifactInfo(
              pluginArtifact.getGroupId(),
              pluginArtifact.getArtifactId(),
              pluginArtifact.getVersion(),
              pluginArtifact.getType(),
              null);

      resolveIfNecessary(artifactInfo, repositories, resolvedArtifacts);

      if (transitive) {
        // todo try to use parallel downloading
        for (Artifact each : result.getIntroducedDependencyArtifacts()) {
          resolveIfNecessary(
              new MavenArtifactInfo(
                  each.getGroupId(), each.getArtifactId(), each.getVersion(), each.getType(), null),
              repositories,
              resolvedArtifacts);
        }
        for (ComponentDependency each : result.getDependencies()) {
          resolveIfNecessary(
              new MavenArtifactInfo(
                  each.getGroupId(), each.getArtifactId(), each.getVersion(), each.getType(), null),
              repositories,
              resolvedArtifacts);
        }
      }

      return new THashSet<MavenArtifact>(resolvedArtifacts.values());
    } catch (Exception e) {
      Maven3ServerGlobals.getLogger().info(e);
      return Collections.emptyList();
    }
  }