-
Notifications
You must be signed in to change notification settings - Fork 0
/
PomLoader.java
207 lines (183 loc) · 6.44 KB
/
PomLoader.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
/*
* Copyright 2012-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.loader.thin;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Properties;
import org.apache.maven.model.DependencyManagement;
import org.apache.maven.model.Model;
import org.apache.maven.model.Parent;
import org.apache.maven.model.building.DefaultModelProcessor;
import org.apache.maven.model.io.DefaultModelReader;
import org.apache.maven.model.locator.DefaultModelLocator;
import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.artifact.DefaultArtifact;
import org.eclipse.aether.graph.Dependency;
import org.eclipse.aether.graph.Exclusion;
import org.eclipse.aether.resolution.ArtifactResolutionException;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.util.PropertyPlaceholderHelper;
/**
* Utility class to help with reading and extracting dependencies from a physical pom.
*
* @author Dave Syer
*
*/
public class PomLoader {
private AetherEngine engine;
public PomLoader(AetherEngine engine) {
this.engine = engine;
}
public String getParent(Resource pom) {
if (!pom.exists()) {
return null;
}
Model model = readModel(pom);
Parent parent = model.getParent();
if (parent != null) {
return parent.getGroupId() + ":" + parent.getArtifactId() + ":pom::"
+ parent.getVersion();
}
return null;
}
public List<Dependency> getDependencies(Resource pom) {
if (!pom.exists()) {
return Collections.emptyList();
}
Model model = readModel(pom);
replacePlaceholders(model);
return convertDependencies(model.getDependencies());
}
public List<Dependency> getDependencyManagement(Resource pom) {
if (!pom.exists()) {
return Collections.emptyList();
}
List<Dependency> list = new ArrayList<>();
Model model = readModel(pom);
replacePlaceholders(model);
if (model.getParent() != null) {
list.add(new Dependency(getParentArtifact(model), "import"));
}
if (model.getDependencyManagement() != null) {
list.addAll(convertDependencies(
model.getDependencyManagement().getDependencies()));
}
return list;
}
private void replacePlaceholders(Model model) {
PropertyPlaceholderHelper helper = new PropertyPlaceholderHelper("${", "}");
Properties properties = new Properties();
loadProperties(model, properties);
for (org.apache.maven.model.Dependency dependency : model.getDependencies()) {
String version = dependency.getVersion();
if (version != null) {
dependency.setVersion(helper.replacePlaceholders(version, properties));
}
}
DependencyManagement dependencyManagement = model.getDependencyManagement();
if (dependencyManagement != null) {
for (org.apache.maven.model.Dependency dependency : dependencyManagement
.getDependencies()) {
String version = dependency.getVersion();
if (version != null) {
dependency
.setVersion(helper.replacePlaceholders(version, properties));
}
}
}
}
private void loadProperties(Model model, Properties properties) {
if (model.getParent() != null) {
Artifact artifact = getParentArtifact(model);
try {
List<File> resolved = engine.resolve(
Arrays.asList(new Dependency(artifact, "import")), false);
loadProperties(readModel(new FileSystemResource(resolved.get(0))),
properties);
}
catch (ArtifactResolutionException e) {
// TODO: log a warning?
}
}
properties.putAll(model.getProperties());
if (model.getVersion() != null) {
properties.setProperty("project.version", model.getVersion());
}
else if (model.getParent().getVersion() != null) {
properties.setProperty("project.version", model.getParent().getVersion());
}
if (model.getGroupId() != null) {
properties.setProperty("project.groupId", model.getGroupId());
}
else if (model.getParent().getGroupId() != null) {
properties.setProperty("project.groupId", model.getParent().getGroupId());
}
if (model.getArtifactId() != null) {
properties.setProperty("project.artifactId", model.getArtifactId());
}
}
private Artifact getParentArtifact(Model model) {
Parent parent = model.getParent();
return new DefaultArtifact(parent.getGroupId(), parent.getArtifactId(), "pom",
parent.getVersion());
}
private List<Dependency> convertDependencies(
List<org.apache.maven.model.Dependency> dependencies) {
List<Dependency> result = new ArrayList<>();
for (org.apache.maven.model.Dependency dependency : dependencies) {
String scope = dependency.getScope();
if (!"test".equals(scope) && !"provided".equals(scope)) {
Dependency item = new Dependency(artifact(dependency),
dependency.getScope());
item = item.setExclusions(convertExclusions(dependency.getExclusions()));
result.add(item);
}
}
return result;
}
private Collection<Exclusion> convertExclusions(
List<org.apache.maven.model.Exclusion> exclusions) {
List<Exclusion> result = new ArrayList<>();
for (org.apache.maven.model.Exclusion exclusion : exclusions) {
result.add(new Exclusion(exclusion.getGroupId(), exclusion.getArtifactId(),
"*", "*"));
}
return result;
}
private Artifact artifact(org.apache.maven.model.Dependency dependency) {
return new DefaultArtifact(dependency.getGroupId(), dependency.getArtifactId(),
dependency.getClassifier(), dependency.getType(),
dependency.getVersion());
}
private static Model readModel(Resource resource) {
DefaultModelProcessor modelProcessor = new DefaultModelProcessor();
modelProcessor.setModelLocator(new DefaultModelLocator());
modelProcessor.setModelReader(new DefaultModelReader());
try {
return modelProcessor.read(resource.getInputStream(), null);
}
catch (IOException ex) {
throw new IllegalStateException("Failed to build model from effective pom",
ex);
}
}
}