1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
49
50
51
52
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 stereotype = new DefaultType(dependency.getType(), Language.NONE, dependency.getType(), null, false);
100 }
101
102 boolean system = dependency.getSystemPath() != null
103 && !dependency.getSystemPath().isEmpty();
104
105 Map<String, String> props = null;
106 if (system) {
107 props = Collections.singletonMap(MavenArtifactProperties.LOCAL_PATH, dependency.getSystemPath());
108 }
109
110 Artifact artifact = new DefaultArtifact(
111 dependency.getGroupId(),
112 dependency.getArtifactId(),
113 dependency.getClassifier(),
114 null,
115 dependency.getVersion(),
116 props,
117 stereotype);
118
119 List<Exclusion> exclusions = new ArrayList<>(dependency.getExclusions().size());
120 for (org.apache.maven.model.Exclusion exclusion : dependency.getExclusions()) {
121 exclusions.add(convert(exclusion));
122 }
123
124 return new Dependency(
125 artifact,
126 dependency.getScope(),
127 dependency.getOptional() != null ? dependency.isOptional() : null,
128 exclusions);
129 }
130
131 private Exclusion convert(org.apache.maven.model.Exclusion exclusion) {
132 return new Exclusion(exclusion.getGroupId(), exclusion.getArtifactId(), "*", "*");
133 }
134
135 private void setArtifactProperties(ArtifactDescriptorResult result, Model model) {
136 String downloadUrl = null;
137 DistributionManagement distMgmt = model.getDistributionManagement();
138 if (distMgmt != null) {
139 downloadUrl = distMgmt.getDownloadUrl();
140 }
141 if (downloadUrl != null && !downloadUrl.isEmpty()) {
142 Artifact artifact = result.getArtifact();
143 Map<String, String> props = new HashMap<>(artifact.getProperties());
144 props.put(ArtifactProperties.DOWNLOAD_URL, downloadUrl);
145 result.setArtifact(artifact.setProperties(props));
146 }
147 }
148 }