]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.support/src/org/argeo/slc/aether/AetherTemplate.java
SLC Repo building
[gpl/argeo-slc.git] / org.argeo.slc.support / src / org / argeo / slc / aether / AetherTemplate.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.slc.aether;
17
18 import java.io.File;
19 import java.util.List;
20
21 import org.argeo.slc.SlcException;
22 import org.eclipse.aether.RepositorySystem;
23 import org.eclipse.aether.RepositorySystemSession;
24 import org.eclipse.aether.artifact.Artifact;
25 import org.eclipse.aether.collection.CollectRequest;
26 import org.eclipse.aether.graph.Dependency;
27 import org.eclipse.aether.graph.DependencyNode;
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
33 /** Simplifies access to Aether. */
34 public class AetherTemplate {
35 private RepositorySystem repositorySystem;
36 private RepositorySystemSession repositorySystemSession;
37 private List<RemoteRepository> remoteRepositories;
38
39 /** Resolves the artifact in order to give access to its file. */
40 public File getResolvedFile(Artifact artifact) {
41 try {
42 ArtifactRequest artifactRequest = new ArtifactRequest(artifact,
43 remoteRepositories, null);
44 ArtifactResult result = repositorySystem.resolveArtifact(
45 repositorySystemSession, artifactRequest);
46 return result.getArtifact().getFile();
47 } catch (ArtifactResolutionException e) {
48 throw new SlcException("Cannot resolve " + artifact, e);
49 }
50 }
51
52 /**
53 * Transitively resolves the dependencies of this artifact (with scope
54 * 'compile')
55 *
56 * @param artifact
57 * the artifact to resolve
58 */
59 public DependencyNode resolveDependencies(Artifact artifact) {
60 return resolveDependencies(artifact, "compile");
61 }
62
63 /**
64 * Transitively resolves the dependencies of this artifact.
65 *
66 * @param artifact
67 * the artifact to resolve
68 * @param scope
69 * the scope
70 */
71 public DependencyNode resolveDependencies(Artifact artifact, String scope) {
72 try {
73 Dependency dependency = new Dependency(artifact, scope);
74 CollectRequest collectRequest = new CollectRequest();
75 collectRequest.setRoot(dependency);
76 for (RemoteRepository remoteRepository : remoteRepositories)
77 collectRequest.addRepository(remoteRepository);
78 DependencyNode node = repositorySystem.collectDependencies(
79 repositorySystemSession, collectRequest).getRoot();
80
81 // FIXME adapt to changes in Aether
82 // repositorySystem.resolveDependencies(repositorySystemSession,
83 // node,
84 // null);
85 return node;
86 } catch (Exception e) {
87 throw new SlcException("Cannot resolve dependencies of " + artifact
88 + " (scope: " + scope + ")", e);
89 }
90 }
91
92 public RepositorySystem getRepositorySystem() {
93 return repositorySystem;
94 }
95
96 public void setRepositorySystem(RepositorySystem repositorySystem) {
97 this.repositorySystem = repositorySystem;
98 }
99
100 public RepositorySystemSession getRepositorySystemSession() {
101 return repositorySystemSession;
102 }
103
104 public void setRepositorySystemSession(
105 RepositorySystemSession repositorySystemSession) {
106 this.repositorySystemSession = repositorySystemSession;
107 }
108
109 public List<RemoteRepository> getRemoteRepositories() {
110 return remoteRepositories;
111 }
112
113 public void setRemoteRepositories(List<RemoteRepository> remoteRepositories) {
114 this.remoteRepositories = remoteRepositories;
115 }
116
117 }