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;
  }
Example #2
0
  /**
   * Resolves dependencies of a bundle <code>groupId:artifactId:version</code>
   *
   * @param groupId
   * @param artifactId
   * @param version
   */
  @SuppressWarnings("unchecked")
  public ArtifactResolutionResult resolve(
      final String groupId, final String artifactId, final String version) {

    // final Artifact artifact = artifactFactory.createDependencyArtifact(groupId, artifactId,
    // VersionRange.createFromVersion(version), type, "", scope);
    final Artifact pomArtifact = getPomArtifact(groupId, artifactId, version);

    try {
      // load the pom as a MavenProject and get all of the dependencies for the project
      final MavenProject project = loadPomAsProject(projectBuilder, pomArtifact);
      final List dependencies = project.getDependencies();

      // make Artifacts of all the dependencies and the project itself
      final Set<Artifact> dependencyArtifacts =
          MavenMetadataSource.createArtifacts(artifactFactory, dependencies, null, null, null);
      dependencyArtifacts.add(project.getArtifact());

      // create listener for monitoring the resolution process
      final List<RuntimeResolutionListener> listeners = new ArrayList<RuntimeResolutionListener>();
      listeners.add(new RuntimeResolutionListener());

      // resolve all dependencies transitively to obtain a comprehensive list of jars
      final ArtifactResolutionResult result =
          artifactResolver.resolveTransitively(
              dependencyArtifacts,
              pomArtifact,
              Collections.EMPTY_MAP,
              localRepository,
              remoteRepositories,
              metadataSource,
              null,
              listeners);

      if (debug) {
        for (final Object o : result.getArtifactResolutionNodes()) {
          System.out.println(o.toString());
        }
      }

      return result;
    } catch (final Exception e) {
      throw new RuntimeException("Dependency Resolver failed", e);
    }
  }
Example #3
0
  private Set<Artifact> resolveExecutableDependencies(Artifact executablePomArtifact)
      throws MojoExecutionException {

    Set<Artifact> executableDependencies;
    try {
      MavenProject executableProject =
          this.projectBuilder.buildFromRepository(
              executablePomArtifact, this.remoteRepositories, this.localRepository);

      // get all of the dependencies for the executable project
      List<Artifact> dependencies = CastUtils.cast(executableProject.getDependencies());

      // make Artifacts of all the dependencies
      Set<Artifact> dependencyArtifacts =
          CastUtils.cast(
              MavenMetadataSource.createArtifacts(
                  this.artifactFactory, dependencies, null, null, null));

      // not forgetting the Artifact of the project itself
      dependencyArtifacts.add(executableProject.getArtifact());

      // resolve all dependencies transitively to obtain a comprehensive
      // list of assemblies
      ArtifactResolutionResult result =
          artifactResolver.resolveTransitively(
              dependencyArtifacts,
              executablePomArtifact,
              Collections.EMPTY_MAP,
              this.localRepository,
              this.remoteRepositories,
              metadataSource,
              null,
              Collections.EMPTY_LIST);
      executableDependencies = CastUtils.cast(result.getArtifacts());

    } catch (Exception ex) {
      throw new MojoExecutionException(
          "Encountered problems resolving dependencies of the executable "
              + "in preparation for its execution.",
          ex);
    }

    return executableDependencies;
  }
  /**
   * Create the transitive classpath.
   *
   * @return The dependent artifacts.
   * @throws MojoExecutionException If the classpath can't be found.
   */
  public Set transitivelyResolvePomDependencies() throws MojoExecutionException {
    // make Artifacts of all the dependencies
    Set dependencyArtifacts;
    try {
      dependencyArtifacts =
          MavenMetadataSource.createArtifacts(artifactFactory, dependencies, null, null, null);
    } catch (InvalidDependencyVersionException e) {
      throw new MojoExecutionException("Invalid dependency", e);
    }

    // not forgetting the Artifact of the project itself
    dependencyArtifacts.add(project.getArtifact());

    List listeners = Collections.EMPTY_LIST;

    // resolve all dependencies transitively to obtain a comprehensive list
    // of jars
    ArtifactResolutionResult result;
    try {
      result =
          artifactResolver.resolveTransitively(
              dependencyArtifacts,
              project.getArtifact(),
              Collections.EMPTY_MAP,
              localRepository,
              remoteRepositories,
              metadataSource,
              null,
              listeners);
    } catch (ArtifactResolutionException e) {
      throw new MojoExecutionException("Unable to resolve Artifact.", e);
    } catch (ArtifactNotFoundException e) {
      throw new MojoExecutionException("Unable to resolve Artifact.", e);
    }

    return result.getArtifacts();
  }