URL createMavenGavURL(String artifactGav) throws MalformedURLException { Artifact artifact = new DefaultArtifact(artifactGav); if (artifact.getVersion() == null) { throw new IllegalArgumentException("Null version"); } VersionScheme versionScheme = new GenericVersionScheme(); try { versionScheme.parseVersion(artifact.getVersion()); } catch (InvalidVersionSpecificationException e) { throw new IllegalArgumentException(e); } try { versionScheme.parseVersionRange(artifact.getVersion()); throw new IllegalArgumentException( artifact.getVersion() + " is a version range. A specific version is needed"); } catch (InvalidVersionSpecificationException expected) { } RepositorySystemSession session = newRepositorySystemSession(); ArtifactRequest artifactRequest = new ArtifactRequest(); artifactRequest.setArtifact(artifact); for (RemoteRepository remoteRepo : remoteRepositories) { artifactRequest.addRepository(remoteRepo); } ArtifactResult artifactResult; try { artifactResult = REPOSITORY_SYSTEM.resolveArtifact(session, artifactRequest); } catch (ArtifactResolutionException e) { throw new RuntimeException(e); } File file = artifactResult.getArtifact().getFile().getAbsoluteFile(); return file.toURI().toURL(); }
List<URL> createMavenGavRecursiveURLs(String artifactGav, String... excludes) throws MalformedURLException, DependencyCollectionException, DependencyResolutionException { Artifact artifact = new DefaultArtifact(artifactGav); if (artifact.getVersion() == null) { throw new IllegalArgumentException("Null version"); } VersionScheme versionScheme = new GenericVersionScheme(); try { versionScheme.parseVersion(artifact.getVersion()); } catch (InvalidVersionSpecificationException e) { throw new IllegalArgumentException(e); } try { versionScheme.parseVersionRange(artifact.getVersion()); throw new IllegalArgumentException( artifact.getVersion() + " is a version range. A specific version is needed"); } catch (InvalidVersionSpecificationException expected) { } RepositorySystemSession session = newRepositorySystemSession(); ArtifactRequest artifactRequest = new ArtifactRequest(); artifactRequest.setArtifact(artifact); for (RemoteRepository remoteRepo : remoteRepositories) { artifactRequest.addRepository(remoteRepo); } ArtifactResult artifactResult; try { artifactResult = REPOSITORY_SYSTEM.resolveArtifact(session, artifactRequest); } catch (ArtifactResolutionException e) { throw new RuntimeException(e); } List<URL> urls = new ArrayList<>(); urls.add(artifactToUrl(artifactResult.getArtifact())); CollectRequest collectRequest = new CollectRequest(); collectRequest.setRoot(new Dependency(artifact, "compile")); for (RemoteRepository remoteRepo : remoteRepositories) { collectRequest.addRepository(remoteRepo); } DependencyNode node = REPOSITORY_SYSTEM.collectDependencies(session, collectRequest).getRoot(); DependencyFilter filter = new ExclusionsDependencyFilter(Arrays.asList(excludes)); DependencyRequest dependencyRequest = new DependencyRequest(node, filter); REPOSITORY_SYSTEM.resolveDependencies(session, dependencyRequest); PreorderNodeListGenerator nlg = new PreorderNodeListGenerator(); node.accept(nlg); for (Artifact cur : nlg.getArtifacts(false)) { urls.add(artifactToUrl(cur)); } log.debug("--------------------"); log.debug(nlg.getClassPath()); log.debug("--------------------"); return urls; }