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