-
-
Notifications
You must be signed in to change notification settings - Fork 200
Expand file tree
/
Copy pathDependencies.java
More file actions
138 lines (118 loc) · 4.59 KB
/
Dependencies.java
File metadata and controls
138 lines (118 loc) · 4.59 KB
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
/*
* Jooby https://jooby.io
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
* Copyright 2014 Edgar Espina
*/
package io.jooby.adoc;
import static io.jooby.SneakyThrows.throwingFunction;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.stream.Stream;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import io.jooby.SneakyThrows;
public class Dependencies {
public static class Dependency {
public String groupId;
public String artifactId;
public String version;
@Override
public String toString() {
return groupId + ":" + artifactId + ":" + version;
}
}
private Map<String, Dependency> dependencyMap = new TreeMap<>();
private Map<String, String> properties = new TreeMap<>();
private static final Dependencies instance = new Dependencies();
private Dependencies() {
try {
for (Document pom : pomList()) {
collectDependencies(pom, pom.select("dependencyManagement").select("dependencies"));
collectDependencies(pom, pom.select("dependencies"));
properties(pom, properties::putIfAbsent);
}
} catch (IOException x) {
throw SneakyThrows.propagate(x);
}
}
private void collectDependencies(Document pom, Elements dependencies) {
for (Element dependency : dependencies.select("dependency")) {
Dependency dep = new Dependency();
dep.groupId = dependency.select("groupId").text();
dep.artifactId = dependency.select("artifactId").text();
dep.version = findVersion(pom, dep.artifactId, dependency.select("version").text());
dependencyMap.putIfAbsent(dep.artifactId, dep);
}
}
public static Dependencies.Dependency get(String artifactId) {
Dependency dep = instance.dependencyMap.get(artifactId);
if (dep == null) {
throw new IllegalArgumentException("Missing artifact: " + artifactId);
}
return dep;
}
public static String version(String property) {
return instance.properties.getOrDefault(property, property);
}
private List<Document> pomList() throws IOException {
List<Document> poms = new ArrayList<>();
Document jooby =
Jsoup.parse(DocGenerator.basedir().getParent().resolve("pom.xml").toFile(), "UTF-8");
poms.add(jooby);
try (Stream<Path> tree = Files.walk(DocGenerator.basedir().getParent())) {
tree.filter(Files::isRegularFile)
.filter(it -> it.toString().endsWith("pom.xml"))
.map(throwingFunction(it -> Jsoup.parse(it.toFile())))
.forEach(poms::add);
}
jooby.select("dependencyManagement").select("dependencies").select("dependency").stream()
.filter(it -> it.select("type").text().equals("pom"))
.map(
it -> {
String artifactId = it.select("artifactId").text();
String versionRef = it.select("version").text();
String version = findVersion(jooby, artifactId, versionRef);
Path location = Paths.get(System.getProperty("user.home"), ".m2", "repository");
location =
Stream.of(it.select("groupId").text().split("\\."))
.reduce(location, Path::resolve, Path::resolve);
location = location.resolve(artifactId);
location = location.resolve(version);
location = location.resolve(artifactId + "-" + version + ".pom");
return location;
})
.map(throwingFunction(it -> Jsoup.parse(it.toFile())))
.forEach(poms::add);
return poms;
}
private static String findVersion(Document pom, String artifactId, String versionRef) {
if (versionRef.equals("${project.version}")) {
return pom.select("version").first().text();
}
if (versionRef.startsWith("${")) {
return pom.select("properties > *").stream()
.filter(e -> versionRef.equalsIgnoreCase("${" + e.tagName() + "}"))
.findFirst()
.map(Element::text)
.map(
it -> {
if (it.equals("${project.version}")) {
return pom.select("version").first().text();
}
return it;
})
.orElseGet(() -> pom.select("version").first().text());
}
return versionRef;
}
private static void properties(Document pom, BiConsumer<String, String> properties) {
pom.select("properties > *").forEach(e -> properties.accept(e.tagName(), e.text()));
}
}