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 = new Authentication( auth.getUsername(), auth.getPassword() );
225         }
226         return result;
227     }
228 
229     private static Proxy toProxy( org.apache.maven.repository.Proxy proxy )
230     {
231         Proxy result = null;
232         if ( proxy != null )
233         {
234             Authentication auth = new Authentication( proxy.getUserName(), proxy.getPassword() );
235             result = new Proxy( proxy.getProtocol(), proxy.getHost(), proxy.getPort(), auth );
236         }
237         return result;
238     }
239 
240     public static ArtifactHandler newHandler( Artifact artifact )
241     {
242         String type = artifact.getProperty( ArtifactProperties.TYPE, artifact.getExtension() );
243         DefaultArtifactHandler handler = new DefaultArtifactHandler( type );
244         handler.setExtension( artifact.getExtension() );
245         handler.setLanguage( artifact.getProperty( ArtifactProperties.LANGUAGE, null ) );
246         handler.setAddedToClasspath( Boolean.parseBoolean( artifact.getProperty( ArtifactProperties.CONSTITUTES_BUILD_PATH,
247                                                                                  "" ) ) );
248         handler.setIncludesDependencies( Boolean.parseBoolean( artifact.getProperty( ArtifactProperties.INCLUDES_DEPENDENCIES,
249                                                                                      "" ) ) );
250         return handler;
251     }
252 
253     public static ArtifactType newArtifactType( String id, ArtifactHandler handler )
254     {
255         return new DefaultArtifactType( id, handler.getExtension(), handler.getClassifier(), handler.getLanguage(),
256                                         handler.isAddedToClasspath(), handler.isIncludesDependencies() );
257     }
258 
259     public static Dependency toDependency( org.apache.maven.model.Dependency dependency,
260                                            ArtifactTypeRegistry stereotypes )
261     {
262         ArtifactType stereotype = stereotypes.get( dependency.getType() );
263         if ( stereotype == null )
264         {
265             stereotype = new DefaultArtifactType( dependency.getType() );
266         }
267 
268         boolean system = dependency.getSystemPath() != null && dependency.getSystemPath().length() > 0;
269 
270         Map<String, String> props = null;
271         if ( system )
272         {
273             props = Collections.singletonMap( ArtifactProperties.LOCAL_PATH, dependency.getSystemPath() );
274         }
275 
276         Artifact artifact =
277             new DefaultArtifact( dependency.getGroupId(), dependency.getArtifactId(), dependency.getClassifier(), null,
278                                  dependency.getVersion(), props, stereotype );
279 
280         List<Exclusion> exclusions = new ArrayList<Exclusion>( dependency.getExclusions().size() );
281         for ( org.apache.maven.model.Exclusion exclusion : dependency.getExclusions() )
282         {
283             exclusions.add( toExclusion( exclusion ) );
284         }
285 
286         Dependency result = new Dependency( artifact, dependency.getScope(), dependency.isOptional(), exclusions );
287 
288         return result;
289     }
290 
291     private static Exclusion toExclusion( org.apache.maven.model.Exclusion exclusion )
292     {
293         return new Exclusion( exclusion.getGroupId(), exclusion.getArtifactId(), "*", "*" );
294     }
295 
296     public static ArtifactTypeRegistry newArtifactTypeRegistry( ArtifactHandlerManager handlerManager )
297     {
298         return new MavenArtifactTypeRegistry( handlerManager );
299     }
300 
301     static class MavenArtifactTypeRegistry
302         implements ArtifactTypeRegistry
303     {
304 
305         private final ArtifactHandlerManager handlerManager;
306 
307         public MavenArtifactTypeRegistry( ArtifactHandlerManager handlerManager )
308         {
309             this.handlerManager = handlerManager;
310         }
311 
312         public ArtifactType get( String stereotypeId )
313         {
314             ArtifactHandler handler = handlerManager.getArtifactHandler( stereotypeId );
315             return newArtifactType( stereotypeId, handler );
316         }
317 
318     }
319 
320 }