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