-
Notifications
You must be signed in to change notification settings - Fork 22
/
ArtifactMavenData.java
314 lines (273 loc) · 10.5 KB
/
ArtifactMavenData.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*
* Copyright 2020 Google LLC.
*
* 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 com.google.cloud.tools.opensource.cloudbomdashboard;
import com.google.cloud.tools.opensource.dependencies.RepositoryUtility;
import com.google.common.collect.ImmutableList;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.io.FileUtils;
import org.apache.maven.artifact.repository.metadata.Metadata;
import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader;
import org.apache.maven.model.Model;
import org.apache.maven.model.building.DefaultModelBuilderFactory;
import org.apache.maven.model.building.DefaultModelBuildingRequest;
import org.apache.maven.model.building.ModelBuilder;
import org.apache.maven.model.building.ModelBuildingException;
import org.apache.maven.model.building.ModelBuildingRequest;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.apache.maven.project.ProjectModelResolver;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import org.eclipse.aether.RepositorySystem;
import org.eclipse.aether.RepositorySystemSession;
import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.internal.impl.DefaultRemoteRepositoryManager;
import org.eclipse.aether.repository.RemoteRepository;
/** Container class for all artifact data pulled from Maven central. */
public class ArtifactMavenData {
private static DateFormat dateFormat = new SimpleDateFormat("yyyyMMddhhmmss");
private static DateFormat outputFormat = new SimpleDateFormat("MM-dd-yyyy");
private static final Logger LOGGER = Logger.getLogger(ArtifactMavenData.class.getName());
private final Artifact artifact;
private final String latestVersion,
lastUpdated,
sharedDependenciesVersion,
sharedDependenciesPosition,
pomFileUrl,
metadataUrl;
private ArtifactMavenData(
Artifact artifact,
String latestVersion,
String lastUpdated,
String sharedDependenciesVersion,
String sharedDependenciesPosition,
String pomFileUrl,
String metadataUrl) {
this.artifact = artifact;
this.latestVersion = latestVersion;
this.lastUpdated = lastUpdated;
this.sharedDependenciesVersion = sharedDependenciesVersion;
this.sharedDependenciesPosition = sharedDependenciesPosition;
this.pomFileUrl = pomFileUrl;
this.metadataUrl = metadataUrl;
}
public Artifact getArtifact() {
return artifact;
}
public String getSharedDependenciesPosition() {
return sharedDependenciesPosition;
}
public String getPomFileUrl() {
return pomFileUrl;
}
public String getMetadataUrl() {
return metadataUrl;
}
public String getLatestVersion() {
return latestVersion;
}
public String getLastUpdated() {
return lastUpdated;
}
public String getSharedDependenciesVersion() {
return sharedDependenciesVersion;
}
public static ArtifactMavenData generateArtifactMavenData(Artifact artifact) {
String metadataUrl = generateMetadataUrl(artifact);
String pomFileUrl = generatePomFileUrl(artifact);
LatestMetadata metadata = getLatestVersionAndLastUpdated(metadataUrl);
String latestVersion = metadata.latestVersion;
String lastUpdated = metadata.lastUpdated;
SharedDependenciesData data = sharedDependencyPositionAndVersion(pomFileUrl, artifact);
String sharedDependenciesPosition = data.sharedDependencyPosition;
String sharedDependenciesVersion = data.sharedDependencyVersion;
return new ArtifactMavenData(
artifact,
latestVersion,
lastUpdated,
sharedDependenciesVersion,
sharedDependenciesPosition,
metadataUrl,
pomFileUrl);
}
private static LatestMetadata getLatestVersionAndLastUpdated(String metadataUrl) {
try {
File metadataFile = File.createTempFile("metadata", ".xml");
metadataFile.deleteOnExit();
URL url = new URL(metadataUrl);
FileUtils.copyURLToFile(url, metadataFile);
MetadataXpp3Reader reader = new MetadataXpp3Reader();
Metadata metadata = reader.read(new FileInputStream(metadataFile));
if (metadata.getVersioning() == null) {
return new LatestMetadata("", "");
}
String latestVersion;
if (metadata.getVersioning().getLatest() == null) {
latestVersion = "";
} else {
latestVersion = metadata.getVersioning().getLatest();
}
String lastUpdated = metadata.getVersioning().getLastUpdated();
if (lastUpdated != null && !lastUpdated.isEmpty()) {
Date date = dateFormat.parse(lastUpdated);
lastUpdated = outputFormat.format(date);
} else {
lastUpdated = "";
}
return new LatestMetadata(latestVersion, lastUpdated);
} catch (XmlPullParserException | ParseException | IOException ignored) {
return new LatestMetadata("", "");
}
}
private static SharedDependenciesData sharedDependencyPositionAndVersion(
String pomUrl, Artifact artifact) {
String groupPath = artifact.getGroupId().replace('.', '/');
String parentPath =
DashboardMain.basePath
+ "/"
+ groupPath
+ "/"
+ artifact.getArtifactId()
+ "-parent"
+ "/"
+ artifact.getVersion()
+ "/"
+ artifact.getArtifactId()
+ "-parent-"
+ artifact.getVersion()
+ ".pom";
String depsBomPath =
DashboardMain.basePath
+ "/"
+ groupPath
+ "/"
+ artifact.getArtifactId()
+ "-deps-bom"
+ "/"
+ artifact.getVersion()
+ "/"
+ artifact.getArtifactId()
+ "-deps-bom-"
+ artifact.getVersion()
+ ".pom";
String repository = artifact.getArtifactId().substring(13);
String releasePath =
"https://raw.githubusercontent.com/googleapis/java-"
+ repository
+ "/v"
+ artifact.getVersion().replaceAll("[^.\\d]", "")
+ "/pom.xml";
String version = getSharedDependenciesVersionFromUrl(parentPath);
if (version != null) {
return new SharedDependenciesData(parentPath, version);
}
version = getSharedDependenciesVersionFromUrl(pomUrl);
if (version != null) {
return new SharedDependenciesData(pomUrl, version);
}
version = getSharedDependenciesVersionFromUrl(releasePath);
if (version != null) {
return new SharedDependenciesData(releasePath, version);
}
version = getSharedDependenciesVersionFromUrl(depsBomPath);
if (version != null) {
return new SharedDependenciesData(depsBomPath, version);
}
return new SharedDependenciesData("", "");
}
private static String getSharedDependenciesVersionFromUrl(String pomUrl) {
try {
File pomFile = File.createTempFile("pomFile", ".xml");
pomFile.deleteOnExit();
URL url = new URL(pomUrl);
BufferedInputStream input = new BufferedInputStream(url.openStream());
FileUtils.copyInputStreamToFile(input, pomFile);
MavenXpp3Reader read = new MavenXpp3Reader();
Model model = read.read(new FileInputStream(pomFile));
ModelBuildingRequest request = new DefaultModelBuildingRequest();
request.setRawModel(model);
RepositorySystem repositorySystem = RepositoryUtility.newRepositorySystem();
RepositorySystemSession session = RepositoryUtility.newSession(repositorySystem);
// Set model resolver to locate parent POM on Maven Central.
RemoteRepository mavenCentral =
new RemoteRepository.Builder("central", "default", "https://repo1.maven.org/maven2/")
.build();
request.setModelResolver(
new ProjectModelResolver(
session,
null,
repositorySystem,
new DefaultRemoteRepositoryManager(),
ImmutableList.of(mavenCentral),
null,
null));
// Capture java version from system environment for profile activation.
request.setSystemProperties(System.getProperties());
ModelBuilder builder = new DefaultModelBuilderFactory().newInstance();
Model effectiveModel = builder.build(request).getEffectiveModel();
return effectiveModel.getProperties().getProperty("google-cloud-shared-dependencies.version");
} catch (XmlPullParserException | IOException | ModelBuildingException exception) {
LOGGER.log(Level.SEVERE, exception, () -> "Failed to parse contents of POM file: " + pomUrl);
}
return null;
}
private static String generatePomFileUrl(Artifact artifact) {
String groupPath = artifact.getGroupId().replace('.', '/');
return DashboardMain.basePath
+ "/"
+ groupPath
+ "/"
+ artifact.getArtifactId()
+ "/"
+ artifact.getVersion()
+ "/"
+ artifact.getArtifactId()
+ "-"
+ artifact.getVersion()
+ ".pom";
}
private static String generateMetadataUrl(Artifact artifact) {
String groupPath = artifact.getGroupId().replace('.', '/');
return DashboardMain.basePath
+ "/"
+ groupPath
+ "/"
+ artifact.getArtifactId()
+ "/maven-metadata.xml";
}
private static class LatestMetadata {
String latestVersion, lastUpdated;
LatestMetadata(String latestVersion, String lastUpdated) {
this.latestVersion = latestVersion;
this.lastUpdated = lastUpdated;
}
}
private static class SharedDependenciesData {
String sharedDependencyPosition, sharedDependencyVersion;
SharedDependenciesData(String sharedDependencyPosition, String sharedDependencyVersion) {
this.sharedDependencyPosition = sharedDependencyPosition;
this.sharedDependencyVersion = sharedDependencyVersion;
}
}
}