-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathResolvedPom.java
221 lines (196 loc) · 6.44 KB
/
ResolvedPom.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package com.lazan.gradlemavenshare;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.maven.model.Dependency;
import org.apache.maven.model.Exclusion;
import org.apache.maven.model.Model;
/**
* Resolved maven model including
* <ul>
* <li>Linkage to the parent</li>
* <li>Dependency management applied to dependencies</li>
* <li>Property placeholders resolved with actual property values</li>
* <li>GroupId and Version inherited from parent hierarchy if not explicitly provided</li>
* </ul>
*/
public class ResolvedPom {
private final File pomFile;
private final ResolvedPom parent;
private final Model model;
private final Map<String, String> properties;
public ResolvedPom(File pomFile, ResolvedPom parent, Model model) {
super();
this.pomFile = pomFile;
this.parent = parent;
this.model = model;
this.properties = Collections.unmodifiableMap(resolveProperties());
}
public List<Dependency> getDependencies() {
if (model.getDependencies() == null || model.getDependencies().isEmpty()) {
return Collections.emptyList();
}
final Map<String, Dependency> managementMap = new LinkedHashMap<>();
ResolvedPomVisitor<Void> visitor = new ResolvedPomVisitor<Void>() {
@Override
public Void visit(ResolvedPom pom) {
if (pom.getModel().getDependencyManagement() != null) {
for (Dependency rawDep : pom.getModel().getDependencyManagement().getDependencies()) {
Dependency dep = resolve(rawDep);
String key = String.format("%s:%s", dep.getGroupId(), dep.getArtifactId());
if (!managementMap.containsKey(key)) {
managementMap.put(key, dep);
}
}
}
return null;
}
};
visitHierarchy(visitor);
List<Dependency> dependencies = new ArrayList<>(model.getDependencies().size());
for (Dependency rawDep : model.getDependencies()) {
Dependency dep = resolve(rawDep);
String key = String.format("%s:%s", dep.getGroupId(), dep.getArtifactId());
Dependency managedDep = managementMap.get(key);
if (managedDep != null) {
if (dep.getVersion() == null) {
dep.setVersion(managedDep.getVersion());
}
if (dep.getScope() == null) {
dep.setScope(managedDep.getScope());
}
}
dependencies.add(dep);
}
return Collections.unmodifiableList(dependencies);
}
protected Dependency resolve(Dependency rawDep) {
Dependency dep = new Dependency();
dep.setGroupId(substituteProperties(rawDep.getGroupId()));
dep.setArtifactId(substituteProperties(rawDep.getArtifactId()));
dep.setVersion(substituteProperties(rawDep.getVersion()));
dep.setScope(substituteProperties(rawDep.getScope()));
dep.setSystemPath(substituteProperties(rawDep.getSystemPath()));
dep.setClassifier(substituteProperties(rawDep.getClassifier()));
dep.setType(substituteProperties(rawDep.getType()));
dep.setOptional(substituteProperties(rawDep.getOptional()));
if (rawDep.getExclusions() != null) {
List<Exclusion> exclusions = new ArrayList<>(rawDep.getExclusions().size());
for (Exclusion rawEx : rawDep.getExclusions()) {
Exclusion ex = new Exclusion();
ex.setArtifactId(substituteProperties(rawEx.getArtifactId()));
ex.setGroupId(substituteProperties(rawEx.getGroupId()));
exclusions.add(ex);
}
dep.setExclusions(exclusions);
}
return dep;
}
protected Map<String, String> resolveProperties() {
final Map<String, String> properties = new LinkedHashMap<>();
properties.put("project.groupId", getGroupId());
properties.put("project.artifactId", getArtifactId());
properties.put("project.version", getVersion());
ResolvedPomVisitor<Void> visitor = new ResolvedPomVisitor<Void>() {
@Override
public Void visit(ResolvedPom pom) {
Model unresolved = pom.getModel();
if (unresolved.getProperties() != null) {
for (Map.Entry<Object, Object> entry : unresolved.getProperties().entrySet()) {
String key = (String) entry.getKey();
String value = (String) entry.getValue();
if (!properties.containsKey(key)) {
properties.put(key, value);
}
}
}
return null;
}
};
visitHierarchy(visitor);
boolean dirtied = true;
while (dirtied) {
dirtied = false;
for (Map.Entry<String, String> entry : properties.entrySet()) {
String value = entry.getValue();
String result = substituteProperties(properties, value);
if (result != null && !result.equals(value)) {
dirtied = true;
entry.setValue(result);
}
}
}
return properties;
}
protected static final Pattern PROPERTY_PATTERN = Pattern.compile("\\$\\{(.*?)}");
public String substituteProperties(String value) {
return substituteProperties(properties, value);
}
protected String substituteProperties(Map<String, String> properties, String value) {
if (value == null) return null;
Matcher matcher = PROPERTY_PATTERN.matcher(value);
if (!matcher.find()) return value;
StringBuffer buffer = new StringBuffer();
do {
String name = matcher.group(1);
String replacement = properties.get(name);
if (replacement == null) {
// leave it as it was
replacement = matcher.group(0);
}
matcher.appendReplacement(buffer, Matcher.quoteReplacement(replacement));
} while (matcher.find());
matcher.appendTail(buffer);
return buffer.toString();
}
public File getPomFile() {
return pomFile;
}
public ResolvedPom getParent() {
return parent;
}
public String getArtifactId() {
return model.getArtifactId();
}
public Model getModel() {
return model;
}
public String getGroupId() {
ResolvedPomVisitor<String> visitor = new ResolvedPomVisitor<String>() {
public String visit(ResolvedPom pom) {
return pom.getModel().getGroupId();
}
};
return visitHierarchy(visitor);
}
public String getVersion() {
ResolvedPomVisitor<String> visitor = new ResolvedPomVisitor<String>() {
public String visit(ResolvedPom pom) {
return pom.getModel().getVersion();
}
};
return visitHierarchy(visitor);
}
protected <T> T visitHierarchy(ResolvedPomVisitor<T> visitor) {
ResolvedPom current = this;
while (current != null) {
T result = visitor.visit(current);
if (result != null) {
return result;
}
current = current.getParent();
}
return null;
}
public String getProperty(String name) {
return properties.get(name);
}
public Map<String, String> getProperties() {
return properties;
}
}