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 public class ArtifactDescriptorReaderDelegate {
54 public void populateResult(RepositorySystemSession session, ArtifactDescriptorResult result, Model model) {
55 ArtifactTypeRegistry stereotypes = session.getArtifactTypeRegistry();
56
57 for (Repository r : model.getRepositories()) {
58 result.addRepository(ArtifactDescriptorUtils.toRemoteRepository(r));
59 }
60
61 for (org.apache.maven.model.Dependency dependency : model.getDependencies()) {
62 result.addDependency(convert(dependency, stereotypes));
63 }
64
65 DependencyManagement mgmt = model.getDependencyManagement();
66 if (mgmt != null) {
67 for (org.apache.maven.model.Dependency dependency : mgmt.getDependencies()) {
68 result.addManagedDependency(convert(dependency, stereotypes));
69 }
70 }
71
72 Map<String, Object> properties = new LinkedHashMap<>();
73
74 Prerequisites prerequisites = model.getPrerequisites();
75 if (prerequisites != null) {
76 properties.put("prerequisites.maven", prerequisites.getMaven());
77 }
78
79 List<License> licenses = model.getLicenses();
80 properties.put("license.count", licenses.size());
81 for (int i = 0; i < licenses.size(); i++) {
82 License license = licenses.get(i);
83 properties.put("license." + i + ".name", license.getName());
84 properties.put("license." + i + ".url", license.getUrl());
85 properties.put("license." + i + ".comments", license.getComments());
86 properties.put("license." + i + ".distribution", license.getDistribution());
87 }
88
89 result.setProperties(properties);
90
91 setArtifactProperties(result, model);
92 }
93
94 private Dependency convert(org.apache.maven.model.Dependency dependency, ArtifactTypeRegistry stereotypes) {
95 ArtifactType stereotype = stereotypes.get(dependency.getType());
96 if (stereotype == null) {
97
98 stereotype = new DefaultType(dependency.getType(), Language.NONE, "", null, false);
99 }
100
101 boolean system = dependency.getSystemPath() != null
102 && !dependency.getSystemPath().isEmpty();
103
104 Map<String, String> props = null;
105 if (system) {
106 props = Collections.singletonMap(MavenArtifactProperties.LOCAL_PATH, dependency.getSystemPath());
107 }
108
109 Artifact artifact = new DefaultArtifact(
110 dependency.getGroupId(),
111 dependency.getArtifactId(),
112 dependency.getClassifier(),
113 null,
114 dependency.getVersion(),
115 props,
116 stereotype);
117
118 List<Exclusion> exclusions = new ArrayList<>(dependency.getExclusions().size());
119 for (org.apache.maven.model.Exclusion exclusion : dependency.getExclusions()) {
120 exclusions.add(convert(exclusion));
121 }
122
123 return new Dependency(
124 artifact,
125 dependency.getScope(),
126 dependency.getOptional() != null ? dependency.isOptional() : null,
127 exclusions);
128 }
129
130 private Exclusion convert(org.apache.maven.model.Exclusion exclusion) {
131 return new Exclusion(exclusion.getGroupId(), exclusion.getArtifactId(), "*", "*");
132 }
133
134 private void setArtifactProperties(ArtifactDescriptorResult result, Model model) {
135 String downloadUrl = null;
136 DistributionManagement distMgmt = model.getDistributionManagement();
137 if (distMgmt != null) {
138 downloadUrl = distMgmt.getDownloadUrl();
139 }
140 if (downloadUrl != null && !downloadUrl.isEmpty()) {
141 Artifact artifact = result.getArtifact();
142 Map<String, String> props = new HashMap<>(artifact.getProperties());
143 props.put(ArtifactProperties.DOWNLOAD_URL, downloadUrl);
144 result.setArtifact(artifact.setProperties(props));
145 }
146 }
147 }