1 package org.apache.maven;
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.Collection;
24 import java.util.Collections;
25 import java.util.List;
26 import java.util.Map;
27
28 import org.apache.maven.artifact.handler.ArtifactHandler;
29 import org.apache.maven.artifact.handler.DefaultArtifactHandler;
30 import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
31 import org.apache.maven.artifact.repository.ArtifactRepository;
32 import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
33 import org.eclipse.aether.artifact.Artifact;
34 import org.eclipse.aether.artifact.ArtifactProperties;
35 import org.eclipse.aether.artifact.ArtifactType;
36 import org.eclipse.aether.artifact.ArtifactTypeRegistry;
37 import org.eclipse.aether.artifact.DefaultArtifact;
38 import org.eclipse.aether.artifact.DefaultArtifactType;
39 import org.eclipse.aether.graph.Dependency;
40 import org.eclipse.aether.graph.DependencyFilter;
41 import org.eclipse.aether.graph.DependencyNode;
42 import org.eclipse.aether.graph.Exclusion;
43 import org.eclipse.aether.repository.Authentication;
44 import org.eclipse.aether.repository.Proxy;
45 import org.eclipse.aether.repository.RemoteRepository;
46 import org.eclipse.aether.repository.RepositoryPolicy;
47 import org.eclipse.aether.util.repository.AuthenticationBuilder;
48
49
50
51
52
53
54
55 public class RepositoryUtils
56 {
57
58 private static String nullify( String string )
59 {
60 return ( string == null || string.length() <= 0 ) ? null : string;
61 }
62
63 private static org.apache.maven.artifact.Artifact toArtifact( Dependency dependency )
64 {
65 if ( dependency == null )
66 {
67 return null;
68 }
69
70 org.apache.maven.artifact.Artifact result = toArtifact( dependency.getArtifact() );
71 result.setScope( dependency.getScope() );
72 result.setOptional( dependency.isOptional() );
73
74 return result;
75 }
76
77 public static org.apache.maven.artifact.Artifact toArtifact( Artifact artifact )
78 {
79 if ( artifact == null )
80 {
81 return null;
82 }
83
84 ArtifactHandler handler = newHandler( artifact );
85
86
87
88
89
90 org.apache.maven.artifact.Artifact result =
91 new org.apache.maven.artifact.DefaultArtifact( artifact.getGroupId(), artifact.getArtifactId(),
92 artifact.getVersion(), null,
93 artifact.getProperty( ArtifactProperties.TYPE,
94 artifact.getExtension() ),
95 nullify( artifact.getClassifier() ), handler );
96
97 result.setFile( artifact.getFile() );
98 result.setResolved( artifact.getFile() != null );
99
100 List<String> trail = new ArrayList<String>( 1 );
101 trail.add( result.getId() );
102 result.setDependencyTrail( trail );
103
104 return result;
105 }
106
107 public static void toArtifacts( Collection<org.apache.maven.artifact.Artifact> artifacts,
108 Collection<? extends DependencyNode> nodes, List<String> trail,
109 DependencyFilter filter )
110 {
111 for ( DependencyNode node : nodes )
112 {
113 org.apache.maven.artifact.Artifact artifact = toArtifact( node.getDependency() );
114
115 List<String> nodeTrail = new ArrayList<String>( trail.size() + 1 );
116 nodeTrail.addAll( trail );
117 nodeTrail.add( artifact.getId() );
118
119 if ( filter == null || filter.accept( node, Collections.<DependencyNode>emptyList() ) )
120 {
121 artifact.setDependencyTrail( nodeTrail );
122 artifacts.add( artifact );
123 }
124
125 toArtifacts( artifacts, node.getChildren(), nodeTrail, filter );
126 }
127 }
128
129 public static Artifact toArtifact( org.apache.maven.artifact.Artifact artifact )
130 {
131 if ( artifact == null )
132 {
133 return null;
134 }
135
136 String version = artifact.getVersion();
137 if ( version == null && artifact.getVersionRange() != null )
138 {
139 version = artifact.getVersionRange().toString();
140 }
141
142 Map<String, String> props = null;
143 if ( org.apache.maven.artifact.Artifact.SCOPE_SYSTEM.equals( artifact.getScope() ) )
144 {
145 String localPath = ( artifact.getFile() != null ) ? artifact.getFile().getPath() : "";
146 props = Collections.singletonMap( ArtifactProperties.LOCAL_PATH, localPath );
147 }
148
149 Artifact result =
150 new DefaultArtifact( artifact.getGroupId(), artifact.getArtifactId(), artifact.getClassifier(),
151 artifact.getArtifactHandler().getExtension(), version, props,
152 newArtifactType( artifact.getType(), artifact.getArtifactHandler() ) );
153 result = result.setFile( artifact.getFile() );
154
155 return result;
156 }
157
158 public static Dependency toDependency( org.apache.maven.artifact.Artifact artifact,
159 Collection<org.apache.maven.model.Exclusion> exclusions )
160 {
161 if ( artifact == null )
162 {
163 return null;
164 }
165
166 Artifact result = toArtifact( artifact );
167
168 List<Exclusion> excl = null;
169 if ( exclusions != null )
170 {
171 excl = new ArrayList<Exclusion>( exclusions.size() );
172 for ( org.apache.maven.model.Exclusion exclusion : exclusions )
173 {
174 excl.add( toExclusion( exclusion ) );
175 }
176 }
177
178 return new Dependency( result, artifact.getScope(), artifact.isOptional(), excl );
179 }
180
181 public static List<RemoteRepository> toRepos( List<ArtifactRepository> repos )
182 {
183 if ( repos == null )
184 {
185 return null;
186 }
187
188 List<RemoteRepository> results = new ArrayList<RemoteRepository>( repos.size() );
189 for ( ArtifactRepository repo : repos )
190 {
191 results.add( toRepo( repo ) );
192 }
193 return results;
194 }
195
196 public static RemoteRepository toRepo( ArtifactRepository repo )
197 {
198 RemoteRepository result = null;
199 if ( repo != null )
200 {
201 RemoteRepository.Builder builder =
202 new RemoteRepository.Builder( repo.getId(), getLayout( repo ), repo.getUrl() );
203 builder.setSnapshotPolicy( toPolicy( repo.getSnapshots() ) );
204 builder.setReleasePolicy( toPolicy( repo.getReleases() ) );
205 builder.setAuthentication( toAuthentication( repo.getAuthentication() ) );
206 builder.setProxy( toProxy( repo.getProxy() ) );
207 builder.setMirroredRepositories( toRepos( repo.getMirroredRepositories() ) );
208 result = builder.build();
209 }
210 return result;
211 }
212
213 public static String getLayout( ArtifactRepository repo )
214 {
215 try
216 {
217 return repo.getLayout().getId();
218 }
219 catch ( LinkageError e )
220 {
221
222
223
224 String className = repo.getLayout().getClass().getSimpleName();
225 if ( className.endsWith( "RepositoryLayout" ) )
226 {
227 String layout = className.substring( 0, className.length() - "RepositoryLayout".length() );
228 if ( layout.length() > 0 )
229 {
230 layout = Character.toLowerCase( layout.charAt( 0 ) ) + layout.substring( 1 );
231 return layout;
232 }
233 }
234 return "";
235 }
236 }
237
238 private static RepositoryPolicy toPolicy( ArtifactRepositoryPolicy policy )
239 {
240 RepositoryPolicy result = null;
241 if ( policy != null )
242 {
243 result = new RepositoryPolicy( policy.isEnabled(), policy.getUpdatePolicy(), policy.getChecksumPolicy() );
244 }
245 return result;
246 }
247
248 private static Authentication toAuthentication( org.apache.maven.artifact.repository.Authentication auth )
249 {
250 Authentication result = null;
251 if ( auth != null )
252 {
253 AuthenticationBuilder authBuilder = new AuthenticationBuilder();
254 authBuilder.addUsername( auth.getUsername() ).addPassword( auth.getPassword() );
255 authBuilder.addPrivateKey( auth.getPrivateKey(), auth.getPassphrase() );
256 result = authBuilder.build();
257 }
258 return result;
259 }
260
261 private static Proxy toProxy( org.apache.maven.repository.Proxy proxy )
262 {
263 Proxy result = null;
264 if ( proxy != null )
265 {
266 AuthenticationBuilder authBuilder = new AuthenticationBuilder();
267 authBuilder.addUsername( proxy.getUserName() ).addPassword( proxy.getPassword() );
268 result = new Proxy( proxy.getProtocol(), proxy.getHost(), proxy.getPort(), authBuilder.build() );
269 }
270 return result;
271 }
272
273 public static ArtifactHandler newHandler( Artifact artifact )
274 {
275 String type = artifact.getProperty( ArtifactProperties.TYPE, artifact.getExtension() );
276 DefaultArtifactHandler handler = new DefaultArtifactHandler( type );
277 handler.setExtension( artifact.getExtension() );
278 handler.setLanguage( artifact.getProperty( ArtifactProperties.LANGUAGE, null ) );
279 String addedToClasspath = artifact.getProperty( ArtifactProperties.CONSTITUTES_BUILD_PATH, "" );
280 handler.setAddedToClasspath( Boolean.parseBoolean( addedToClasspath ) );
281 String includesDependencies = artifact.getProperty( ArtifactProperties.INCLUDES_DEPENDENCIES, "" );
282 handler.setIncludesDependencies( Boolean.parseBoolean( includesDependencies ) );
283 return handler;
284 }
285
286 public static ArtifactType newArtifactType( String id, ArtifactHandler handler )
287 {
288 return new DefaultArtifactType( id, handler.getExtension(), handler.getClassifier(), handler.getLanguage(),
289 handler.isAddedToClasspath(), handler.isIncludesDependencies() );
290 }
291
292 public static Dependency toDependency( org.apache.maven.model.Dependency dependency,
293 ArtifactTypeRegistry stereotypes )
294 {
295 ArtifactType stereotype = stereotypes.get( dependency.getType() );
296 if ( stereotype == null )
297 {
298 stereotype = new DefaultArtifactType( dependency.getType() );
299 }
300
301 boolean system = dependency.getSystemPath() != null && dependency.getSystemPath().length() > 0;
302
303 Map<String, String> props = null;
304 if ( system )
305 {
306 props = Collections.singletonMap( ArtifactProperties.LOCAL_PATH, dependency.getSystemPath() );
307 }
308
309 Artifact artifact =
310 new DefaultArtifact( dependency.getGroupId(), dependency.getArtifactId(), dependency.getClassifier(), null,
311 dependency.getVersion(), props, stereotype );
312
313 List<Exclusion> exclusions = new ArrayList<Exclusion>( dependency.getExclusions().size() );
314 for ( org.apache.maven.model.Exclusion exclusion : dependency.getExclusions() )
315 {
316 exclusions.add( toExclusion( exclusion ) );
317 }
318
319 Dependency result = new Dependency( artifact, dependency.getScope(), dependency.isOptional(), exclusions );
320
321 return result;
322 }
323
324 private static Exclusion toExclusion( org.apache.maven.model.Exclusion exclusion )
325 {
326 return new Exclusion( exclusion.getGroupId(), exclusion.getArtifactId(), "*", "*" );
327 }
328
329 public static ArtifactTypeRegistry newArtifactTypeRegistry( ArtifactHandlerManager handlerManager )
330 {
331 return new MavenArtifactTypeRegistry( handlerManager );
332 }
333
334 static class MavenArtifactTypeRegistry
335 implements ArtifactTypeRegistry
336 {
337
338 private final ArtifactHandlerManager handlerManager;
339
340 public MavenArtifactTypeRegistry( ArtifactHandlerManager handlerManager )
341 {
342 this.handlerManager = handlerManager;
343 }
344
345 public ArtifactType get( String stereotypeId )
346 {
347 ArtifactHandler handler = handlerManager.getArtifactHandler( stereotypeId );
348 return newArtifactType( stereotypeId, handler );
349 }
350
351 }
352
353 public static Collection<Artifact> toArtifacts( Collection<org.apache.maven.artifact.Artifact> artifactsToConvert )
354 {
355 List<Artifact> artifacts = new ArrayList<Artifact>();
356 for ( org.apache.maven.artifact.Artifact a : artifactsToConvert )
357 {
358 artifacts.add( toArtifact( a ) );
359 }
360 return artifacts;
361 }
362 }