Set<Artifact> getResolvedArtifactsFromUnresolvedDependencies(
      List<Dependency> unresolvedDependencies, boolean resolveTransitively)
      throws MojoExecutionException {
    final Set<Artifact> resolvedArtifacts = new HashSet<Artifact>();
    //    Artifact mojoArtifact = this.artifactFactory.createBuildArtifact(project.getGroupId(),
    // project.getArtifactId(), project.getVersion(), "pom");

    /*
     * Resolve each artifact.  This will get all transitive artifacts AND eliminate conflicts.
     */
    final Set<Artifact> unresolvedArtifacts;

    //    for (Dependency d : unresolvedDependencies)
    //      System.out.println("dependency: " + d.toString());

    try {
      unresolvedArtifacts =
          MavenMetadataSource.createArtifacts(
              this.artifactFactory, unresolvedDependencies, null, null, null);
      //      for (Artifact artifact : unresolvedArtifacts) {
      //        System.out.println("unresolved " + artifact.toString());
      //      }

      if (resolveTransitively) {
        ArtifactResolutionResult artifacts =
            artifactResolver.resolveTransitively(
                unresolvedArtifacts,
                project.getArtifact(),
                remoteRepositories,
                localRepository,
                artifactMetadataSource);
        resolvedArtifacts.addAll(artifacts.getArtifacts());
      } else {
        // resolve each artifact individually
        for (Artifact artifact : unresolvedArtifacts) {
          artifactResolver.resolve(artifact, remoteRepositories, localRepository);

          resolvedArtifacts.add(artifact);
        }
      }

    } catch (Exception e) {
      throw new MojoExecutionException("Unable to complete configuring the build settings", e);
    }

    for (Artifact artifact : resolvedArtifacts) {
      System.out.println("matched " + artifact.toString());
    }

    return resolvedArtifacts;
  }
  private Set<Artifact> collectAllProjectArtifacts() throws MojoExecutionException {
    final Set<Artifact> resolvedArtifacts = new HashSet<Artifact>();

    /*
     * Get the Grails dependencies from the plugin's POM file first.
     */
    final MavenProject pluginProject;
    try {
      pluginProject = getPluginProject();
    } catch (ProjectBuildingException e) {
      throw new MojoExecutionException("Unable to get plugin project", e);
    }

    /*
     * Add the plugin's dependencies and the project using the plugin's dependencies to the list
     * of unresolved dependencies.  This is done so they can all be resolved at the same time so
     * that we get the benefit of Maven's conflict resolution.
     */

    Set<Artifact> uncheckedArtifacts =
        useTransitives
            ? resolveFromTree()
            : getResolvedArtifactsFromUnresolvedDependencies(project.getDependencies(), false);
    Map<String, Artifact> checklist = new HashMap<String, Artifact>();

    for (Artifact artifact : uncheckedArtifacts) {
      //      resolvedArtifacts.add(artifact);
      checklist.put(artifactToKey(artifact), artifact);
      //      resolvedArtifacts.add(artifact);
    }

    // major breaking change, no dependencies from plugin
    /*
        for( Artifact artifact : getResolvedArtifactsFromUnresolvedDependencies(replaceVersion(filterGrailsDependencies(pluginProject.getDependencies())), true) ) {

    //      resolvedArtifacts.add(artifact);

          String key = artifactToKey(artifact);
          Artifact existing = checklist.get(key);

          if (existing == null)
            checklist.put(key, artifact);
        }
        */

    resolvedArtifacts.addAll(checklist.values());

    return resolvedArtifacts;
  }
  /**
   * Removes any Grails plugin artifacts from the supplied list of dependencies. A Grails plugin is
   * any artifact whose type is equal to "grails-plugin" or "zip"
   *
   * @param artifact The list of artifacts to be cleansed.
   * @return list of plugins
   */
  private Set<Artifact> removePluginArtifacts(final Set<Artifact> artifact) {
    final Set<Artifact> pluginArtifacts = new HashSet<Artifact>();

    if (artifact != null) {
      for (final Iterator<Artifact> iter = artifact.iterator(); iter.hasNext(); ) {
        final Artifact dep = iter.next();
        if (dep.getType() != null
            && (dep.getType().equals("grails-plugin")
                || dep.getType().equals("zip")
                || (dep.getType().equals("grails-plugin2")
                    && "plugin".equals(dep.getClassifier())))) {
          pluginArtifacts.add(dep);
          //          System.out.println("removing " + dep.toString());
          iter.remove();
        }
      }
    }

    return pluginArtifacts;
  }
  @NotNull
  @Override
  public List<MavenArtifact> resolveTransitively(
      @NotNull List<MavenArtifactInfo> artifacts,
      @NotNull List<MavenRemoteRepository> remoteRepositories)
      throws RemoteException, MavenServerProcessCanceledException {

    try {
      Set<Artifact> toResolve = new LinkedHashSet<Artifact>();
      for (MavenArtifactInfo each : artifacts) {
        toResolve.add(createArtifact(each));
      }

      Artifact project =
          getComponent(ArtifactFactory.class).createBuildArtifact("temp", "temp", "666", "pom");

      Set<Artifact> res =
          getComponent(ArtifactResolver.class)
              .resolveTransitively(
                  toResolve,
                  project,
                  Collections.EMPTY_MAP,
                  myLocalRepository,
                  convertRepositories(remoteRepositories),
                  getComponent(ArtifactMetadataSource.class))
              .getArtifacts();

      return MavenModelConverter.convertArtifacts(
          res, new THashMap<Artifact, MavenArtifact>(), getLocalRepositoryFile());
    } catch (ArtifactResolutionException e) {
      Maven3ServerGlobals.getLogger().info(e);
    } catch (ArtifactNotFoundException e) {
      Maven3ServerGlobals.getLogger().info(e);
    } catch (Exception e) {
      throw rethrowException(e);
    }

    return Collections.emptyList();
  }
  private void printIntellijIDEASettings(
      DecentGrailsLauncher launcher, Field settingsField, Set<Artifact> pluginArtifacts) {
    try {
      Object settings = settingsField.get(launcher);
      Field configField = settings.getClass().getSuperclass().getDeclaredField("config");
      configField.setAccessible(true);
      Object config = configField.get(settings);
      Map flatten = (Map) config.getClass().getDeclaredMethod("flatten").invoke(config);

      System.out.println();
      System.out.println(SETTINGS_START_MARKER);

      for (Object key : flatten.keySet()) {
        Object value = flatten.get(key);
        if (value instanceof String || value instanceof GString) {
          String realKey = key.toString();
          if (GRAILS_PROPERTY_LIST.contains(realKey)) {
            System.out.println(realKey + "=" + value.toString().replace('\\', '/'));
          }
        }
      }

      for (Artifact plugin : pluginArtifacts) {
        File targetDir = getPluginTargetDir(plugin);
        System.out.println(
            "grails.plugin.location."
                + getPluginName(plugin)
                + "="
                + targetDir.getAbsolutePath().replace('\\', '/'));
      }

      System.out.println();
      System.out.println(SETTINGS_END_MARKER);
    } catch (Exception ex) {
      getLog().error("Unable to get flattened configuration data", ex);
    }
  }