1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.nuxeo.build.maven.graph;
20
21 import java.util.List;
22
23 import org.apache.maven.RepositoryUtils;
24 import org.apache.maven.artifact.handler.ArtifactHandler;
25 import org.apache.tools.ant.BuildException;
26 import org.apache.tools.ant.Project;
27 import org.eclipse.aether.artifact.Artifact;
28 import org.eclipse.aether.collection.DependencyManagement;
29 import org.eclipse.aether.graph.Dependency;
30 import org.eclipse.aether.graph.DependencyNode;
31 import org.eclipse.aether.repository.RemoteRepository;
32 import org.eclipse.aether.resolution.ArtifactRequest;
33 import org.eclipse.aether.resolution.ArtifactResolutionException;
34 import org.eclipse.aether.resolution.ArtifactResult;
35 import org.eclipse.aether.resolution.DependencyRequest;
36 import org.eclipse.aether.resolution.DependencyResolutionException;
37 import org.eclipse.aether.resolution.DependencyResult;
38 import org.eclipse.aether.resolution.VersionRangeRequest;
39 import org.eclipse.aether.resolution.VersionRangeResolutionException;
40 import org.eclipse.aether.resolution.VersionRangeResult;
41 import org.eclipse.aether.util.artifact.ArtifactIdUtils;
42 import org.nuxeo.build.ant.AntClient;
43 import org.nuxeo.build.maven.AntBuildMojo;
44 import org.nuxeo.build.maven.filter.Filter;
45
46
47
48
49
50
51
52 public class DependencyUtils {
53
54 private DependencyUtils() {
55 }
56
57 public static org.apache.maven.artifact.Artifact aetherToMaven(Artifact aetherArtifact, String scope,
58 ArtifactHandler artifactHandler) {
59 org.apache.maven.artifact.Artifact mavenArtifact = new org.apache.maven.artifact.DefaultArtifact(
60 aetherArtifact.getGroupId(), aetherArtifact.getArtifactId(), aetherArtifact.getVersion(), scope,
61 aetherArtifact.getExtension(), aetherArtifact.getClassifier(), artifactHandler);
62 mavenArtifact.setFile(aetherArtifact.getFile());
63 mavenArtifact.setResolved(aetherArtifact.getFile() != null);
64 return mavenArtifact;
65 }
66
67 public static org.apache.maven.artifact.Artifact toMavenArtifact(Dependency dependency) {
68 return aetherToMaven(
69 dependency.getArtifact(),
70 dependency.getScope(),
71 AntBuildMojo.getInstance()
72 .getArtifactHandlerManager()
73 .getArtifactHandler(dependency.getArtifact().getExtension()));
74 }
75
76 public static org.apache.maven.artifact.Artifact aetherToMaven(Artifact aetherArtifact, String scope) {
77 return aetherToMaven(aetherArtifact, scope, AntBuildMojo.getInstance()
78 .getArtifactHandlerManager()
79 .getArtifactHandler(aetherArtifact.getExtension()));
80 }
81
82
83
84
85 @Deprecated
86 public static Artifact mavenToAether(org.apache.maven.artifact.Artifact artifact) {
87 return RepositoryUtils.toArtifact(artifact);
88 }
89
90
91
92
93 public static Artifact resolve(Artifact artifact) throws org.eclipse.aether.resolution.ArtifactResolutionException {
94 AntBuildMojo mojo = AntBuildMojo.getInstance();
95 return resolve(artifact, mojo.getRemoteRepositories());
96 }
97
98
99
100
101 public static Artifact resolve(Artifact artifact, List<RemoteRepository> remoteRepositories)
102 throws org.eclipse.aether.resolution.ArtifactResolutionException {
103 AntBuildMojo mojo = AntBuildMojo.getInstance();
104 ArtifactResult result = mojo.getSystem().resolveArtifact(mojo.getSession(),
105 new ArtifactRequest(artifact, remoteRepositories, null));
106 artifact = result.getArtifact();
107 AntClient.getInstance().log(artifact + " resolved to " + artifact.getFile(), Project.MSG_DEBUG);
108 return artifact;
109 }
110
111
112
113
114
115
116 @Deprecated
117 public static void resolve(org.apache.maven.artifact.Artifact artifact) throws ArtifactResolutionException {
118 resolve(RepositoryUtils.toArtifact(artifact));
119 }
120
121
122
123
124
125
126
127
128
129 @Deprecated
130 public static Dependency mavenToDependency(org.apache.maven.artifact.Artifact artifact) {
131
132
133 return new Dependency(RepositoryUtils.toArtifact(artifact), artifact.getScope(), artifact.isOptional());
134 }
135
136
137
138
139
140
141 public static Dependency getManagedDependency(Dependency dependency) {
142 AntBuildMojo mojo = AntBuildMojo.getInstance();
143 DependencyManagement depMgt = mojo.getSession().getDependencyManager().manageDependency(dependency);
144 if (depMgt != null) {
145 if (depMgt.getVersion() != null) {
146 Artifact artifact = dependency.getArtifact();
147 dependency = dependency.setArtifact(artifact.setVersion(depMgt.getVersion()));
148 }
149 if (depMgt.getProperties() != null) {
150 Artifact artifact = dependency.getArtifact();
151 dependency = dependency.setArtifact(artifact.setProperties(depMgt.getProperties()));
152 }
153 if (depMgt.getScope() != null) {
154 dependency = dependency.setScope(depMgt.getScope());
155 }
156 if (depMgt.getOptional() != null) {
157 dependency = dependency.setOptional(depMgt.getOptional());
158 }
159 if (depMgt.getExclusions() != null) {
160 dependency = dependency.setExclusions(depMgt.getExclusions());
161 }
162 }
163 return dependency;
164 }
165
166
167
168
169
170
171 public static Artifact setManagedVersion(Artifact artifact) {
172 AntBuildMojo mojo = AntBuildMojo.getInstance();
173 List<org.apache.maven.model.Dependency> managedDeps = AntBuildMojo.getInstance()
174 .getProject()
175 .getDependencyManagement()
176 .getDependencies();
177 for (org.apache.maven.model.Dependency dependency : managedDeps) {
178 Artifact managedArtifact = RepositoryUtils.toDependency(dependency,
179 mojo.getSession().getArtifactTypeRegistry()).getArtifact();
180 if (ArtifactIdUtils.equalsVersionlessId(managedArtifact, artifact)) {
181 artifact = artifact.setVersion(managedArtifact.getVersion());
182 AntClient.getInstance().log("Managed version set on " + artifact);
183 break;
184 }
185 }
186 return artifact;
187 }
188
189
190
191
192
193
194 public static Artifact setNewestVersion(Artifact artifact) {
195 AntBuildMojo mojo = AntBuildMojo.getInstance();
196 artifact = artifact.setVersion("[0,)");
197 VersionRangeRequest rangeRequest = new VersionRangeRequest(artifact, AntBuildMojo.getInstance()
198 .getRemoteRepositories(), null);
199 try {
200 VersionRangeResult rangeResult = mojo.getSystem().resolveVersionRange(mojo.getSession(), rangeRequest);
201 AntClient.getInstance().log(
202 String.format("Versions found for %s: %s", artifact, rangeResult.getVersions()), Project.MSG_DEBUG);
203 artifact = artifact.setVersion(rangeResult.getHighestVersion().toString());
204 AntClient.getInstance().log("Highest version found set on " + artifact);
205 } catch (VersionRangeResolutionException e) {
206 AntClient.getInstance().log(e.getMessage(), e, Project.MSG_ERR);
207 }
208 return artifact;
209 }
210
211
212
213
214 public static DependencyResult resolveDependencies(DependencyNode node, Filter filter, int depth) {
215 AntBuildMojo mojo = AntBuildMojo.getInstance();
216 AntClient.getInstance().log(String.format("Resolving %s with filter %s and depth %d", node, filter, depth),
217 Project.MSG_DEBUG);
218 DependencyRequest dependencyRequest = new DependencyRequest(node, filter);
219 try {
220 DependencyResult result = mojo.getSystem().resolveDependencies(mojo.getSession(), dependencyRequest);
221 AntClient.getInstance().log("Dependency result: " + result, new Error(), Project.MSG_DEBUG);
222 AntClient.getInstance().log("Dependency exceptions: " + result.getCollectExceptions(), Project.MSG_DEBUG);
223 return result;
224 } catch (DependencyResolutionException e) {
225 throw new BuildException("Cannot resolve dependency tree for " + node, e);
226 }
227 }
228
229 }