]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.aether/src/main/java/org/argeo/slc/aether/AetherTemplate.java
SLC Repo
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.aether / src / main / java / org / argeo / slc / aether / AetherTemplate.java
1 package org.argeo.slc.aether;
2
3 import java.io.File;
4 import java.util.List;
5
6 import org.argeo.slc.SlcException;
7 import org.sonatype.aether.RepositorySystem;
8 import org.sonatype.aether.RepositorySystemSession;
9 import org.sonatype.aether.artifact.Artifact;
10 import org.sonatype.aether.collection.CollectRequest;
11 import org.sonatype.aether.graph.Dependency;
12 import org.sonatype.aether.graph.DependencyNode;
13 import org.sonatype.aether.repository.RemoteRepository;
14 import org.sonatype.aether.resolution.ArtifactRequest;
15 import org.sonatype.aether.resolution.ArtifactResolutionException;
16 import org.sonatype.aether.resolution.ArtifactResult;
17
18 /** Simplifies access to Aether. */
19 public class AetherTemplate {
20 private RepositorySystem repositorySystem;
21 private RepositorySystemSession repositorySystemSession;
22 private List<RemoteRepository> remoteRepositories;
23
24 /** Resolves the artifact in order to give access to its file. */
25 public File getResolvedFile(Artifact artifact) {
26 try {
27 ArtifactRequest artifactRequest = new ArtifactRequest(artifact,
28 remoteRepositories, null);
29 ArtifactResult result = repositorySystem.resolveArtifact(
30 repositorySystemSession, artifactRequest);
31 return result.getArtifact().getFile();
32 } catch (ArtifactResolutionException e) {
33 throw new SlcException("Cannot resolve " + artifact, e);
34 }
35 }
36
37 /**
38 * Transitively resolves the dependencies of this artifact (with scope
39 * 'compile')
40 *
41 * @param artifact
42 * the artifact to resolve
43 */
44 public DependencyNode resolveDependencies(Artifact artifact) {
45 return resolveDependencies(artifact, "compile");
46 }
47
48 /**
49 * Transitively resolves the dependencies of this artifact.
50 *
51 * @param artifact
52 * the artifact to resolve
53 * @param scope
54 * the scope
55 */
56 public DependencyNode resolveDependencies(Artifact artifact, String scope) {
57 try {
58 Dependency dependency = new Dependency(artifact, scope);
59 CollectRequest collectRequest = new CollectRequest();
60 collectRequest.setRoot(dependency);
61 for (RemoteRepository remoteRepository : remoteRepositories)
62 collectRequest.addRepository(remoteRepository);
63 DependencyNode node = repositorySystem.collectDependencies(
64 repositorySystemSession, collectRequest).getRoot();
65
66 repositorySystem.resolveDependencies(repositorySystemSession, node,
67 null);
68 return node;
69 } catch (Exception e) {
70 throw new SlcException("Cannot resolve dependencies of " + artifact
71 + " (scope: " + scope + ")", e);
72 }
73 }
74
75 public RepositorySystem getRepositorySystem() {
76 return repositorySystem;
77 }
78
79 public void setRepositorySystem(RepositorySystem repositorySystem) {
80 this.repositorySystem = repositorySystem;
81 }
82
83 public RepositorySystemSession getRepositorySystemSession() {
84 return repositorySystemSession;
85 }
86
87 public void setRepositorySystemSession(
88 RepositorySystemSession repositorySystemSession) {
89 this.repositorySystemSession = repositorySystemSession;
90 }
91
92 public List<RemoteRepository> getRemoteRepositories() {
93 return remoteRepositories;
94 }
95
96 public void setRemoteRepositories(List<RemoteRepository> remoteRepositories) {
97 this.remoteRepositories = remoteRepositories;
98 }
99
100 }