1 | |
package org.apache.maven.report.projectinfo.dependencies; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import java.net.UnknownHostException; |
23 | |
import java.util.ArrayList; |
24 | |
import java.util.List; |
25 | |
|
26 | |
import org.apache.maven.artifact.Artifact; |
27 | |
import org.apache.maven.artifact.ArtifactUtils; |
28 | |
import org.apache.maven.artifact.factory.ArtifactFactory; |
29 | |
import org.apache.maven.artifact.manager.WagonConfigurationException; |
30 | |
import org.apache.maven.artifact.manager.WagonManager; |
31 | |
import org.apache.maven.artifact.metadata.ArtifactMetadata; |
32 | |
import org.apache.maven.artifact.repository.ArtifactRepository; |
33 | |
import org.apache.maven.artifact.repository.metadata.Metadata; |
34 | |
import org.apache.maven.artifact.repository.metadata.RepositoryMetadataManager; |
35 | |
import org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata; |
36 | |
import org.apache.maven.artifact.resolver.ArtifactNotFoundException; |
37 | |
import org.apache.maven.artifact.resolver.ArtifactResolutionException; |
38 | |
import org.apache.maven.artifact.resolver.ArtifactResolver; |
39 | |
import org.apache.maven.plugin.logging.Log; |
40 | |
import org.apache.maven.project.MavenProject; |
41 | |
import org.apache.maven.project.MavenProjectBuilder; |
42 | |
import org.apache.maven.project.ProjectBuildingException; |
43 | |
import org.apache.maven.settings.Proxy; |
44 | |
import org.apache.maven.settings.Settings; |
45 | |
import org.apache.maven.wagon.ConnectionException; |
46 | |
import org.apache.maven.wagon.TransferFailedException; |
47 | |
import org.apache.maven.wagon.UnsupportedProtocolException; |
48 | |
import org.apache.maven.wagon.Wagon; |
49 | |
import org.apache.maven.wagon.authentication.AuthenticationException; |
50 | |
import org.apache.maven.wagon.authentication.AuthenticationInfo; |
51 | |
import org.apache.maven.wagon.authorization.AuthorizationException; |
52 | |
import org.apache.maven.wagon.observers.Debug; |
53 | |
import org.apache.maven.wagon.proxy.ProxyInfo; |
54 | |
import org.apache.maven.wagon.repository.Repository; |
55 | |
import org.codehaus.plexus.util.StringUtils; |
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
public class RepositoryUtils |
64 | |
{ |
65 | 1 | private static final List<String> UNKNOWN_HOSTS = new ArrayList<String>(); |
66 | |
|
67 | |
private final Log log; |
68 | |
|
69 | |
private final WagonManager wagonManager; |
70 | |
|
71 | |
private final Settings settings; |
72 | |
|
73 | |
private final MavenProjectBuilder mavenProjectBuilder; |
74 | |
|
75 | |
private final ArtifactFactory factory; |
76 | |
|
77 | |
private final List<ArtifactRepository> remoteRepositories; |
78 | |
|
79 | |
private final List<ArtifactRepository> pluginRepositories; |
80 | |
|
81 | |
private final ArtifactResolver resolver; |
82 | |
|
83 | |
private final ArtifactRepository localRepository; |
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
public RepositoryUtils( Log log, WagonManager wagonManager, Settings settings, |
98 | |
MavenProjectBuilder mavenProjectBuilder, ArtifactFactory factory, |
99 | |
ArtifactResolver resolver, List<ArtifactRepository> remoteRepositories, |
100 | |
List<ArtifactRepository> pluginRepositories, ArtifactRepository localRepository, |
101 | |
RepositoryMetadataManager repositoryMetadataManager ) |
102 | 2 | { |
103 | 2 | this.log = log; |
104 | 2 | this.wagonManager = wagonManager; |
105 | 2 | this.settings = settings; |
106 | 2 | this.mavenProjectBuilder = mavenProjectBuilder; |
107 | 2 | this.factory = factory; |
108 | 2 | this.resolver = resolver; |
109 | 2 | this.remoteRepositories = remoteRepositories; |
110 | 2 | this.pluginRepositories = pluginRepositories; |
111 | 2 | this.localRepository = localRepository; |
112 | 2 | } |
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
public ArtifactRepository getLocalRepository() |
118 | |
{ |
119 | 0 | return localRepository; |
120 | |
} |
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
public List<ArtifactRepository> getRemoteArtifactRepositories() |
126 | |
{ |
127 | 0 | return remoteRepositories; |
128 | |
} |
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
public List<ArtifactRepository> getPluginArtifactRepositories() |
134 | |
{ |
135 | 0 | return pluginRepositories; |
136 | |
} |
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
public void resolve( Artifact artifact ) |
145 | |
throws ArtifactResolutionException, ArtifactNotFoundException |
146 | |
{ |
147 | 0 | List<ArtifactRepository> repos = |
148 | |
new ArrayList<ArtifactRepository>( pluginRepositories.size() + remoteRepositories.size() ); |
149 | 0 | repos.addAll( pluginRepositories ); |
150 | 0 | repos.addAll( remoteRepositories ); |
151 | |
|
152 | 0 | resolver.resolve( artifact, repos, localRepository ); |
153 | 0 | } |
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
public boolean dependencyExistsInRepo( ArtifactRepository repo, Artifact artifact ) |
162 | |
{ |
163 | 0 | if ( repo.isBlacklisted() ) |
164 | |
{ |
165 | 0 | if ( log.isDebugEnabled() ) |
166 | |
{ |
167 | 0 | log.debug( "The repo '" + repo.getId() + "' is black listed - Ignored it" ); |
168 | |
} |
169 | 0 | return false; |
170 | |
} |
171 | |
|
172 | 0 | if ( UNKNOWN_HOSTS.contains( repo.getUrl() ) ) |
173 | |
{ |
174 | 0 | if ( log.isDebugEnabled() ) |
175 | |
{ |
176 | 0 | log.debug( "The repo url '" + repo.getUrl() + "' is unknowned - Ignored it" ); |
177 | |
} |
178 | 0 | return false; |
179 | |
} |
180 | |
|
181 | 0 | repo = wagonManager.getMirrorRepository( repo ); |
182 | |
|
183 | 0 | String id = repo.getId(); |
184 | 0 | Repository repository = new Repository( id, repo.getUrl() ); |
185 | |
|
186 | |
Wagon wagon; |
187 | |
try |
188 | |
{ |
189 | 0 | wagon = wagonManager.getWagon( repository ); |
190 | |
} |
191 | 0 | catch ( UnsupportedProtocolException e ) |
192 | |
{ |
193 | 0 | log.error( "Unsupported protocol: '" + repo.getProtocol() + "'", e ); |
194 | 0 | return false; |
195 | |
} |
196 | 0 | catch ( WagonConfigurationException e ) |
197 | |
{ |
198 | 0 | log.error( "Unsupported protocol: '" + repo.getProtocol() + "'", e ); |
199 | 0 | return false; |
200 | 0 | } |
201 | |
|
202 | 0 | wagon.setTimeout( 1000 ); |
203 | |
|
204 | 0 | if ( log.isDebugEnabled() ) |
205 | |
{ |
206 | 0 | Debug debug = new Debug(); |
207 | |
|
208 | 0 | wagon.addSessionListener( debug ); |
209 | 0 | wagon.addTransferListener( debug ); |
210 | |
} |
211 | |
|
212 | |
try |
213 | |
{ |
214 | |
|
215 | 0 | AuthenticationInfo auth = wagonManager.getAuthenticationInfo( repo.getId() ); |
216 | |
|
217 | 0 | ProxyInfo proxyInfo = getProxyInfo(); |
218 | 0 | if ( proxyInfo != null ) |
219 | |
{ |
220 | 0 | wagon.connect( repository, auth, proxyInfo ); |
221 | |
} |
222 | |
else |
223 | |
{ |
224 | 0 | wagon.connect( repository, auth ); |
225 | |
} |
226 | |
|
227 | 0 | return wagon.resourceExists( StringUtils.replace( getDependencyUrlFromRepository( artifact, repo ), |
228 | |
repo.getUrl(), "" ) ); |
229 | |
} |
230 | 0 | catch ( ConnectionException e ) |
231 | |
{ |
232 | 0 | if ( log.isDebugEnabled() ) |
233 | |
{ |
234 | 0 | log.error( "Unable to connect to: " + repo.getUrl(), e ); |
235 | |
} |
236 | |
else |
237 | |
{ |
238 | 0 | log.error( "Unable to connect to: " + repo.getUrl() ); |
239 | |
} |
240 | 0 | return false; |
241 | |
} |
242 | 0 | catch ( AuthenticationException e ) |
243 | |
{ |
244 | 0 | if ( log.isDebugEnabled() ) |
245 | |
{ |
246 | 0 | log.error( "Unable to connect to: " + repo.getUrl(), e ); |
247 | |
} |
248 | |
else |
249 | |
{ |
250 | 0 | log.error( "Unable to connect to: " + repo.getUrl() ); |
251 | |
} |
252 | 0 | return false; |
253 | |
} |
254 | 0 | catch ( TransferFailedException e ) |
255 | |
{ |
256 | 0 | if ( e.getCause() instanceof UnknownHostException ) |
257 | |
{ |
258 | 0 | log.error( "Unknown host " + e.getCause().getMessage() + " - ignored it" ); |
259 | 0 | UNKNOWN_HOSTS.add( repo.getUrl() ); |
260 | |
} |
261 | |
else |
262 | |
{ |
263 | 0 | if ( log.isDebugEnabled() ) |
264 | |
{ |
265 | 0 | log.error( "Unable to determine if resource " + artifact + " exists in " + repo.getUrl(), e ); |
266 | |
} |
267 | |
else |
268 | |
{ |
269 | 0 | log.error( "Unable to determine if resource " + artifact + " exists in " + repo.getUrl() ); |
270 | |
} |
271 | |
} |
272 | 0 | return false; |
273 | |
} |
274 | 0 | catch ( AuthorizationException e ) |
275 | |
{ |
276 | 0 | if ( log.isDebugEnabled() ) |
277 | |
{ |
278 | 0 | log.error( "Unable to connect to: " + repo.getUrl(), e ); |
279 | |
} |
280 | |
else |
281 | |
{ |
282 | 0 | log.error( "Unable to connect to: " + repo.getUrl() ); |
283 | |
} |
284 | 0 | return false; |
285 | |
} |
286 | 0 | catch ( AbstractMethodError e ) |
287 | |
{ |
288 | 0 | log.error( "Wagon " + wagon.getClass().getName() + " does not support the resourceExists method" ); |
289 | 0 | return false; |
290 | |
} |
291 | |
finally |
292 | |
{ |
293 | 0 | try |
294 | |
{ |
295 | 0 | wagon.disconnect(); |
296 | |
} |
297 | 0 | catch ( ConnectionException e ) |
298 | |
{ |
299 | 0 | if ( log.isDebugEnabled() ) |
300 | |
{ |
301 | 0 | log.error( "Error disconnecting wagon - ignored", e ); |
302 | |
} |
303 | |
else |
304 | |
{ |
305 | 0 | log.error( "Error disconnecting wagon - ignored" ); |
306 | |
} |
307 | 0 | } |
308 | |
} |
309 | |
} |
310 | |
|
311 | |
|
312 | |
|
313 | |
|
314 | |
|
315 | |
|
316 | |
|
317 | |
|
318 | |
public MavenProject getMavenProjectFromRepository( Artifact artifact ) |
319 | |
throws ProjectBuildingException |
320 | |
{ |
321 | 4 | Artifact projectArtifact = artifact; |
322 | |
|
323 | 4 | boolean allowStubModel = false; |
324 | 4 | if ( !"pom".equals( artifact.getType() ) ) |
325 | |
{ |
326 | 3 | projectArtifact = factory.createProjectArtifact( artifact.getGroupId(), artifact.getArtifactId(), |
327 | |
artifact.getVersion(), artifact.getScope() ); |
328 | 3 | allowStubModel = true; |
329 | |
} |
330 | |
|
331 | |
|
332 | 4 | return mavenProjectBuilder.buildFromRepository( projectArtifact, remoteRepositories, localRepository, |
333 | |
allowStubModel ); |
334 | |
} |
335 | |
|
336 | |
|
337 | |
|
338 | |
|
339 | |
|
340 | |
|
341 | |
|
342 | |
public String getDependencyUrlFromRepository( Artifact artifact, ArtifactRepository repo ) |
343 | |
{ |
344 | 0 | if ( repo.isBlacklisted() ) |
345 | |
{ |
346 | 0 | return null; |
347 | |
} |
348 | |
|
349 | 0 | Artifact copyArtifact = ArtifactUtils.copyArtifact( artifact ); |
350 | |
|
351 | 0 | if ( ( artifact.isSnapshot() && repo.getSnapshots().isEnabled() ) ) |
352 | |
{ |
353 | 0 | if ( artifact.getBaseVersion().equals( artifact.getVersion() ) ) |
354 | |
{ |
355 | |
|
356 | 0 | if ( artifact.getMetadataList() == null || artifact.getMetadataList().isEmpty() ) |
357 | |
{ |
358 | |
try |
359 | |
{ |
360 | 0 | resolve( artifact ); |
361 | |
} |
362 | 0 | catch ( ArtifactResolutionException e ) |
363 | |
{ |
364 | 0 | log.error( "Artifact: " + artifact.getId() + " could not be resolved." ); |
365 | |
} |
366 | 0 | catch ( ArtifactNotFoundException e ) |
367 | |
{ |
368 | 0 | log.error( "Artifact: " + artifact.getId() + " was not found." ); |
369 | 0 | } |
370 | |
} |
371 | |
|
372 | 0 | for ( ArtifactMetadata m : artifact.getMetadataList() ) |
373 | |
{ |
374 | 0 | if ( m instanceof SnapshotArtifactRepositoryMetadata ) |
375 | |
{ |
376 | 0 | SnapshotArtifactRepositoryMetadata snapshotMetadata = (SnapshotArtifactRepositoryMetadata) m; |
377 | |
|
378 | 0 | Metadata metadata = snapshotMetadata.getMetadata(); |
379 | 0 | if ( metadata.getVersioning() == null || metadata.getVersioning().getSnapshot() == null |
380 | |
|| metadata.getVersioning().getSnapshot().isLocalCopy() |
381 | |
|| metadata.getVersioning().getSnapshot().getTimestamp() == null ) |
382 | |
{ |
383 | 0 | continue; |
384 | |
} |
385 | |
|
386 | |
|
387 | 0 | String version = |
388 | |
StringUtils.replace( copyArtifact.getVersion(), Artifact.SNAPSHOT_VERSION, |
389 | |
metadata.getVersioning().getSnapshot().getTimestamp() ) |
390 | |
+ "-" + metadata.getVersioning().getSnapshot().getBuildNumber(); |
391 | 0 | copyArtifact.setVersion( version ); |
392 | 0 | } |
393 | |
} |
394 | |
} |
395 | |
} |
396 | |
|
397 | 0 | return repo.getUrl() + "/" + repo.pathOf( copyArtifact ); |
398 | |
} |
399 | |
|
400 | |
|
401 | |
|
402 | |
|
403 | |
|
404 | |
|
405 | |
|
406 | |
|
407 | |
|
408 | |
|
409 | |
|
410 | |
private ProxyInfo getProxyInfo() |
411 | |
{ |
412 | 0 | ProxyInfo proxyInfo = null; |
413 | 0 | if ( settings != null && settings.getActiveProxy() != null ) |
414 | |
{ |
415 | 0 | Proxy settingsProxy = settings.getActiveProxy(); |
416 | |
|
417 | 0 | proxyInfo = new ProxyInfo(); |
418 | 0 | proxyInfo.setHost( settingsProxy.getHost() ); |
419 | 0 | proxyInfo.setType( settingsProxy.getProtocol() ); |
420 | 0 | proxyInfo.setPort( settingsProxy.getPort() ); |
421 | 0 | proxyInfo.setNonProxyHosts( settingsProxy.getNonProxyHosts() ); |
422 | 0 | proxyInfo.setUserName( settingsProxy.getUsername() ); |
423 | 0 | proxyInfo.setPassword( settingsProxy.getPassword() ); |
424 | |
} |
425 | |
|
426 | 0 | return proxyInfo; |
427 | |
} |
428 | |
} |