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