2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.odlparent;
12 import java.util.ArrayList;
13 import java.util.LinkedHashSet;
14 import java.util.List;
16 import org.eclipse.aether.DefaultRepositorySystemSession;
17 import org.eclipse.aether.RepositorySystem;
18 import org.eclipse.aether.RepositorySystemSession;
19 import org.eclipse.aether.artifact.Artifact;
20 import org.eclipse.aether.artifact.DefaultArtifact;
21 import org.eclipse.aether.collection.CollectRequest;
22 import org.eclipse.aether.graph.Dependency;
23 import org.eclipse.aether.graph.DependencyFilter;
24 import org.eclipse.aether.installation.InstallationException;
25 import org.eclipse.aether.installation.InstallRequest;
26 import org.eclipse.aether.repository.LocalRepository;
27 import org.eclipse.aether.repository.LocalRepositoryManager;
28 import org.eclipse.aether.repository.RemoteRepository;
29 import org.eclipse.aether.resolution.ArtifactRequest;
30 import org.eclipse.aether.resolution.ArtifactResolutionException;
31 import org.eclipse.aether.resolution.ArtifactResult;
32 import org.eclipse.aether.resolution.DependencyRequest;
33 import org.eclipse.aether.resolution.DependencyResolutionException;
34 import org.eclipse.aether.resolution.DependencyResult;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
38 public class AetherUtil {
39 private static final Logger LOG = LoggerFactory.getLogger(AetherUtil.class);
40 private RepositorySystem repoSystem;
42 private RepositorySystemSession repoSession;
44 private List<RemoteRepository> remoteRepos;
46 protected File localRepository;
49 * Create an instance for the given repositories.
51 * @param repoSystem The repository system.
52 * @param repoSession The repository session.
53 * @param remoteRepos The remote repositories.
54 * @param localRepository The local repository.
57 RepositorySystem repoSystem, RepositorySystemSession repoSession, List<RemoteRepository> remoteRepos,
58 File localRepository) {
59 this.repoSystem = repoSystem;
60 this.repoSession = repoSession;
61 this.remoteRepos = remoteRepos;
62 this.localRepository = localRepository;
66 * Resolves the given dependencies.
68 * @param dependencies The dependencies.
69 * @param filter The dependency filter.
70 * @return The corresponding artifacts.
71 * @throws DependencyResolutionException if an error occurs.
73 public Set<Artifact> resolveDependencies(List<Dependency> dependencies, DependencyFilter filter)
74 throws DependencyResolutionException {
75 Set<Artifact> artifacts = new LinkedHashSet<Artifact>();
76 CollectRequest collectRequest = new CollectRequest();
77 collectRequest.setDependencies(dependencies);
78 collectRequest.setRepositories(remoteRepos);
79 DependencyRequest request = new DependencyRequest(collectRequest, filter);
80 DependencyResult results = repoSystem.resolveDependencies(repoSession, request);
81 for (ArtifactResult artifactResult : results.getArtifactResults()) {
82 artifacts.add(artifactResult.getArtifact());
84 LOG.trace("resolveDependencies({}) returns {}", dependencies, artifacts);
89 * Resolves the given artifact.
91 * @param artifact The artifact.
92 * @return The resolved artifact, or {@code null} if it can't be resolved.
94 public Artifact resolveArtifact(Artifact artifact) {
95 ArtifactRequest request = new ArtifactRequest(artifact, remoteRepos, null);
96 ArtifactResult result;
98 result = repoSystem.resolveArtifact(repoSession, request);
99 } catch (ArtifactResolutionException e) {
100 LOG.warn("Unable to resolve artifact: {}", e.getMessage(), e);
103 LOG.trace("resolveArtifacts({}) returns {}", artifact, result.getArtifact());
104 return result.getArtifact();
108 * Resolves the given coordinates.
110 * @param coord The coordinates to resolve.
111 * @return The resolved artifact, or {@code null} if the coordinates can't be resolved.
113 public Artifact resolveArtifact(String coord) {
114 DefaultArtifact artifact = new DefaultArtifact(coord);
115 return resolveArtifact(artifact);
119 * Resolves the given coordinates.
121 * @param coords The set of coordinates to resolve.
122 * @return The resolved artifacts. Unresolvable coordinates are skipped without error.
124 public Set<Artifact> resolveArtifacts(Set<String> coords) {
125 Set<Artifact> result = new LinkedHashSet<Artifact>();
126 for (String coord : coords) {
127 Artifact artifact = resolveArtifact(coord);
128 if (artifact != null) {
129 result.add(artifact);
132 LOG.trace("resolveArtifacts({}) returns {}", coords, result);
137 * Converts the given artifact coordinates to a {@link Dependency} instance.
139 * @param coord The coordinates.
140 * @return The dependency.
142 public Dependency toDependency(String coord) {
143 Artifact artifact = new DefaultArtifact(coord);
144 return new Dependency(artifact, null);
148 * Converts the given list of artifact coordinates to dependencies.
150 * @param coords The list of coordinates.
151 * @return The corresponding dependencies.
153 public List<Dependency> toDependencies(List<String> coords) {
154 List<Dependency> result = new ArrayList<Dependency>();
155 for (String coord : coords) {
156 result.add(toDependency(coord));
158 LOG.trace("toDependencies({}) returns {}", coords, result);
163 * Installs the given artifacts.
165 * @param artifacts The artifacts to install.
166 * @throws InstallationException if an error occurs.
168 public void installArtifacts(Set<Artifact> artifacts) throws InstallationException {
169 LocalRepository localRepo = new LocalRepository(localRepository);
170 LocalRepositoryManager localManager = repoSystem.newLocalRepositoryManager(repoSession, localRepo);
171 DefaultRepositorySystemSession localSession = new DefaultRepositorySystemSession();
172 localSession.setLocalRepositoryManager(localManager);
173 InstallRequest installRequest = new InstallRequest();
174 for (Artifact featureArtifact : artifacts) {
175 installRequest.addArtifact(featureArtifact);
177 repoSystem.install(localSession, installRequest);