View Javadoc

1   package org.apache.maven;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * <strong>Warning:</strong> This is an internal utility class that is only public for technical reasons, it is not part
50   * of the public API. In particular, this class can be changed or deleted without prior notice.
51   * 
52   * @author Benjamin Bentmann
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           * NOTE: From Artifact.hasClassifier(), an empty string and a null both denote "no classifier". However, some
87           * plugins only check for null, so be sure to nullify an empty classifier.
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(), repo.getLayout().getId(), 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         }
206         return result;
207     }
208 
209     private static RepositoryPolicy toPolicy( ArtifactRepositoryPolicy policy )
210     {
211         RepositoryPolicy result = null;
212         if ( policy != null )
213         {
214             result = new RepositoryPolicy( policy.isEnabled(), policy.getUpdatePolicy(), policy.getChecksumPolicy() );
215         }
216         return result;
217     }
218 
219     private static Authentication toAuthentication( org.apache.maven.artifact.repository.Authentication auth )
220     {
221         Authentication result = null;
222         if ( auth != null )
223         {
224             result =
225                 new Authentication( auth.getUsername(), auth.getPassword(), auth.getPrivateKey(), auth.getPassphrase() );
226         }
227         return result;
228     }
229 
230     private static Proxy toProxy( org.apache.maven.repository.Proxy proxy )
231     {
232         Proxy result = null;
233         if ( proxy != null )
234         {
235             Authentication auth = new Authentication( proxy.getUserName(), proxy.getPassword() );
236             result = new Proxy( proxy.getProtocol(), proxy.getHost(), proxy.getPort(), auth );
237         }
238         return result;
239     }
240 
241     public static ArtifactHandler newHandler( Artifact artifact )
242     {
243         String type = artifact.getProperty( ArtifactProperties.TYPE, artifact.getExtension() );
244         DefaultArtifactHandler handler = new DefaultArtifactHandler( type );
245         handler.setExtension( artifact.getExtension() );
246         handler.setLanguage( artifact.getProperty( ArtifactProperties.LANGUAGE, null ) );
247         handler.setAddedToClasspath( Boolean.parseBoolean( artifact.getProperty( ArtifactProperties.CONSTITUTES_BUILD_PATH,
248                                                                                  "" ) ) );
249         handler.setIncludesDependencies( Boolean.parseBoolean( artifact.getProperty( ArtifactProperties.INCLUDES_DEPENDENCIES,
250                                                                                      "" ) ) );
251         return handler;
252     }
253 
254     public static ArtifactType newArtifactType( String id, ArtifactHandler handler )
255     {
256         return new DefaultArtifactType( id, handler.getExtension(), handler.getClassifier(), handler.getLanguage(),
257                                         handler.isAddedToClasspath(), handler.isIncludesDependencies() );
258     }
259 
260     public static Dependency toDependency( org.apache.maven.model.Dependency dependency,
261                                            ArtifactTypeRegistry stereotypes )
262     {
263         ArtifactType stereotype = stereotypes.get( dependency.getType() );
264         if ( stereotype == null )
265         {
266             stereotype = new DefaultArtifactType( dependency.getType() );
267         }
268 
269         boolean system = dependency.getSystemPath() != null && dependency.getSystemPath().length() > 0;
270 
271         Map<String, String> props = null;
272         if ( system )
273         {
274             props = Collections.singletonMap( ArtifactProperties.LOCAL_PATH, dependency.getSystemPath() );
275         }
276 
277         Artifact artifact =
278             new DefaultArtifact( dependency.getGroupId(), dependency.getArtifactId(), dependency.getClassifier(), null,
279                                  dependency.getVersion(), props, stereotype );
280 
281         List<Exclusion> exclusions = new ArrayList<Exclusion>( dependency.getExclusions().size() );
282         for ( org.apache.maven.model.Exclusion exclusion : dependency.getExclusions() )
283         {
284             exclusions.add( toExclusion( exclusion ) );
285         }
286 
287         Dependency result = new Dependency( artifact, dependency.getScope(), dependency.isOptional(), exclusions );
288 
289         return result;
290     }
291 
292     private static Exclusion toExclusion( org.apache.maven.model.Exclusion exclusion )
293     {
294         return new Exclusion( exclusion.getGroupId(), exclusion.getArtifactId(), "*", "*" );
295     }
296 
297     public static ArtifactTypeRegistry newArtifactTypeRegistry( ArtifactHandlerManager handlerManager )
298     {
299         return new MavenArtifactTypeRegistry( handlerManager );
300     }
301 
302     static class MavenArtifactTypeRegistry
303         implements ArtifactTypeRegistry
304     {
305 
306         private final ArtifactHandlerManager handlerManager;
307 
308         public MavenArtifactTypeRegistry( ArtifactHandlerManager handlerManager )
309         {
310             this.handlerManager = handlerManager;
311         }
312 
313         public ArtifactType get( String stereotypeId )
314         {
315             ArtifactHandler handler = handlerManager.getArtifactHandler( stereotypeId );
316             return newArtifactType( stereotypeId, handler );
317         }
318 
319     }
320 
321 }