1 package org.apache.maven.bridge;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.Collections;
26 import java.util.HashMap;
27 import java.util.LinkedHashMap;
28 import java.util.List;
29 import java.util.Map;
30
31 import org.apache.maven.RepositoryUtils;
32 import org.apache.maven.artifact.Artifact;
33 import org.apache.maven.artifact.DefaultArtifact;
34 import org.apache.maven.artifact.InvalidRepositoryException;
35 import org.apache.maven.artifact.handler.ArtifactHandler;
36 import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
37 import org.apache.maven.artifact.repository.ArtifactRepository;
38 import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
39 import org.apache.maven.artifact.repository.Authentication;
40 import org.apache.maven.artifact.repository.MavenArtifactRepository;
41 import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
42 import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout2;
43 import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout;
44 import org.apache.maven.artifact.resolver.filter.ExcludesArtifactFilter;
45 import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
46 import org.apache.maven.artifact.versioning.VersionRange;
47 import org.apache.maven.model.Dependency;
48 import org.apache.maven.model.Exclusion;
49 import org.apache.maven.model.Plugin;
50 import org.apache.maven.repository.Proxy;
51 import org.apache.maven.settings.Mirror;
52 import org.apache.maven.settings.Server;
53 import org.apache.maven.settings.building.SettingsProblem;
54 import org.apache.maven.settings.crypto.DefaultSettingsDecryptionRequest;
55 import org.apache.maven.settings.crypto.SettingsDecrypter;
56 import org.apache.maven.settings.crypto.SettingsDecryptionRequest;
57 import org.apache.maven.settings.crypto.SettingsDecryptionResult;
58 import org.codehaus.plexus.PlexusContainer;
59 import org.codehaus.plexus.component.annotations.Component;
60 import org.codehaus.plexus.component.annotations.Requirement;
61 import org.codehaus.plexus.logging.Logger;
62 import org.codehaus.plexus.util.StringUtils;
63 import org.eclipse.aether.RepositorySystemSession;
64 import org.eclipse.aether.impl.ArtifactResolver;
65 import org.eclipse.aether.repository.AuthenticationContext;
66 import org.eclipse.aether.repository.AuthenticationSelector;
67 import org.eclipse.aether.repository.ProxySelector;
68 import org.eclipse.aether.repository.RemoteRepository;
69
70
71
72
73 @Component( role = MavenRepositorySystem.class, hint = "default" )
74 public class MavenRepositorySystem
75 {
76
77 @Requirement
78 private Logger logger;
79
80 @Requirement
81 private ArtifactHandlerManager artifactHandlerManager;
82
83 @Requirement
84 private ArtifactResolver artifactResolver;
85
86 @Requirement( role = ArtifactRepositoryLayout.class )
87 private Map<String, ArtifactRepositoryLayout> layouts;
88
89 @Requirement
90 private PlexusContainer plexus;
91
92 @Requirement
93 private SettingsDecrypter settingsDecrypter;
94
95
96 public Artifact createArtifact( String groupId, String artifactId, String version, String scope, String type )
97 {
98 return XcreateArtifact( groupId, artifactId, version, scope, type );
99 }
100
101
102 public Artifact createProjectArtifact( String groupId, String artifactId, String metaVersionId )
103 {
104 return XcreateProjectArtifact( groupId, artifactId, metaVersionId );
105 }
106
107
108 public Artifact createDependencyArtifact( Dependency d )
109 {
110 if ( d.getVersion() == null )
111 {
112 return null;
113 }
114
115 VersionRange versionRange;
116 try
117 {
118 versionRange = VersionRange.createFromVersionSpec( d.getVersion() );
119 }
120 catch ( InvalidVersionSpecificationException e )
121 {
122 return null;
123 }
124
125 Artifact artifact =
126 XcreateDependencyArtifact( d.getGroupId(), d.getArtifactId(), versionRange, d.getType(),
127 d.getClassifier(), d.getScope(), d.isOptional() );
128
129 if ( Artifact.SCOPE_SYSTEM.equals( d.getScope() ) && d.getSystemPath() != null )
130 {
131 artifact.setFile( new File( d.getSystemPath() ) );
132 }
133
134 if ( !d.getExclusions().isEmpty() )
135 {
136 List<String> exclusions = new ArrayList<String>();
137
138 for ( Exclusion exclusion : d.getExclusions() )
139 {
140 exclusions.add( exclusion.getGroupId() + ':' + exclusion.getArtifactId() );
141 }
142
143 artifact.setDependencyFilter( new ExcludesArtifactFilter( exclusions ) );
144 }
145
146 return artifact;
147 }
148
149
150 public Artifact createExtensionArtifact( String groupId, String artifactId, String version )
151 {
152 VersionRange versionRange;
153 try
154 {
155 versionRange = VersionRange.createFromVersionSpec( version );
156 }
157 catch ( InvalidVersionSpecificationException e )
158 {
159 return null;
160 }
161
162 return XcreateExtensionArtifact( groupId, artifactId, versionRange );
163 }
164
165
166 public Artifact createParentArtifact( String groupId, String artifactId, String version )
167 {
168 return XcreateParentArtifact( groupId, artifactId, version );
169 }
170
171
172 public Artifact createPluginArtifact( Plugin plugin )
173 {
174 VersionRange versionRange;
175 try
176 {
177 String version = plugin.getVersion();
178 if ( StringUtils.isEmpty( version ) )
179 {
180 version = "RELEASE";
181 }
182 versionRange = VersionRange.createFromVersionSpec( version );
183 }
184 catch ( InvalidVersionSpecificationException e )
185 {
186 return null;
187 }
188
189 return XcreatePluginArtifact( plugin.getGroupId(), plugin.getArtifactId(), versionRange );
190 }
191
192 public List<ArtifactRepository> getEffectiveRepositories( List<ArtifactRepository> repositories )
193 {
194 if ( repositories == null )
195 {
196 return null;
197 }
198
199 Map<String, List<ArtifactRepository>> reposByKey = new LinkedHashMap<String, List<ArtifactRepository>>();
200
201 for ( ArtifactRepository repository : repositories )
202 {
203 String key = repository.getId();
204
205 List<ArtifactRepository> aliasedRepos = reposByKey.get( key );
206
207 if ( aliasedRepos == null )
208 {
209 aliasedRepos = new ArrayList<ArtifactRepository>();
210 reposByKey.put( key, aliasedRepos );
211 }
212
213 aliasedRepos.add( repository );
214 }
215
216 List<ArtifactRepository> effectiveRepositories = new ArrayList<ArtifactRepository>();
217
218 for ( List<ArtifactRepository> aliasedRepos : reposByKey.values() )
219 {
220 List<ArtifactRepository> mirroredRepos = new ArrayList<ArtifactRepository>();
221
222 List<ArtifactRepositoryPolicy> releasePolicies =
223 new ArrayList<ArtifactRepositoryPolicy>( aliasedRepos.size() );
224
225 for ( ArtifactRepository aliasedRepo : aliasedRepos )
226 {
227 releasePolicies.add( aliasedRepo.getReleases() );
228 mirroredRepos.addAll( aliasedRepo.getMirroredRepositories() );
229 }
230
231 ArtifactRepositoryPolicy releasePolicy = getEffectivePolicy( releasePolicies );
232
233 List<ArtifactRepositoryPolicy> snapshotPolicies =
234 new ArrayList<ArtifactRepositoryPolicy>( aliasedRepos.size() );
235
236 for ( ArtifactRepository aliasedRepo : aliasedRepos )
237 {
238 snapshotPolicies.add( aliasedRepo.getSnapshots() );
239 }
240
241 ArtifactRepositoryPolicy snapshotPolicy = getEffectivePolicy( snapshotPolicies );
242
243 ArtifactRepository aliasedRepo = aliasedRepos.get( 0 );
244
245 ArtifactRepository effectiveRepository =
246 createArtifactRepository( aliasedRepo.getId(), aliasedRepo.getUrl(), aliasedRepo.getLayout(),
247 snapshotPolicy, releasePolicy );
248
249 effectiveRepository.setAuthentication( aliasedRepo.getAuthentication() );
250
251 effectiveRepository.setProxy( aliasedRepo.getProxy() );
252
253 effectiveRepository.setMirroredRepositories( mirroredRepos );
254
255 effectiveRepositories.add( effectiveRepository );
256 }
257
258 return effectiveRepositories;
259 }
260
261 private ArtifactRepositoryPolicy getEffectivePolicy( Collection<ArtifactRepositoryPolicy> policies )
262 {
263 ArtifactRepositoryPolicy effectivePolicy = null;
264
265 for ( ArtifactRepositoryPolicy policy : policies )
266 {
267 if ( effectivePolicy == null )
268 {
269 effectivePolicy = new ArtifactRepositoryPolicy( policy );
270 }
271 else
272 {
273 effectivePolicy.merge( policy );
274 }
275 }
276
277 return effectivePolicy;
278 }
279
280 public Mirror getMirror( ArtifactRepository repository, List<Mirror> mirrors )
281 {
282 return MirrorSelector.getMirror( repository, mirrors );
283 }
284
285 public void injectMirror( List<ArtifactRepository> repositories, List<Mirror> mirrors )
286 {
287 if ( repositories != null && mirrors != null )
288 {
289 for ( ArtifactRepository repository : repositories )
290 {
291 Mirror mirror = getMirror( repository, mirrors );
292 injectMirror( repository, mirror );
293 }
294 }
295 }
296
297 private Mirror getMirror( RepositorySystemSession session, ArtifactRepository repository )
298 {
299 if ( session != null )
300 {
301 org.eclipse.aether.repository.MirrorSelector selector = session.getMirrorSelector();
302 if ( selector != null )
303 {
304 RemoteRepository repo = selector.getMirror( RepositoryUtils.toRepo( repository ) );
305 if ( repo != null )
306 {
307 Mirror mirror = new Mirror();
308 mirror.setId( repo.getId() );
309 mirror.setUrl( repo.getUrl() );
310 mirror.setLayout( repo.getContentType() );
311 return mirror;
312 }
313 }
314 }
315 return null;
316 }
317
318 public void injectMirror( RepositorySystemSession session, List<ArtifactRepository> repositories )
319 {
320 if ( repositories != null && session != null )
321 {
322 for ( ArtifactRepository repository : repositories )
323 {
324 Mirror mirror = getMirror( session, repository );
325 injectMirror( repository, mirror );
326 }
327 }
328 }
329
330 private void injectMirror( ArtifactRepository repository, Mirror mirror )
331 {
332 if ( mirror != null )
333 {
334 ArtifactRepository original =
335 createArtifactRepository( repository.getId(), repository.getUrl(), repository.getLayout(),
336 repository.getSnapshots(), repository.getReleases() );
337
338 repository.setMirroredRepositories( Collections.singletonList( original ) );
339
340 repository.setId( mirror.getId() );
341 repository.setUrl( mirror.getUrl() );
342
343 if ( StringUtils.isNotEmpty( mirror.getLayout() ) )
344 {
345 repository.setLayout( getLayout( mirror.getLayout() ) );
346 }
347 }
348 }
349
350 public void injectAuthentication( List<ArtifactRepository> repositories, List<Server> servers )
351 {
352 if ( repositories != null )
353 {
354 Map<String, Server> serversById = new HashMap<String, Server>();
355
356 if ( servers != null )
357 {
358 for ( Server server : servers )
359 {
360 if ( !serversById.containsKey( server.getId() ) )
361 {
362 serversById.put( server.getId(), server );
363 }
364 }
365 }
366
367 for ( ArtifactRepository repository : repositories )
368 {
369 Server server = serversById.get( repository.getId() );
370
371 if ( server != null )
372 {
373 SettingsDecryptionRequest request = new DefaultSettingsDecryptionRequest( server );
374 SettingsDecryptionResult result = settingsDecrypter.decrypt( request );
375 server = result.getServer();
376
377 if ( logger.isDebugEnabled() )
378 {
379 for ( SettingsProblem problem : result.getProblems() )
380 {
381 logger.debug( problem.getMessage(), problem.getException() );
382 }
383 }
384
385 Authentication authentication = new Authentication( server.getUsername(), server.getPassword() );
386 authentication.setPrivateKey( server.getPrivateKey() );
387 authentication.setPassphrase( server.getPassphrase() );
388
389 repository.setAuthentication( authentication );
390 }
391 else
392 {
393 repository.setAuthentication( null );
394 }
395 }
396 }
397 }
398
399 private Authentication getAuthentication( RepositorySystemSession session, ArtifactRepository repository )
400 {
401 if ( session != null )
402 {
403 AuthenticationSelector selector = session.getAuthenticationSelector();
404 if ( selector != null )
405 {
406 RemoteRepository repo = RepositoryUtils.toRepo( repository );
407 org.eclipse.aether.repository.Authentication auth = selector.getAuthentication( repo );
408 if ( auth != null )
409 {
410 repo = new RemoteRepository.Builder( repo ).setAuthentication( auth ).build();
411 AuthenticationContext authCtx = AuthenticationContext.forRepository( session, repo );
412 Authentication result =
413 new Authentication( authCtx.get( AuthenticationContext.USERNAME ),
414 authCtx.get( AuthenticationContext.PASSWORD ) );
415 result.setPrivateKey( authCtx.get( AuthenticationContext.PRIVATE_KEY_PATH ) );
416 result.setPassphrase( authCtx.get( AuthenticationContext.PRIVATE_KEY_PASSPHRASE ) );
417 authCtx.close();
418 return result;
419 }
420 }
421 }
422 return null;
423 }
424
425 public void injectAuthentication( RepositorySystemSession session, List<ArtifactRepository> repositories )
426 {
427 if ( repositories != null && session != null )
428 {
429 for ( ArtifactRepository repository : repositories )
430 {
431 repository.setAuthentication( getAuthentication( session, repository ) );
432 }
433 }
434 }
435
436 private Proxy getProxy( RepositorySystemSession session, ArtifactRepository repository )
437 {
438 if ( session != null )
439 {
440 ProxySelector selector = session.getProxySelector();
441 if ( selector != null )
442 {
443 RemoteRepository repo = RepositoryUtils.toRepo( repository );
444 org.eclipse.aether.repository.Proxy proxy = selector.getProxy( repo );
445 if ( proxy != null )
446 {
447 Proxy p = new Proxy();
448 p.setHost( proxy.getHost() );
449 p.setProtocol( proxy.getType() );
450 p.setPort( proxy.getPort() );
451 if ( proxy.getAuthentication() != null )
452 {
453 repo = new RemoteRepository.Builder( repo ).setProxy( proxy ).build();
454 AuthenticationContext authCtx = AuthenticationContext.forProxy( session, repo );
455 p.setUserName( authCtx.get( AuthenticationContext.USERNAME ) );
456 p.setPassword( authCtx.get( AuthenticationContext.PASSWORD ) );
457 p.setNtlmDomain( authCtx.get( AuthenticationContext.NTLM_DOMAIN ) );
458 p.setNtlmHost( authCtx.get( AuthenticationContext.NTLM_WORKSTATION ) );
459 authCtx.close();
460 }
461 return p;
462 }
463 }
464 }
465 return null;
466 }
467
468 public void injectProxy( RepositorySystemSession session, List<ArtifactRepository> repositories )
469 {
470 if ( repositories != null && session != null )
471 {
472 for ( ArtifactRepository repository : repositories )
473 {
474 repository.setProxy( getProxy( session, repository ) );
475 }
476 }
477 }
478
479 private ArtifactRepositoryLayout getLayout( String id )
480 {
481 ArtifactRepositoryLayout layout = layouts.get( id );
482
483 return layout;
484 }
485
486
487
488
489
490
491 public static org.apache.maven.model.Repository fromSettingsRepository( org.apache.maven.settings.Repository settingsRepository )
492 {
493 org.apache.maven.model.Repository modelRepository = new org.apache.maven.model.Repository();
494 modelRepository.setId( settingsRepository.getId() );
495 modelRepository.setLayout( settingsRepository.getLayout() );
496 modelRepository.setName( settingsRepository.getName() );
497 modelRepository.setUrl( settingsRepository.getUrl() );
498 modelRepository.setReleases( fromSettingsRepositoryPolicy( settingsRepository.getReleases() ) );
499 modelRepository.setSnapshots( fromSettingsRepositoryPolicy( settingsRepository.getSnapshots() ) );
500 return modelRepository;
501 }
502
503 public static org.apache.maven.model.RepositoryPolicy fromSettingsRepositoryPolicy( org.apache.maven.settings.RepositoryPolicy settingsRepositoryPolicy )
504 {
505 org.apache.maven.model.RepositoryPolicy modelRepositoryPolicy = new org.apache.maven.model.RepositoryPolicy();
506 if ( settingsRepositoryPolicy != null )
507 {
508 modelRepositoryPolicy.setEnabled( settingsRepositoryPolicy.isEnabled() );
509 modelRepositoryPolicy.setUpdatePolicy( settingsRepositoryPolicy.getUpdatePolicy() );
510 modelRepositoryPolicy.setChecksumPolicy( settingsRepositoryPolicy.getChecksumPolicy() );
511 }
512 return modelRepositoryPolicy;
513 }
514
515 public static ArtifactRepository buildArtifactRepository( org.apache.maven.settings.Repository repo )
516 throws InvalidRepositoryException
517 {
518 return buildArtifactRepository( fromSettingsRepository( repo ) );
519 }
520
521 public static ArtifactRepository buildArtifactRepository( org.apache.maven.model.Repository repo )
522 throws InvalidRepositoryException
523 {
524 if ( repo != null )
525 {
526 String id = repo.getId();
527
528 if ( StringUtils.isEmpty( id ) )
529 {
530 throw new InvalidRepositoryException( "Repository identifier missing", "" );
531 }
532
533 String url = repo.getUrl();
534
535 if ( StringUtils.isEmpty( url ) )
536 {
537 throw new InvalidRepositoryException( "URL missing for repository " + id, id );
538 }
539
540 ArtifactRepositoryPolicy snapshots = buildArtifactRepositoryPolicy( repo.getSnapshots() );
541
542 ArtifactRepositoryPolicy releases = buildArtifactRepositoryPolicy( repo.getReleases() );
543
544 ArtifactRepositoryLayout layout = new DefaultRepositoryLayout();
545
546 return createArtifactRepository( id, url, layout, snapshots, releases );
547 }
548 else
549 {
550 return null;
551 }
552 }
553
554 public static ArtifactRepositoryPolicy buildArtifactRepositoryPolicy( org.apache.maven.model.RepositoryPolicy policy )
555 {
556 boolean enabled = true;
557
558 String updatePolicy = null;
559
560 String checksumPolicy = null;
561
562 if ( policy != null )
563 {
564 enabled = policy.isEnabled();
565
566 if ( policy.getUpdatePolicy() != null )
567 {
568 updatePolicy = policy.getUpdatePolicy();
569 }
570 if ( policy.getChecksumPolicy() != null )
571 {
572 checksumPolicy = policy.getChecksumPolicy();
573 }
574 }
575
576 return new ArtifactRepositoryPolicy( enabled, updatePolicy, checksumPolicy );
577 }
578
579 public static ArtifactRepository createArtifactRepository( String id, String url,
580 ArtifactRepositoryLayout repositoryLayout,
581 ArtifactRepositoryPolicy snapshots,
582 ArtifactRepositoryPolicy releases )
583 {
584 if ( snapshots == null )
585 {
586 snapshots = new ArtifactRepositoryPolicy();
587 }
588
589 if ( releases == null )
590 {
591 releases = new ArtifactRepositoryPolicy();
592 }
593
594 ArtifactRepository repository;
595 if ( repositoryLayout instanceof ArtifactRepositoryLayout2 )
596 {
597 repository =
598 ( (ArtifactRepositoryLayout2) repositoryLayout ).newMavenArtifactRepository( id, url, snapshots,
599 releases );
600 }
601 else
602 {
603 repository = new MavenArtifactRepository( id, url, repositoryLayout, snapshots, releases );
604 }
605
606 return repository;
607 }
608
609
610 private Artifact XcreateArtifact( String groupId, String artifactId, String version, String scope, String type )
611 {
612 return XcreateArtifact( groupId, artifactId, version, scope, type, null, null );
613 }
614
615 private Artifact XcreateDependencyArtifact( String groupId, String artifactId, VersionRange versionRange,
616 String type, String classifier, String scope, boolean optional )
617 {
618 return XcreateArtifact( groupId, artifactId, versionRange, type, classifier, scope, null, optional );
619 }
620
621 private Artifact XcreateProjectArtifact( String groupId, String artifactId, String version )
622 {
623 return XcreateProjectArtifact( groupId, artifactId, version, null );
624 }
625
626 private Artifact XcreateParentArtifact( String groupId, String artifactId, String version )
627 {
628 return XcreateProjectArtifact( groupId, artifactId, version );
629 }
630
631 private Artifact XcreatePluginArtifact( String groupId, String artifactId, VersionRange versionRange )
632 {
633 return XcreateArtifact( groupId, artifactId, versionRange, "maven-plugin", null, Artifact.SCOPE_RUNTIME, null );
634 }
635
636 private Artifact XcreateProjectArtifact( String groupId, String artifactId, String version, String scope )
637 {
638 return XcreateArtifact( groupId, artifactId, version, scope, "pom" );
639 }
640
641 private Artifact XcreateExtensionArtifact( String groupId, String artifactId, VersionRange versionRange )
642 {
643 return XcreateArtifact( groupId, artifactId, versionRange, "jar", null, Artifact.SCOPE_RUNTIME, null );
644 }
645
646 private Artifact XcreateArtifact( String groupId, String artifactId, String version, String scope, String type,
647 String classifier, String inheritedScope )
648 {
649 VersionRange versionRange = null;
650 if ( version != null )
651 {
652 versionRange = VersionRange.createFromVersion( version );
653 }
654 return XcreateArtifact( groupId, artifactId, versionRange, type, classifier, scope, inheritedScope );
655 }
656
657 private Artifact XcreateArtifact( String groupId, String artifactId, VersionRange versionRange, String type,
658 String classifier, String scope, String inheritedScope )
659 {
660 return XcreateArtifact( groupId, artifactId, versionRange, type, classifier, scope, inheritedScope, false );
661 }
662
663 private Artifact XcreateArtifact( String groupId, String artifactId, VersionRange versionRange, String type,
664 String classifier, String scope, String inheritedScope, boolean optional )
665 {
666 String desiredScope = Artifact.SCOPE_RUNTIME;
667
668 if ( inheritedScope == null )
669 {
670 desiredScope = scope;
671 }
672 else if ( Artifact.SCOPE_TEST.equals( scope ) || Artifact.SCOPE_PROVIDED.equals( scope ) )
673 {
674 return null;
675 }
676 else if ( Artifact.SCOPE_COMPILE.equals( scope ) && Artifact.SCOPE_COMPILE.equals( inheritedScope ) )
677 {
678
679 desiredScope = Artifact.SCOPE_COMPILE;
680 }
681
682 if ( Artifact.SCOPE_TEST.equals( inheritedScope ) )
683 {
684 desiredScope = Artifact.SCOPE_TEST;
685 }
686
687 if ( Artifact.SCOPE_PROVIDED.equals( inheritedScope ) )
688 {
689 desiredScope = Artifact.SCOPE_PROVIDED;
690 }
691
692 if ( Artifact.SCOPE_SYSTEM.equals( scope ) )
693 {
694
695 desiredScope = Artifact.SCOPE_SYSTEM;
696 }
697
698 ArtifactHandler handler = artifactHandlerManager.getArtifactHandler( type );
699
700 return new DefaultArtifact( groupId, artifactId, versionRange, desiredScope, type, classifier, handler,
701 optional );
702 }
703 }