1 package org.apache.maven.shared.transfer.dependencies.collect.internal;
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.List;
24
25 import org.apache.maven.RepositoryUtils;
26 import org.apache.maven.artifact.handler.ArtifactHandler;
27 import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
28 import org.apache.maven.model.Model;
29 import org.apache.maven.shared.transfer.dependencies.DependableCoordinate;
30 import org.apache.maven.shared.transfer.dependencies.collect.CollectorResult;
31 import org.apache.maven.shared.transfer.dependencies.collect.DependencyCollector;
32 import org.apache.maven.shared.transfer.dependencies.collect.DependencyCollectorException;
33 import org.eclipse.aether.RepositorySystem;
34 import org.eclipse.aether.RepositorySystemSession;
35 import org.eclipse.aether.artifact.Artifact;
36 import org.eclipse.aether.artifact.ArtifactTypeRegistry;
37 import org.eclipse.aether.artifact.DefaultArtifact;
38 import org.eclipse.aether.collection.CollectRequest;
39 import org.eclipse.aether.collection.DependencyCollectionException;
40 import org.eclipse.aether.graph.Dependency;
41 import org.eclipse.aether.repository.RemoteRepository;
42
43
44
45
46
47
48 class Maven31DependencyCollector implements MavenDependencyCollector
49 {
50 private final RepositorySystem repositorySystem;
51
52 private final ArtifactHandlerManager artifactHandlerManager;
53
54 private final RepositorySystemSession session;
55
56 private final List<RemoteRepository> aetherRepositories;
57
58 Maven31DependencyCollector( RepositorySystem repositorySystem, ArtifactHandlerManager artifactHandlerManager,
59 RepositorySystemSession session, List<RemoteRepository> aetherRepositories )
60 {
61 super();
62 this.repositorySystem = repositorySystem;
63 this.artifactHandlerManager = artifactHandlerManager;
64 this.session = session;
65 this.aetherRepositories = aetherRepositories;
66 }
67
68 private static Dependency toDependency( org.apache.maven.model.Dependency mavenDependency,
69 ArtifactTypeRegistry typeRegistry ) throws DependencyCollectorException
70 {
71 Class<?>[] argClasses = new Class<?>[] {org.apache.maven.model.Dependency.class, ArtifactTypeRegistry.class};
72
73 Object[] args = new Object[] {mavenDependency, typeRegistry};
74
75 return Invoker.invoke( RepositoryUtils.class, "toDependency", argClasses, args );
76 }
77
78 @Override
79 public CollectorResult collectDependencies( org.apache.maven.model.Dependency root )
80 throws DependencyCollectorException
81 {
82 ArtifactTypeRegistry typeRegistry = Invoker.invoke( RepositoryUtils.class, "newArtifactTypeRegistry",
83 ArtifactHandlerManager.class, artifactHandlerManager );
84
85 CollectRequest request = new CollectRequest();
86 request.setRoot( toDependency( root, typeRegistry ) );
87
88 return collectDependencies( request );
89 }
90
91 @Override
92 public CollectorResult collectDependencies( DependableCoordinate root ) throws DependencyCollectorException
93 {
94 ArtifactHandler artifactHandler = artifactHandlerManager.getArtifactHandler( root.getType() );
95
96 String extension = artifactHandler != null ? artifactHandler.getExtension() : null;
97
98 Artifact aetherArtifact = new DefaultArtifact( root.getGroupId(), root.getArtifactId(), root.getClassifier(),
99 extension, root.getVersion() );
100
101 CollectRequest request = new CollectRequest();
102 request.setRoot( new Dependency( aetherArtifact, null ) );
103
104 return collectDependencies( request );
105 }
106
107 @Override
108 public CollectorResult collectDependencies( Model root ) throws DependencyCollectorException
109 {
110
111 ArtifactHandler artifactHandler = artifactHandlerManager.getArtifactHandler( root.getPackaging() );
112
113 String extension = artifactHandler != null ? artifactHandler.getExtension() : null;
114
115 Artifact aetherArtifact = new DefaultArtifact( root.getGroupId(), root.getArtifactId(), extension,
116 root.getVersion() );
117
118 CollectRequest request = new CollectRequest();
119 request.setRoot( new Dependency( aetherArtifact, null ) );
120
121 ArtifactTypeRegistry typeRegistry = Invoker.invoke( RepositoryUtils.class, "newArtifactTypeRegistry",
122 ArtifactHandlerManager.class, artifactHandlerManager );
123
124 List<Dependency> aetherDependencies = new ArrayList<>( root.getDependencies().size() );
125 for ( org.apache.maven.model.Dependency mavenDependency : root.getDependencies() )
126 {
127 aetherDependencies.add( toDependency( mavenDependency, typeRegistry ) );
128 }
129 request.setDependencies( aetherDependencies );
130
131 if ( root.getDependencyManagement() != null )
132 {
133 List<Dependency> aetherManagerDependencies = new ArrayList<>(
134 root.getDependencyManagement().getDependencies().size() );
135
136 for ( org.apache.maven.model.Dependency mavenDependency : root.getDependencyManagement().getDependencies() )
137 {
138 aetherManagerDependencies.add( toDependency( mavenDependency, typeRegistry ) );
139 }
140
141 request.setManagedDependencies( aetherManagerDependencies );
142 }
143
144 return collectDependencies( request );
145 }
146
147 private CollectorResult collectDependencies( CollectRequest request ) throws DependencyCollectorException
148 {
149 request.setRepositories( aetherRepositories );
150
151 try
152 {
153 return new Maven31CollectorResult( repositorySystem.collectDependencies( session, request ) );
154 }
155 catch ( DependencyCollectionException e )
156 {
157 throw new DependencyCollectorException( e.getMessage(), e );
158 }
159 }
160 }