@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); } }
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); } }