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.List;
23
24 import org.apache.maven.RepositoryUtils;
25 import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
26 import org.apache.maven.model.Dependency;
27 import org.apache.maven.model.Model;
28 import org.apache.maven.project.ProjectBuildingRequest;
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.codehaus.plexus.PlexusConstants;
34 import org.codehaus.plexus.PlexusContainer;
35 import org.codehaus.plexus.component.annotations.Component;
36 import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
37 import org.codehaus.plexus.context.Context;
38 import org.codehaus.plexus.context.ContextException;
39 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable;
40
41
42
43
44
45
46 @Component( role = DependencyCollector.class, hint = "default" )
47 class DefaultDependencyCollector implements DependencyCollector, Contextualizable
48 {
49 private PlexusContainer container;
50
51 @Override
52 public CollectorResult collectDependencies( ProjectBuildingRequest buildingRequest, Dependency root )
53 throws DependencyCollectorException
54 {
55 validateParameters( buildingRequest, root );
56
57 try
58 {
59 return getMavenDependencyCollector( buildingRequest ).collectDependencies( root );
60 }
61 catch ( ComponentLookupException e )
62 {
63 throw new DependencyCollectorException( e.getMessage(), e );
64 }
65 }
66
67 @Override
68 public CollectorResult collectDependencies( ProjectBuildingRequest buildingRequest, DependableCoordinate root )
69 throws DependencyCollectorException
70 {
71 validateParameters( buildingRequest, root );
72
73 try
74 {
75 return getMavenDependencyCollector( buildingRequest ).collectDependencies( root );
76 }
77 catch ( ComponentLookupException e )
78 {
79 throw new DependencyCollectorException( e.getMessage(), e );
80 }
81 }
82
83 @Override
84 public CollectorResult collectDependencies( ProjectBuildingRequest buildingRequest, Model root )
85 throws DependencyCollectorException
86 {
87 validateParameters( buildingRequest, root );
88
89 try
90 {
91 return getMavenDependencyCollector( buildingRequest ).collectDependencies( root );
92 }
93 catch ( ComponentLookupException e )
94 {
95 throw new DependencyCollectorException( e.getMessage(), e );
96 }
97 }
98
99 private void validateParameters( ProjectBuildingRequest buildingRequest, DependableCoordinate root )
100 {
101 validateBuildingRequest( buildingRequest );
102 validateRoot( root );
103 }
104
105 private void validateParameters( ProjectBuildingRequest buildingRequest, Dependency root )
106 {
107 validateBuildingRequest( buildingRequest );
108 validateRoot( root );
109 }
110
111 private void validateParameters( ProjectBuildingRequest buildingRequest, Model root )
112 {
113 validateBuildingRequest( buildingRequest );
114 validateRoot( root );
115 }
116
117 private void validateBuildingRequest( ProjectBuildingRequest buildingRequest )
118 {
119 if ( buildingRequest == null )
120 {
121 throw new IllegalArgumentException( "The parameter buildingRequest is not allowed to be null." );
122 }
123 }
124
125 private void validateRoot( Object root )
126 {
127 if ( root == null )
128 {
129 throw new IllegalArgumentException( "The parameter root is not allowed to be null." );
130 }
131 }
132
133
134
135
136 private boolean isMaven31()
137 {
138 try
139 {
140
141 Thread.currentThread().getContextClassLoader().loadClass( "org.eclipse.aether.artifact.Artifact" );
142 return true;
143 }
144 catch ( ClassNotFoundException e )
145 {
146 return false;
147 }
148 }
149
150
151
152
153
154
155
156 public void contextualize( Context context ) throws ContextException
157 {
158 container = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY );
159 }
160
161 private MavenDependencyCollector getMavenDependencyCollector( ProjectBuildingRequest buildingRequest )
162 throws ComponentLookupException, DependencyCollectorException
163 {
164 ArtifactHandlerManager artifactHandlerManager = container.lookup( ArtifactHandlerManager.class );
165
166 if ( isMaven31() )
167 {
168 org.eclipse.aether.RepositorySystem m31RepositorySystem = container.lookup(
169 org.eclipse.aether.RepositorySystem.class );
170
171 org.eclipse.aether.RepositorySystemSession session = Invoker.invoke( buildingRequest,
172 "getRepositorySession" );
173
174 List<org.eclipse.aether.repository.RemoteRepository> aetherRepositories = Invoker.invoke(
175 RepositoryUtils.class, "toRepos", List.class, buildingRequest.getRemoteRepositories() );
176
177 return new Maven31DependencyCollector( m31RepositorySystem, artifactHandlerManager, session,
178 aetherRepositories );
179 }
180 else
181 {
182
183 org.sonatype.aether.RepositorySystem m30RepositorySystem = container.lookup(
184 org.sonatype.aether.RepositorySystem.class );
185
186 org.sonatype.aether.RepositorySystemSession session = Invoker.invoke( buildingRequest,
187 "getRepositorySession" );
188
189 List<org.sonatype.aether.repository.RemoteRepository> aetherRepositories = Invoker.invoke(
190 RepositoryUtils.class, "toRepos", List.class, buildingRequest.getRemoteRepositories() );
191
192 return new Maven30DependencyCollector( m30RepositorySystem, artifactHandlerManager, session,
193 aetherRepositories );
194 }
195
196 }
197
198 }