2 * Copyright (C) 2007-2012 Argeo GmbH
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org
.argeo
.slc
.aether
;
19 import java
.util
.List
;
21 import org
.argeo
.slc
.SlcException
;
22 import org
.sonatype
.aether
.RepositorySystem
;
23 import org
.sonatype
.aether
.RepositorySystemSession
;
24 import org
.sonatype
.aether
.artifact
.Artifact
;
25 import org
.sonatype
.aether
.collection
.CollectRequest
;
26 import org
.sonatype
.aether
.graph
.Dependency
;
27 import org
.sonatype
.aether
.graph
.DependencyNode
;
28 import org
.sonatype
.aether
.repository
.RemoteRepository
;
29 import org
.sonatype
.aether
.resolution
.ArtifactRequest
;
30 import org
.sonatype
.aether
.resolution
.ArtifactResolutionException
;
31 import org
.sonatype
.aether
.resolution
.ArtifactResult
;
33 /** Simplifies access to Aether. */
34 public class AetherTemplate
{
35 private RepositorySystem repositorySystem
;
36 private RepositorySystemSession repositorySystemSession
;
37 private List
<RemoteRepository
> remoteRepositories
;
39 /** Resolves the artifact in order to give access to its file. */
40 public File
getResolvedFile(Artifact artifact
) {
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
);
53 * Transitively resolves the dependencies of this artifact (with scope
57 * the artifact to resolve
59 public DependencyNode
resolveDependencies(Artifact artifact
) {
60 return resolveDependencies(artifact
, "compile");
64 * Transitively resolves the dependencies of this artifact.
67 * the artifact to resolve
71 public DependencyNode
resolveDependencies(Artifact artifact
, String scope
) {
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();
81 repositorySystem
.resolveDependencies(repositorySystemSession
, node
,
84 } catch (Exception e
) {
85 throw new SlcException("Cannot resolve dependencies of " + artifact
86 + " (scope: " + scope
+ ")", e
);
90 public RepositorySystem
getRepositorySystem() {
91 return repositorySystem
;
94 public void setRepositorySystem(RepositorySystem repositorySystem
) {
95 this.repositorySystem
= repositorySystem
;
98 public RepositorySystemSession
getRepositorySystemSession() {
99 return repositorySystemSession
;
102 public void setRepositorySystemSession(
103 RepositorySystemSession repositorySystemSession
) {
104 this.repositorySystemSession
= repositorySystemSession
;
107 public List
<RemoteRepository
> getRemoteRepositories() {
108 return remoteRepositories
;
111 public void setRemoteRepositories(List
<RemoteRepository
> remoteRepositories
) {
112 this.remoteRepositories
= remoteRepositories
;