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