@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); } }
private VersionRangeResult getVersions( Dependency dep, final List<RemoteRepository> repositories) { try { String version = dep.getVersion(); if (Strings.isNullOrEmpty(version)) { dep = DependencyBuilder.create(dep).setVersion("[,)"); } else if (!version.matches("(\\(|\\[).*?(\\)|\\])")) { dep = DependencyBuilder.create(dep).setVersion("[" + version + "]"); } RepositorySystem maven = container.lookup(RepositorySystem.class); MavenRepositorySystemSession session = setupRepoSession(maven); VersionRangeRequest rangeRequest = new VersionRangeRequest(new DefaultArtifact(dep.toCoordinates()), repositories, null); VersionRangeResult rangeResult = maven.resolveVersionRange(session, rangeRequest); return rangeResult; } catch (Exception e) { throw new ProjectModelException("Failed to look up versions for [" + dep + "]", e); } }
@Override public List<DependencyResource> resolveDependencies( Dependency dep, final List<DependencyRepository> repositories) { List<DependencyResource> result = new ArrayList<DependencyResource>(); try { if (Strings.isNullOrEmpty(dep.getVersion())) { dep = DependencyBuilder.create(dep).setVersion("[,)"); } RepositorySystem system = container.lookup(RepositorySystem.class); MavenRepositorySystemSession session = setupRepoSession(system); DefaultArtifact artifact = new DefaultArtifact(dep.toCoordinates()); CollectRequest collectRequest = new CollectRequest( new org.sonatype.aether.graph.Dependency(artifact, null), convertToMavenRepos(repositories)); DependencyRequest request = new DependencyRequest(collectRequest, null); DependencyResult artifacts = system.resolveDependencies(session, request); for (ArtifactResult a : artifacts.getArtifactResults()) { File file = a.getArtifact().getFile(); Dependency d = DependencyBuilder.create() .setArtifactId(a.getArtifact().getArtifactId()) .setGroupId(a.getArtifact().getGroupId()) .setVersion(a.getArtifact().getVersion()); DependencyResource resource = new DependencyResource(factory, file, d); result.add(resource); } return result; } catch (Exception e) { throw new ProjectModelException("Unable to resolve an artifact", e); } }
@Override public String getStringValue() { return Strings.unquote(getLiteralValue()); }