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