Example #1
0
  @Override
  public DependencyMetadata resolveDependencyMetadata(
      Dependency query, final List<DependencyRepository> repositories) {
    try {
      if (Strings.isNullOrEmpty(query.getVersion())) {
        query = DependencyBuilder.create(query).setVersion("[,)");
      }

      RepositorySystem system = container.lookup(RepositorySystem.class);
      MavenRepositorySystemSession session = setupRepoSession(system);

      DefaultArtifact artifact = new DefaultArtifact(query.toCoordinates());

      ArtifactDescriptorRequest ar =
          new ArtifactDescriptorRequest(artifact, convertToMavenRepos(repositories), null);
      ArtifactDescriptorResult results = system.readArtifactDescriptor(session, ar);

      Artifact a = results.getArtifact();
      Dependency d =
          DependencyBuilder.create()
              .setArtifactId(a.getArtifactId())
              .setGroupId(a.getGroupId())
              .setVersion(a.getVersion());

      return new DependencyMetadataImpl(d, results);
    } catch (Exception e) {
      throw new ProjectModelException(
          "Unable to resolve any artifacts for query [" + query + "]", e);
    }
  }
  @Test
  public void testLookup() throws ArtifactDescriptorException {
    StubArtifact art = new StubArtifact("gid:aid:ext:ver");
    ArtifactDescriptorRequest request = new ArtifactDescriptorRequest(art, null, "");
    ArtifactDescriptorResult description = reader.readArtifactDescriptor(session, request);

    assertEquals(request, description.getRequest());
    assertEquals(art, description.getArtifact());

    assertEquals(1, description.getRelocations().size());
    Artifact artifact = description.getRelocations().get(0);
    assertEquals("gid", artifact.getGroupId());
    assertEquals("aid", artifact.getArtifactId());
    assertEquals("ver", artifact.getVersion());
    assertEquals("ext", artifact.getExtension());

    assertEquals(1, description.getRepositories().size());
    RemoteRepository repo = description.getRepositories().get(0);
    assertEquals("id", repo.getId());
    assertEquals("type", repo.getContentType());
    assertEquals("protocol://some/url?for=testing", repo.getUrl());

    assertDependencies(description.getDependencies());
    assertDependencies(description.getManagedDependencies());
  }
  private boolean hasArtifact(
      final RepositorySystemSession session,
      final VersionRangeRequest request,
      final Version version) {

    if (version == null) {
      return false;
    }

    /* this will blow, because it'll try to find a jar for a pom artifact
    try {
        final Artifact artifact = request.getArtifact();
        final Artifact versionedArtifact =
                new DefaultArtifact(artifact.getGroupId(),
                        artifact.getArtifactId(), artifact.getClassifier(),
                        artifact.getExtension(), version.toString(),
                        artifact.getProperties(), artifact.getFile());

        final ArtifactRequest alternateRequest =
                new ArtifactRequest(versionedArtifact,
                        singletonList(fossRepository),
                        request.getRequestContext());

        alternateRequest.setTrace(request.getTrace());

        final ArtifactResult result =
                artifactResolver.resolveArtifact(session, alternateRequest);

        if (result.getExceptions().isEmpty())
            return true;
    } catch (ArtifactResolutionException e) {
        throw new RuntimeException(e);
    }
    */

    try {
      final ArtifactDescriptorRequest alternateRequest =
          new ArtifactDescriptorRequest(
                  request.getArtifact(), singletonList(fossRepository), request.getRequestContext())
              .setTrace(request.getTrace());

      final ArtifactDescriptorResult result =
          delegate.readArtifactDescriptor(session, alternateRequest);
      if (result.getExceptions().isEmpty()) {
        return true;
      }
    } catch (ArtifactDescriptorException e) {
      throw new RuntimeException(e);
    }

    return false;
  }
  public void execute() throws MojoExecutionException, MojoFailureException {
    try {
      ArtifactDescriptorResult artifactDescriptor =
          repoSystem.readArtifactDescriptor(
              repoSession,
              new ArtifactDescriptorRequest(new DefaultArtifact(artifact), projectRepos, null));

      CollectRequest collectRequest = new CollectRequest();
      collectRequest.setDependencies(artifactDescriptor.getDependencies());
      collectRequest.setManagedDependencies(artifactDescriptor.getManagedDependencies());
      collectRequest.setRepositories(projectRepos);

      CollectResult dependencies = repoSystem.collectDependencies(repoSession, collectRequest);

      StringWriter writer = new StringWriter();
      NodeFormatter nodeFormatter =
          new DefaultNodeFormatter(artifactDescriptor.getArtifact().toString());
      dependencies
          .getRoot()
          .accept(
              new SerializingDependencyVisitor(
                  writer, SerializingDependencyVisitor.STANDARD_TOKENS, nodeFormatter));

      try {
        if (outputFile == null) {
          log(writer.toString(), getLog());
        } else {
          write(writer.toString(), outputFile);
          getLog().info("Wrote dependency tree to: " + outputFile);
        }
      } catch (IOException e) {
        throw new MojoExecutionException("Failed to write dependencies to file", e);
      }
    } catch (ArtifactDescriptorException e) {
      throw new MojoExecutionException("Failed to read artifact descriptor", e);
    } catch (DependencyCollectionException e) {
      throw new MojoExecutionException("Failed to collect dependencies", e);
    }
  }
  @Override
  public ArtifactDescriptorResult readArtifactDescriptor(
      RepositorySystemSession session, ArtifactDescriptorRequest request)
      throws ArtifactDescriptorException {

    ArtifactDescriptorException originalException = null;
    final Artifact artifact = request.getArtifact();
    // try FOSS local repo
    {
      try {
        final ArtifactDescriptorRequest alternateRequest =
            new ArtifactDescriptorRequest(
                    request.getArtifact(),
                    singletonList(fossRepository),
                    request.getRequestContext())
                .setTrace(request.getTrace());

        final ArtifactDescriptorResult result =
            delegate.readArtifactDescriptor(session, alternateRequest);
        if (result.getExceptions().isEmpty()) {
          return result;
        }
      } catch (ArtifactDescriptorException e) {
        originalException = e;
      }
    }
    // try FOSS local repo with LATEST
    if (!artifact.getVersion().equals(LATEST_VERSION)) {
      try {
        final Artifact alternateArtifact = artifact.setVersion(LATEST_VERSION);

        final ArtifactDescriptorRequest alternateRequest =
            new ArtifactDescriptorRequest(
                    alternateArtifact, singletonList(fossRepository), request.getRequestContext())
                .setTrace(request.getTrace());

        final ArtifactDescriptorResult result =
            delegate.readArtifactDescriptor(session, alternateRequest);
        if (result.getExceptions().isEmpty()) {
          logger.warn(
              "Could not find artifact descriptor "
                  + artifact
                  + ", using LATEST "
                  + result.getArtifact());
          return result;
        }
      } catch (ArtifactDescriptorException e) {
        logger.debug("LATEST resolution of " + artifact + " failed", e);
        if (originalException == null) {
          originalException = e;
        }
      }
    }
    // try JPP local repo
    if (useJpp) {
      // use maven as much as possible
      final RepositorySystemSession alternateSession = openJpp(session);
      try {
        final ArtifactDescriptorRequest alternateRequest =
            new ArtifactDescriptorRequest(
                request.getArtifact(), singletonList(fossRepository), request.getRequestContext());

        alternateRequest.setTrace(request.getTrace());

        final ArtifactDescriptorResult result =
            delegate.readArtifactDescriptor(alternateSession, alternateRequest);
        //                logger.warn("result from JPP " + result);
        if (result.getExceptions().isEmpty()) {
          // TODO: I may want to muck the result a bit to make sure JPP is also used for
          // resolveArtifact
          // JPP probably did not return the proper version, which
          // makes the MavenPluginValidator barf
          // lets muck it
          //                    result.setArtifact(new JPPArtifact(result.getArtifact()));
          logger.warn(
              "Could not find artifact descriptor "
                  + artifact
                  + ", using JPP "
                  + result.getArtifact());
          return result;
        }
      } catch (ArtifactDescriptorException e) {
        logger.debug("JPP resolution of " + artifact + " failed", e);
        if (originalException == null) {
          originalException = e;
        }
      }
    }

    if (originalException != null) {
      throw originalException;
    }

    throw new RuntimeException(
        "NYI: org.fedoraproject.maven.repository.internal."
            + "FossRepositorySystem.readArtifactDescriptor");
  }