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