private MavenRepositoryMetadata makeRepositoryMetaData( final ArtifactRepository repository, final MavenRepositorySource source) { if (repository instanceof LocalRepository) { final LocalRepository localRepository = (LocalRepository) repository; return new MavenRepositoryMetadata( localRepository.getId(), MavenSettings.getSettings().getLocalRepository(), source); } else if (repository instanceof RemoteRepository) { final RemoteRepository remoteRepository = (RemoteRepository) repository; return new MavenRepositoryMetadata( remoteRepository.getId(), remoteRepository.getUrl(), source); } return null; }
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; }