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.api.Language;
29  import org.apache.maven.model.DependencyManagement;
30  import org.apache.maven.model.DistributionManagement;
31  import org.apache.maven.model.License;
32  import org.apache.maven.model.Model;
33  import org.apache.maven.model.Prerequisites;
34  import org.apache.maven.model.Repository;
35  import org.apache.maven.repository.internal.artifact.MavenArtifactProperties;
36  import org.apache.maven.repository.internal.type.DefaultType;
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.graph.Dependency;
44  import org.eclipse.aether.graph.Exclusion;
45  import org.eclipse.aether.resolution.ArtifactDescriptorResult;
46  
47  /**
48   * Populates Aether {@link ArtifactDescriptorResult} from Maven project {@link Model}.
49   * <p>
50   * <strong>Note:</strong> This class is part of work in progress and can be changed or removed without notice.
51   * @since 3.2.4
52   * @deprecated since 4.0.0, use {@code maven-api-impl} jar instead
53   */
54  @Deprecated(since = "4.0.0")
55  public class ArtifactDescriptorReaderDelegate {
56      public void populateResult(RepositorySystemSession session, ArtifactDescriptorResult result, Model model) {
57          ArtifactTypeRegistry stereotypes = session.getArtifactTypeRegistry();
58  
59          for (Repository r : model.getRepositories()) {
60              result.addRepository(ArtifactDescriptorUtils.toRemoteRepository(r));
61          }
62  
63          for (org.apache.maven.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.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.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.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.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 }