View Javadoc
1   package org.apache.maven.resolver.internal.ant;
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.LinkedHashSet;
26  import java.util.List;
27  import java.util.Map;
28  import java.util.Properties;
29  
30  import org.apache.maven.resolver.internal.ant.types.Authentication;
31  import org.apache.maven.resolver.internal.ant.types.Dependency;
32  import org.apache.maven.resolver.internal.ant.types.Exclusion;
33  import org.apache.maven.resolver.internal.ant.types.Proxy;
34  import org.apache.maven.resolver.internal.ant.types.RemoteRepositories;
35  import org.apache.maven.resolver.internal.ant.types.RemoteRepository;
36  import org.apache.tools.ant.Project;
37  import org.eclipse.aether.RepositorySystemSession;
38  import org.eclipse.aether.artifact.ArtifactProperties;
39  import org.eclipse.aether.artifact.ArtifactType;
40  import org.eclipse.aether.artifact.ArtifactTypeRegistry;
41  import org.eclipse.aether.artifact.DefaultArtifact;
42  import org.eclipse.aether.artifact.DefaultArtifactType;
43  import org.eclipse.aether.impl.RemoteRepositoryManager;
44  import org.eclipse.aether.repository.RepositoryPolicy;
45  import org.eclipse.aether.util.repository.AuthenticationBuilder;
46  
47  /**
48   * Utility methods to convert between Aether and Ant objects.
49   */
50  class ConverterUtils
51  {
52  
53      private static org.eclipse.aether.artifact.Artifact toArtifact( Dependency dependency, ArtifactTypeRegistry types )
54      {
55          ArtifactType type = types.get( dependency.getType() );
56          if ( type == null )
57          {
58              type = new DefaultArtifactType( dependency.getType() );
59          }
60  
61          Map<String, String> props = null;
62          if ( "system".equals( dependency.getScope() ) && dependency.getSystemPath() != null )
63          {
64              props = Collections.singletonMap( ArtifactProperties.LOCAL_PATH, dependency.getSystemPath().getPath() );
65          }
66  
67          return new DefaultArtifact( dependency.getGroupId(), dependency.getArtifactId(), dependency.getClassifier(), null,
68                               dependency.getVersion(), props, type );
69      }
70  
71      public static org.eclipse.aether.repository.Authentication toAuthentication( Authentication auth )
72      {
73          if ( auth == null )
74          {
75              return null;
76          }
77          AuthenticationBuilder authBuilder = new AuthenticationBuilder();
78          authBuilder.addUsername( auth.getUsername() ).addPassword( auth.getPassword() );
79          authBuilder.addPrivateKey( auth.getPrivateKeyFile(), auth.getPassphrase() );
80          return authBuilder.build();
81      }
82  
83      public static org.eclipse.aether.graph.Dependency toDependency( Dependency dependency, List<Exclusion> exclusions,
84                                                                       RepositorySystemSession session )
85      {
86          return new org.eclipse.aether.graph.Dependency( toArtifact( dependency, session.getArtifactTypeRegistry() ),
87                                                           dependency.getScope(), false,
88                                                           toExclusions( dependency.getExclusions(), exclusions ) );
89      }
90  
91      /**
92       * Converts the given ant repository type to an Aether repository instance with authentication and proxy filled in
93       * via the sessions' selectors.
94       */
95      public static org.eclipse.aether.repository.RemoteRepository toDistRepository( RemoteRepository repo,
96                                                                         RepositorySystemSession session )
97      {
98          org.eclipse.aether.repository.RemoteRepository result = toRepository( repo );
99          org.eclipse.aether.repository.RemoteRepository.Builder builder =
100             new org.eclipse.aether.repository.RemoteRepository.Builder( result );
101         builder.setAuthentication( session.getAuthenticationSelector().getAuthentication( result ) );
102         builder.setProxy( session.getProxySelector().getProxy( result ) );
103         return builder.build();
104     }
105 
106     private static org.eclipse.aether.graph.Exclusion toExclusion( Exclusion exclusion )
107     {
108         return new org.eclipse.aether.graph.Exclusion( exclusion.getGroupId(), exclusion.getArtifactId(),
109                                                         exclusion.getClassifier(), exclusion.getExtension() );
110     }
111 
112     private static Collection<org.eclipse.aether.graph.Exclusion> toExclusions( Collection<Exclusion> exclusions1,
113                                                                                  Collection<Exclusion> exclusions2 )
114     {
115         Collection<org.eclipse.aether.graph.Exclusion> results =
116             new LinkedHashSet<org.eclipse.aether.graph.Exclusion>();
117         if ( exclusions1 != null )
118         {
119             for ( Exclusion exclusion : exclusions1 )
120             {
121                 results.add( toExclusion( exclusion ) );
122             }
123         }
124         if ( exclusions2 != null )
125         {
126             for ( Exclusion exclusion : exclusions2 )
127             {
128                 results.add( toExclusion( exclusion ) );
129             }
130         }
131         return results;
132     }
133 
134     private static RepositoryPolicy toPolicy( RemoteRepository.Policy policy, boolean enabled, String updates,
135                                               String checksums )
136     {
137         if ( policy != null )
138         {
139             enabled = policy.isEnabled();
140             if ( policy.getChecksums() != null )
141             {
142                 checksums = policy.getChecksums();
143             }
144             if ( policy.getUpdates() != null )
145             {
146                 updates = policy.getUpdates();
147             }
148         }
149         return new RepositoryPolicy( enabled, updates, checksums );
150     }
151 
152     /**
153      * Adds every &lt;String, String>-entry in the map as a property to the given Properties.
154      */
155     public static Properties addProperties( Properties props, Map<?, ?> map )
156     {
157         if ( props == null )
158         {
159             props = new Properties();
160         }
161         for ( Map.Entry<?, ?> entry : map.entrySet() )
162         {
163             if ( entry.getKey() instanceof String && entry.getValue() instanceof String )
164             {
165                 props.put( entry.getKey(), entry.getValue() );
166             }
167         }
168         return props;
169     }
170 
171     public static org.eclipse.aether.repository.Proxy toProxy( Proxy proxy )
172     {
173         if ( proxy == null )
174         {
175             return null;
176         }
177         return new org.eclipse.aether.repository.Proxy( proxy.getType(), proxy.getHost(), proxy.getPort(),
178                                                          toAuthentication( proxy.getAuthentication() ) );
179     }
180 
181     private static org.eclipse.aether.repository.RemoteRepository toRepository( RemoteRepository repo )
182     {
183         org.eclipse.aether.repository.RemoteRepository.Builder builder =
184             new org.eclipse.aether.repository.RemoteRepository.Builder( repo.getId(), repo.getType(), repo.getUrl() );
185         builder.setSnapshotPolicy( toPolicy( repo.getSnapshotPolicy(), repo.isSnapshots(), repo.getUpdates(),
186                                              repo.getChecksums() ) );
187         builder.setReleasePolicy( toPolicy( repo.getReleasePolicy(), repo.isReleases(), repo.getUpdates(),
188                                             repo.getChecksums() ) );
189         builder.setAuthentication( toAuthentication( repo.getAuthentication() ) );
190         return builder.build();
191     }
192 
193     public static List<org.eclipse.aether.repository.RemoteRepository> toRepositories( Project project,
194                                                                           RepositorySystemSession session,
195                                                                           RemoteRepositories repos, RemoteRepositoryManager remoteRepositoryManager )
196     {
197         List<RemoteRepository> repositories;
198 
199         if ( repos != null )
200         {
201             repositories = repos.getRepositories();
202         }
203         else
204         {
205             repositories = new ArrayList<RemoteRepository>();
206         }
207 
208         List<org.eclipse.aether.repository.RemoteRepository> results =
209             new ArrayList<org.eclipse.aether.repository.RemoteRepository>();
210         for ( RemoteRepository repo : repositories )
211         {
212             results.add( toRepository( repo ) );
213         }
214 
215         results =
216             remoteRepositoryManager.aggregateRepositories( session,
217                                                       Collections.<org.eclipse.aether.repository.RemoteRepository>emptyList(),
218                                                       results, true );
219 
220         return results;
221     }
222 
223 }