View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.resolver.internal.ant;
20  
21  import java.util.ArrayList;
22  import java.util.Collection;
23  import java.util.Collections;
24  import java.util.LinkedHashSet;
25  import java.util.List;
26  import java.util.Map;
27  import java.util.Properties;
28  
29  import org.apache.maven.resolver.internal.ant.types.Authentication;
30  import org.apache.maven.resolver.internal.ant.types.Dependency;
31  import org.apache.maven.resolver.internal.ant.types.Exclusion;
32  import org.apache.maven.resolver.internal.ant.types.Proxy;
33  import org.apache.maven.resolver.internal.ant.types.RemoteRepositories;
34  import org.apache.maven.resolver.internal.ant.types.RemoteRepository;
35  import org.apache.tools.ant.Project;
36  import org.eclipse.aether.RepositorySystem;
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.repository.RepositoryPolicy;
44  import org.eclipse.aether.util.repository.AuthenticationBuilder;
45  
46  /**
47   * Utility methods to convert between Aether and Ant objects.
48   */
49  class ConverterUtils {
50  
51      private static org.eclipse.aether.artifact.Artifact toArtifact(Dependency dependency, ArtifactTypeRegistry types) {
52          ArtifactType type = types.get(dependency.getType());
53          if (type == null) {
54              type = new DefaultArtifactType(dependency.getType());
55          }
56  
57          Map<String, String> props = null;
58          if ("system".equals(dependency.getScope()) && dependency.getSystemPath() != null) {
59              props = Collections.singletonMap(
60                      ArtifactProperties.LOCAL_PATH, dependency.getSystemPath().getPath());
61          }
62  
63          return new DefaultArtifact(
64                  dependency.getGroupId(),
65                  dependency.getArtifactId(),
66                  dependency.getClassifier(),
67                  null,
68                  dependency.getVersion(),
69                  props,
70                  type);
71      }
72  
73      public static org.eclipse.aether.repository.Authentication toAuthentication(Authentication auth) {
74          if (auth == null) {
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(
84              Dependency dependency, List<Exclusion> exclusions, RepositorySystemSession session) {
85          return new org.eclipse.aether.graph.Dependency(
86                  toArtifact(dependency, session.getArtifactTypeRegistry()),
87                  dependency.getScope(),
88                  false,
89                  toExclusions(dependency.getExclusions(), exclusions));
90      }
91  
92      /**
93       * Converts the given ant repository type to an Aether repository instance with authentication and proxy filled in
94       * via the sessions' selectors.
95       */
96      public static org.eclipse.aether.repository.RemoteRepository toDistRepository(
97              RemoteRepository repo, RepositorySystemSession session) {
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         return new org.eclipse.aether.graph.Exclusion(
108                 exclusion.getGroupId(), exclusion.getArtifactId(), exclusion.getClassifier(), exclusion.getExtension());
109     }
110 
111     private static Collection<org.eclipse.aether.graph.Exclusion> toExclusions(
112             Collection<Exclusion> exclusions1, Collection<Exclusion> exclusions2) {
113         Collection<org.eclipse.aether.graph.Exclusion> results = new LinkedHashSet<>();
114         if (exclusions1 != null) {
115             for (Exclusion exclusion : exclusions1) {
116                 results.add(toExclusion(exclusion));
117             }
118         }
119         if (exclusions2 != null) {
120             for (Exclusion exclusion : exclusions2) {
121                 results.add(toExclusion(exclusion));
122             }
123         }
124         return results;
125     }
126 
127     private static RepositoryPolicy toPolicy(
128             RemoteRepository.Policy policy, boolean enabled, String updates, String checksums) {
129         if (policy != null) {
130             enabled = policy.isEnabled();
131             if (policy.getChecksums() != null) {
132                 checksums = policy.getChecksums();
133             }
134             if (policy.getUpdates() != null) {
135                 updates = policy.getUpdates();
136             }
137         }
138         return new RepositoryPolicy(enabled, updates, checksums);
139     }
140 
141     /**
142      * Adds every &lt;String, String>-entry in the map as a property to the given Properties.
143      */
144     public static Properties addProperties(Properties props, Map<?, ?> map) {
145         if (props == null) {
146             props = new Properties();
147         }
148         for (Map.Entry<?, ?> entry : map.entrySet()) {
149             if (entry.getKey() instanceof String && entry.getValue() instanceof String) {
150                 props.put(entry.getKey(), entry.getValue());
151             }
152         }
153         return props;
154     }
155 
156     public static org.eclipse.aether.repository.Proxy toProxy(Proxy proxy) {
157         if (proxy == null) {
158             return null;
159         }
160         return new org.eclipse.aether.repository.Proxy(
161                 proxy.getType(), proxy.getHost(), proxy.getPort(), toAuthentication(proxy.getAuthentication()));
162     }
163 
164     private static org.eclipse.aether.repository.RemoteRepository toRepository(RemoteRepository repo) {
165         org.eclipse.aether.repository.RemoteRepository.Builder builder =
166                 new org.eclipse.aether.repository.RemoteRepository.Builder(repo.getId(), repo.getType(), repo.getUrl());
167         builder.setSnapshotPolicy(
168                 toPolicy(repo.getSnapshotPolicy(), repo.isSnapshots(), repo.getUpdates(), repo.getChecksums()));
169         builder.setReleasePolicy(
170                 toPolicy(repo.getReleasePolicy(), repo.isReleases(), repo.getUpdates(), repo.getChecksums()));
171         builder.setAuthentication(toAuthentication(repo.getAuthentication()));
172         return builder.build();
173     }
174 
175     public static List<org.eclipse.aether.repository.RemoteRepository> toRepositories(
176             Project project,
177             RepositorySystem repositorySystem,
178             RepositorySystemSession session,
179             RemoteRepositories repos) {
180         List<RemoteRepository> repositories;
181 
182         if (repos != null) {
183             repositories = repos.getRepositories();
184         } else {
185             repositories = new ArrayList<>();
186         }
187 
188         List<org.eclipse.aether.repository.RemoteRepository> results = new ArrayList<>();
189         for (RemoteRepository repo : repositories) {
190             results.add(toRepository(repo));
191         }
192 
193         results = repositorySystem.newResolutionRepositories(session, results);
194 
195         return results;
196     }
197 }