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