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.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
47
48
49
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 return new Dependency(
121 artifact,
122 dependency.getScope(),
123 dependency.getOptional() != null ? dependency.isOptional() : null,
124 exclusions);
125 }
126
127 private Exclusion convert(org.apache.maven.model.Exclusion exclusion) {
128 return new Exclusion(exclusion.getGroupId(), exclusion.getArtifactId(), "*", "*");
129 }
130
131 private void setArtifactProperties(ArtifactDescriptorResult result, Model model) {
132 String downloadUrl = null;
133 DistributionManagement distMgmt = model.getDistributionManagement();
134 if (distMgmt != null) {
135 downloadUrl = distMgmt.getDownloadUrl();
136 }
137 if (downloadUrl != null && !downloadUrl.isEmpty()) {
138 Artifact artifact = result.getArtifact();
139 Map<String, String> props = new HashMap<>(artifact.getProperties());
140 props.put(ArtifactProperties.DOWNLOAD_URL, downloadUrl);
141 result.setArtifact(artifact.setProperties(props));
142 }
143 }
144 }