public File downloadArtifact(String groupId, String artifactId, String packaging, String version) throws ArtifactResolutionException { LOGGER.debug("Downloading artifact asd"); RepositorySystem repositorySystem = newRepositorySystem(); RepositorySystemSession session = newSession(repositorySystem); ArtifactRequest artifactRequest = new ArtifactRequest(); artifactRequest.setArtifact( new DefaultArtifact(groupId, artifactId, packaging.toLowerCase(), version)); Builder builder = new RemoteRepository.Builder("central", "default", "http://repo.maven.apache.org/maven2"); artifactRequest.addRepository(builder.build()); ArtifactResult artifactResult = repositorySystem.resolveArtifact(session, artifactRequest); return artifactResult.getArtifact().getFile(); }
@Override public InputStream openStream() throws IOException { RepositorySystem repositorySystem; RepositoryConnectorProvider repositoryConnectorProvider; try { repositorySystem = this.plexusContainer.lookup(RepositorySystem.class); repositoryConnectorProvider = this.plexusContainer.lookup(RepositoryConnectorProvider.class); } catch (ComponentLookupException e) { throw new IOException("Failed to get org.sonatype.aether.RepositorySystem component", e); } RepositorySystemSession session = this.repository.createRepositorySystemSession(); RepositoryConnector connector; try { connector = repositoryConnectorProvider.newRepositoryConnector( session, this.repository.getRemoteRepository()); } catch (NoRepositoryConnectorException e) { throw new IOException("Failed to download artifact [" + this.artifact + "]", e); } ArtifactDownload download = new ArtifactDownload(); download.setArtifact(this.artifact); download.setRepositories(Arrays.asList(this.repository.getRemoteRepository())); try { connector.get(Arrays.asList(download), null); } finally { connector.close(); } // /////////////////////////////////////////////////////////////////////////////: ArtifactRequest artifactRequest = new ArtifactRequest(); artifactRequest.addRepository(this.repository.getRemoteRepository()); artifactRequest.setArtifact(this.artifact); ArtifactResult artifactResult; try { artifactResult = repositorySystem.resolveArtifact(session, artifactRequest); } catch (ArtifactResolutionException e) { throw new IOException("Failed to resolve artifact", e); } File aetherFile = artifactResult.getArtifact().getFile(); return new AetherExtensionFileInputStream(aetherFile); }
protected File resolveArtifact(Artifact artifact) throws MojoExecutionException, DependencyCollectionException { ArtifactRequest request = new ArtifactRequest(); request.setArtifact(artifact); request.setRepositories(remoteRepos); ArtifactResult result; try { result = repositorySystem.resolveArtifact(repoSession, request); } catch (ArtifactResolutionException e) { throw new MojoExecutionException(e.getMessage(), e); } return result.getArtifact().getFile(); }
public static Optional<File> resolveArtifact( String groupId, String artifactId, String version, Optional<String> type, Optional<String> classifier, Optional<List<Repository>> customRepositories) { Optional<File> result; LocalRepository localRepository = MavenSettingsUtil.getLocalRepository(MavenSettingsUtil.loadSettings()); RepositorySystem system = AetherBootstrap.newRepositorySystem(); RepositorySystemSession session = AetherBootstrap.newRepositorySystemSession( system, localRepository.getBasedir().getAbsolutePath()); Artifact artifact = new DefaultArtifact(groupId, artifactId, classifier.orNull(), type.orNull(), version); ArtifactRequest artifactRequest = new ArtifactRequest(); artifactRequest.setArtifact(artifact); artifactRequest.setRepositories( MavenSettingsUtil.getRemoteRepositories( MavenSettingsUtil.loadSettings(), customRepositories.orNull())); try { ArtifactResult artifactResult = system.resolveArtifact(session, artifactRequest); artifact = artifactResult.getArtifact(); if (artifact != null) { result = Optional.fromNullable(artifact.getFile()); } else { result = Optional.absent(); } } catch (ArtifactResolutionException e) { // TODO add error handling -> maybe throw an exception that indicates the error or return an // Optional result = Optional.absent(); } return result; }
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; }
private void resolveOld( Artifact artifact, List<ArtifactRepository> remoteRepositories, RepositorySystemSession session) throws ArtifactResolutionException, ArtifactNotFoundException { if (artifact == null) { return; } if (Artifact.SCOPE_SYSTEM.equals(artifact.getScope())) { File systemFile = artifact.getFile(); if (systemFile == null) { throw new ArtifactNotFoundException( "System artifact: " + artifact + " has no file attached", artifact); } if (!systemFile.exists()) { throw new ArtifactNotFoundException( "System artifact: " + artifact + " not found in path: " + systemFile, artifact); } if (!systemFile.isFile()) { throw new ArtifactNotFoundException( "System artifact: " + artifact + " is not a file: " + systemFile, artifact); } artifact.setResolved(true); return; } if (!artifact.isResolved()) { ArtifactResult result; try { ArtifactRequest artifactRequest = new ArtifactRequest(); artifactRequest.setArtifact(RepositoryUtils.toArtifact(artifact)); artifactRequest.setRepositories(RepositoryUtils.toRepos(remoteRepositories)); // Maven 2.x quirk: an artifact always points at the local repo, regardless whether resolved // or not LocalRepositoryManager lrm = session.getLocalRepositoryManager(); String path = lrm.getPathForLocalArtifact(artifactRequest.getArtifact()); artifact.setFile(new File(lrm.getRepository().getBasedir(), path)); result = repoSystem.resolveArtifact(session, artifactRequest); } catch (org.eclipse.aether.resolution.ArtifactResolutionException e) { if (e.getCause() instanceof org.eclipse.aether.transfer.ArtifactNotFoundException) { throw new ArtifactNotFoundException( e.getMessage(), artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(), artifact.getType(), artifact.getClassifier(), remoteRepositories, artifact.getDownloadUrl(), artifact.getDependencyTrail(), e); } else { throw new ArtifactResolutionException(e.getMessage(), artifact, remoteRepositories, e); } } artifact.selectVersion(result.getArtifact().getVersion()); artifact.setFile(result.getArtifact().getFile()); artifact.setResolved(true); if (artifact.isSnapshot()) { Matcher matcher = Artifact.VERSION_FILE_PATTERN.matcher(artifact.getVersion()); if (matcher.matches()) { Snapshot snapshot = new Snapshot(); snapshot.setTimestamp(matcher.group(2)); try { snapshot.setBuildNumber(Integer.parseInt(matcher.group(3))); artifact.addMetadata(new SnapshotArtifactRepositoryMetadata(artifact, snapshot)); } catch (NumberFormatException e) { logger.warn( "Invalid artifact version " + artifact.getVersion() + ": " + e.getMessage()); } } } } }
private Set<MavenRepositoryMetadata> getRepositoriesResolvingArtifact( final GAV gav, final MavenProject mavenProject) { ArtifactResult result = null; ArtifactRequest artifactRequest = null; final String artifactName = gav.toString(); final Artifact artifact = new DefaultArtifact(artifactName); final Aether aether = new Aether(mavenProject); final Set<MavenRepositoryMetadata> repositoriesResolvingArtifact = new HashSet<MavenRepositoryMetadata>(); final Map<MavenRepositorySource, Collection<RemoteRepository>> repositories = getRemoteRepositories(mavenProject); // Local Repository artifactRequest = new ArtifactRequest(); artifactRequest.setArtifact(artifact); try { result = aether.getSystem().resolveArtifact(aether.getSession(), artifactRequest); if (result != null && result.isResolved()) { final MavenRepositoryMetadata artifactRepositoryMetaData = makeRepositoryMetaData(result.getRepository(), MavenRepositorySource.LOCAL); if (artifactRepositoryMetaData != null) { repositoriesResolvingArtifact.add(artifactRepositoryMetaData); } } } catch (ArtifactResolutionException are) { // Ignore - this means the Artifact could not be resolved against the given RemoteRepository } // Remote Repositories try { for (Map.Entry<MavenRepositorySource, Collection<RemoteRepository>> e : repositories.entrySet()) { for (ArtifactRepository repository : e.getValue()) { artifactRequest = new ArtifactRequest(); artifactRequest.setArtifact(artifact); java.nio.file.Path tempLocalRepositoryBasePath = null; try { // Maven always tries to resolve against LocalRepository first, which is not much use // when we want to check // if the Artifact is available on a RemoteRepository. Therefore substitute the default // RepositorySystemSession // with one that provides a LocalRepositoryManager that always uses an empty transient // LocalRepository to ensure // Maven does not resolve Artifacts locally. artifactRequest.addRepository((RemoteRepository) repository); tempLocalRepositoryBasePath = getRepositoryPath(gav); result = aether .getSystem() .resolveArtifact( new MavenRepositorySystemSessionWrapper( tempLocalRepositoryBasePath.toString(), aether.getSession()), artifactRequest); if (result != null && result.isResolved()) { final MavenRepositoryMetadata artifactRepositoryMetaData = makeRepositoryMetaData(result.getRepository(), e.getKey()); if (artifactRepositoryMetaData != null) { repositoriesResolvingArtifact.add(artifactRepositoryMetaData); } } } catch (ArtifactResolutionException are) { // Ignore - this means the Artifact could not be resolved against the given // RemoteRepository } finally { tearDownMavenRepository(tempLocalRepositoryBasePath); } } } } catch (IOException ioe) { log.error( "Error resolving '" + gav.toString() + "' against Repositories. Returning empty Collection. ", ioe); } return repositoriesResolvingArtifact; }