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.internal.impl.resolver;
20  
21  import java.util.ArrayList;
22  import java.util.Collections;
23  import java.util.HashMap;
24  import java.util.LinkedHashMap;
25  import java.util.List;
26  import java.util.Map;
27  
28  import org.apache.maven.api.Language;
29  import org.apache.maven.api.model.DependencyManagement;
30  import org.apache.maven.api.model.DistributionManagement;
31  import org.apache.maven.api.model.License;
32  import org.apache.maven.api.model.Model;
33  import org.apache.maven.api.model.Prerequisites;
34  import org.apache.maven.api.model.Repository;
35  import org.apache.maven.api.services.RepositoryFactory;
36  import org.apache.maven.internal.impl.InternalSession;
37  import org.apache.maven.internal.impl.resolver.artifact.MavenArtifactProperties;
38  import org.apache.maven.internal.impl.resolver.type.DefaultType;
39  import org.eclipse.aether.artifact.Artifact;
40  import org.eclipse.aether.artifact.ArtifactProperties;
41  import org.eclipse.aether.artifact.ArtifactType;
42  import org.eclipse.aether.artifact.ArtifactTypeRegistry;
43  import org.eclipse.aether.artifact.DefaultArtifact;
44  import org.eclipse.aether.graph.Dependency;
45  import org.eclipse.aether.graph.Exclusion;
46  import org.eclipse.aether.resolution.ArtifactDescriptorResult;
47  
48  /**
49   * Populates Aether {@link ArtifactDescriptorResult} from Maven project {@link Model}.
50   * <p>
51   * <strong>Note:</strong> This class is part of work in progress and can be changed or removed without notice.
52   * @since 3.2.4
53   */
54  public class ArtifactDescriptorReaderDelegate {
55      public void populateResult(InternalSession session, ArtifactDescriptorResult result, Model model) {
56          ArtifactTypeRegistry stereotypes = session.getSession().getArtifactTypeRegistry();
57  
58          for (Repository r : model.getRepositories()) {
59              result.addRepository(session.toRepository(
60                      session.getService(RepositoryFactory.class).createRemote(r)));
61          }
62  
63          for (org.apache.maven.api.model.Dependency dependency : model.getDependencies()) {
64              result.addDependency(convert(dependency, stereotypes));
65          }
66  
67          DependencyManagement mgmt = model.getDependencyManagement();
68          if (mgmt != null) {
69              for (org.apache.maven.api.model.Dependency dependency : mgmt.getDependencies()) {
70                  result.addManagedDependency(convert(dependency, stereotypes));
71              }
72          }
73  
74          Map<String, Object> properties = new LinkedHashMap<>();
75  
76          Prerequisites prerequisites = model.getPrerequisites();
77          if (prerequisites != null) {
78              properties.put("prerequisites.maven", prerequisites.getMaven());
79          }
80  
81          List<License> licenses = model.getLicenses();
82          properties.put("license.count", licenses.size());
83          for (int i = 0; i < licenses.size(); i++) {
84              License license = licenses.get(i);
85              properties.put("license." + i + ".name", license.getName());
86              properties.put("license." + i + ".url", license.getUrl());
87              properties.put("license." + i + ".comments", license.getComments());
88              properties.put("license." + i + ".distribution", license.getDistribution());
89          }
90  
91          result.setProperties(properties);
92  
93          setArtifactProperties(result, model);
94      }
95  
96      private Dependency convert(org.apache.maven.api.model.Dependency dependency, ArtifactTypeRegistry stereotypes) {
97          ArtifactType stereotype = stereotypes.get(dependency.getType());
98          if (stereotype == null) {
99              // TODO: this here is fishy
100             stereotype = new DefaultType(dependency.getType(), Language.NONE, "", null, false);
101         }
102 
103         boolean system = dependency.getSystemPath() != null
104                 && !dependency.getSystemPath().isEmpty();
105 
106         Map<String, String> props = null;
107         if (system) {
108             props = Collections.singletonMap(MavenArtifactProperties.LOCAL_PATH, dependency.getSystemPath());
109         }
110 
111         Artifact artifact = new DefaultArtifact(
112                 dependency.getGroupId(),
113                 dependency.getArtifactId(),
114                 dependency.getClassifier(),
115                 null,
116                 dependency.getVersion(),
117                 props,
118                 stereotype);
119 
120         List<Exclusion> exclusions = new ArrayList<>(dependency.getExclusions().size());
121         for (org.apache.maven.api.model.Exclusion exclusion : dependency.getExclusions()) {
122             exclusions.add(convert(exclusion));
123         }
124 
125         return new Dependency(
126                 artifact,
127                 dependency.getScope(),
128                 dependency.getOptional() != null ? dependency.isOptional() : null,
129                 exclusions);
130     }
131 
132     private Exclusion convert(org.apache.maven.api.model.Exclusion exclusion) {
133         return new Exclusion(exclusion.getGroupId(), exclusion.getArtifactId(), "*", "*");
134     }
135 
136     private void setArtifactProperties(ArtifactDescriptorResult result, Model model) {
137         String downloadUrl = null;
138         DistributionManagement distMgmt = model.getDistributionManagement();
139         if (distMgmt != null) {
140             downloadUrl = distMgmt.getDownloadUrl();
141         }
142         if (downloadUrl != null && !downloadUrl.isEmpty()) {
143             Artifact artifact = result.getArtifact();
144             Map<String, String> props = new HashMap<>(artifact.getProperties());
145             props.put(ArtifactProperties.DOWNLOAD_URL, downloadUrl);
146             result.setArtifact(artifact.setProperties(props));
147         }
148     }
149 }