-
Notifications
You must be signed in to change notification settings - Fork 1
/
MavenArtifactVersions.java
183 lines (167 loc) · 6.92 KB
/
MavenArtifactVersions.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
/*
* Copyright (c) 2010-2022 BSI Business Systems Integration AG.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* BSI Business Systems Integration AG - initial API and implementation
*/
package org.eclipse.scout.sdk.core.s.util.maven;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.jar.Attributes.Name;
import java.util.jar.JarFile;
import java.util.stream.Stream;
import java.util.zip.ZipFile;
import javax.xml.xpath.XPathExpressionException;
import org.eclipse.scout.sdk.core.ISourceFolders;
import org.eclipse.scout.sdk.core.apidef.ApiVersion;
import org.eclipse.scout.sdk.core.model.api.IClasspathEntry;
import org.eclipse.scout.sdk.core.model.api.IJavaEnvironment;
import org.eclipse.scout.sdk.core.util.SdkException;
import org.eclipse.scout.sdk.core.util.Strings;
import org.eclipse.scout.sdk.core.util.Xml;
import org.w3c.dom.Node;
/**
* Helper class to get version of a Maven artifacts.<br>
*/
public final class MavenArtifactVersions {
@SuppressWarnings("StaticCollection")
private static final Map<Path, Optional<ApiVersion>> VERSION_CACHE = new ConcurrentHashMap<>();
private MavenArtifactVersions() {
}
/**
* Tries to find the module specified in the given {@link IJavaEnvironment} and computes its Maven version.
* <p>
* For binary jars the version is taken from the {@link Name#IMPLEMENTATION_VERSION} manifest header.<br>
* For source folders it is taken from the parent pom.xml<br>
* Source or javadoc jar files are not supported.
* </p>
*
* @param artifactId
* The name of the Maven module. This is the name of the jar file (without any suffixes) and the name of the
* directory that contains the source folders. Typically, this is equal to the artifactId of the module. May
* be {@code null} but then the resulting {@link Optional} will always be empty.
* @param context
* The {@link IJavaEnvironment} in which the module should be searched. May be {@code null} but then the
* resulting {@link Optional} will always be empty.
* @return An {@link Optional} holding the Maven version of the given module or an empty {@link Optional} if the
* module could not be found, the version cannot be parsed or one of the parameters is {@code null}.
*/
public static Optional<ApiVersion> usedIn(String artifactId, IJavaEnvironment context) {
return modulePathIn(artifactId, context).flatMap(MavenArtifactVersions::version);
}
/**
* Gets the newest 200 (limitation by Maven central) versions of given artifact that can be found on Maven-central.
*
* @param groupId
* The groupId of the artifact. Must not be {@code null}.
* @param artifactId
* The artifactId of the artifact. Must not be {@code null}.
* @return A {@link Stream} with all versions found on Maven-central.
* @throws IOException
* if there is an error reading the value from Maven-central.
*/
public static Stream<String> allOnCentral(String groupId, String artifactId) throws IOException {
try {
var g = URLEncoder.encode(groupId, StandardCharsets.UTF_8);
var a = URLEncoder.encode(artifactId, StandardCharsets.UTF_8);
var uri = new URI("https://search.maven.org/solrsearch/select?q=g:" + g + "+AND+a:" + a + "&core=gav&rows=100&wt=xml");
var dom = Xml.get(uri);
return Xml.evaluateXPath("result/doc/str[@name='v']", dom.getDocumentElement()).stream()
.map(Node::getTextContent)
.filter(Strings::hasText);
}
catch (URISyntaxException | XPathExpressionException e) {
throw new IOException(e);
}
}
static Optional<Path> modulePathIn(String moduleName, IJavaEnvironment context) {
if (context == null || Strings.isBlank(moduleName)) {
return Optional.empty();
}
var jarPath = context.classpath()
.filter(p -> isModuleJar(moduleName, p))
.map(IClasspathEntry::path)
.findAny();
if (jarPath.isPresent()) {
return jarPath;
}
return context.classpath()
.filter(p -> isModuleSourceFolder(moduleName, p))
.map(IClasspathEntry::path)
.findAny();
}
static Optional<ApiVersion> version(Path pathToModule) {
return VERSION_CACHE.computeIfAbsent(pathToModule, MavenArtifactVersions::detectVersion);
}
static Optional<ApiVersion> detectVersion(Path pathToModule) {
if (Files.isRegularFile(pathToModule)) {
return versionOfJar(pathToModule);
}
return versionOfSourceFolder(pathToModule);
}
static Optional<ApiVersion> versionOfSourceFolder(Path sourceFolder) {
// remove src/main/java
var mainFolder = sourceFolder.getParent();
if (mainFolder == null) {
return Optional.empty();
}
var srcFolder = mainFolder.getParent();
if (srcFolder == null) {
return Optional.empty();
}
var modulePath = srcFolder.getParent();
if (modulePath == null) {
return Optional.empty();
}
try {
var pomContent = Xml.get(modulePath.resolve(IMavenConstants.POM));
return Pom.version(pomContent).flatMap(ApiVersion::parse);
}
catch (IOException e) {
throw new SdkException("Error parsing version of source folder '{}'.", sourceFolder, e);
}
}
static Optional<ApiVersion> versionOfJar(Path jar) {
try (var f = new JarFile(jar.toFile(), false, ZipFile.OPEN_READ)) {
return Optional.ofNullable(f.getManifest())
.map(mf -> mf.getMainAttributes().getValue(Name.IMPLEMENTATION_VERSION.toString()))
.flatMap(ApiVersion::parse);
}
catch (IOException e) {
throw new SdkException("Error parsing version of jar '{}'.", jar, e);
}
}
static boolean isModuleSourceFolder(String moduleName, IClasspathEntry entry) {
if (!entry.isDirectory()) {
return false;
}
return entry.path().endsWith(moduleName + '/' + ISourceFolders.MAIN_JAVA_SOURCE_FOLDER);
}
static boolean isModuleJar(String moduleName, IClasspathEntry entry) {
if (entry.isDirectory()) {
return false;
}
var lastSegment = entry.path().getFileName();
if (lastSegment == null) {
return false;
}
var fileName = lastSegment.toString();
// fileName is of form "org.eclipse.scout.rt.platform-10.0.5.jar" or "org.eclipse.scout.rt.platform-10.0.5-sources.jar"
return Strings.endsWith(fileName, ".jar", false)
&& Strings.startsWith(fileName, moduleName + '-')
&& !Strings.endsWith(fileName, "-sources.jar", false)
&& !Strings.endsWith(fileName, "-javadoc.jar", false);
}
}