-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathPOM.java
More file actions
379 lines (333 loc) · 11.2 KB
/
POM.java
File metadata and controls
379 lines (333 loc) · 11.2 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
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/*
* #%L
* SciJava Common shared library for SciJava software.
* %%
* Copyright (C) 2009 - 2026 SciJava developers.
* %%
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* #L%
*/
package org.scijava.util;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Enumeration;
import java.util.List;
import javax.xml.parsers.ParserConfigurationException;
import org.scijava.Context;
import org.scijava.Versioned;
import org.xml.sax.SAXException;
/**
* Helper class for working with Maven POMs.
*
* @author Curtis Rueden
*/
public class POM extends XML implements Comparable<POM>, Versioned {
private String version;
/** Parses a POM from the given file. */
public POM(final File file) throws ParserConfigurationException,
SAXException, IOException
{
super(file);
}
/** Parses a POM from the given URL. */
public POM(final URL url) throws ParserConfigurationException, SAXException,
IOException
{
super(url);
}
/** Parses a POM from the given input stream. */
public POM(final InputStream in) throws ParserConfigurationException,
SAXException, IOException
{
super(in);
}
/** Parses a POM from the given string. */
public POM(final String s) throws ParserConfigurationException, SAXException,
IOException
{
super(s);
}
// -- POM methods --
/** Gets the POM's parent groupId. */
public String getParentGroupId() {
return cdata("//project/parent/groupId");
}
/** Gets the POM's parent artifactId. */
public String getParentArtifactId() {
return cdata("//project/parent/artifactId");
}
/** Gets the POM's parent artifactId. */
public String getParentVersion() {
return cdata("//project/parent/version");
}
/** Gets the POM's groupId. */
public String getGroupId() {
final String groupId = cdata("//project/groupId");
if (groupId != null) return groupId;
return getParentGroupId();
}
/** Gets the POM's artifactId. */
public String getArtifactId() {
return cdata("//project/artifactId");
}
/** Gets the project name. */
public String getProjectName() {
return cdata("//project/name");
}
/** Gets the project description. */
public String getProjectDescription() {
return cdata("//project/description");
}
/** Gets the project URL. */
public String getProjectURL() {
return cdata("//project/url");
}
/** Gets the project inception year. */
public String getProjectInceptionYear() {
return cdata("//project/inceptionYear");
}
/** Gets the organization name. */
public String getOrganizationName() {
return cdata("//project/organization/name");
}
/** Gets the organization URL. */
public String getOrganizationURL() {
return cdata("//project/organization/url");
}
/** Gets the SCM connection string. */
public String getSCMConnection() {
return cdata("//project/scm/connection");
}
/** Gets the SCM developerConnection string. */
public String getSCMDeveloperConnection() {
return cdata("//project/scm/developerConnection");
}
/** Gets the SCM tag. */
public String getSCMTag() {
return cdata("//project/scm/tag");
}
/** Gets the SCM URL. */
public String getSCMURL() {
return cdata("//project/scm/url");
}
/** Gets the issue management system. */
public String getIssueManagementSystem() {
return cdata("//project/issueManagement/system");
}
/** Gets the issue management URL. */
public String getIssueManagementURL() {
return cdata("//project/issueManagement/url");
}
/** Gets the CI management system. */
public String getCIManagementSystem() {
return cdata("//project/ciManagement/system");
}
/** Gets the CI management URL. */
public String getCIManagementURL() {
return cdata("//project/ciManagement/url");
}
// -- Comparable methods --
private static final Comparator<String> STRING_COMPARATOR = //
Comparator.nullsFirst(String::compareTo);
private static final Comparator<POM> POM_COMPARATOR = Comparator//
// sort by groupId first
.comparing(POM::getGroupId, STRING_COMPARATOR)
// sort by artifactId second
.thenComparing(POM::getArtifactId, STRING_COMPARATOR)//
// finally, sort by version
.thenComparing(POM::getVersion, POM::compareVersions);
@Override
public int compareTo(final POM pom) {
return POM_COMPARATOR.compare(this, pom);
}
// -- Versioned methods --
/** Gets the POM's version. */
@Override
public String getVersion() {
if (version == null) {
synchronized (this) {
if (version == null) {
version = cdata("//project/version");
if (version == null) version = getParentVersion();
}
}
}
return version;
}
// -- Utility methods --
/**
* Gets the Maven POM associated with the given class.
*
* @param c The class to use as a base when searching for a pom.xml.
* @return {@link POM} object representing the discovered POM, or null if no
* POM could be found.
*/
public static POM getPOM(final Class<?> c) {
return getPOM(c, null, null);
}
/**
* Gets the Maven POM associated with the given class.
*
* @param c The class to use as a base when searching for a pom.xml.
* @param groupId The Maven groupId of the desired POM.
* @param artifactId The Maven artifactId of the desired POM.
* @return {@link POM} object representing the discovered POM, or null if no
* POM could be found.
*/
public static POM getPOM(final Class<?> c, final String groupId,
final String artifactId)
{
try {
final URL location = Types.location(c);
if (!location.getProtocol().equals("file") ||
location.toString().endsWith(".jar"))
{
// look for pom.xml in JAR's META-INF/maven subdirectory
if (groupId == null || artifactId == null) {
// groupId and/or artifactId is unknown; scan for the POM
final URL pomBase = new URL("jar:" + //
location.toString() + "!/META-INF/maven");
for (final URL url : FileUtils.listContents(pomBase, true, true)) {
if (url.toExternalForm().endsWith("/pom.xml")) {
return new POM(url);
}
}
}
else {
// known groupId and artifactId; grab it directly
final String pomPath =
"META-INF/maven/" + groupId + "/" + artifactId + "/pom.xml";
final URL pomURL =
new URL("jar:" + location.toString() + "!/" + pomPath);
return new POM(pomURL);
}
}
// look for the POM in the class's base directory
final File file = FileUtils.urlToFile(location);
final File baseDir = AppUtils.getBaseDirectory(file, null);
final File pomFile = new File(baseDir, "pom.xml");
return new POM(pomFile);
}
catch (final IOException | ParserConfigurationException | SAXException e) {
return null;
}
}
/** Gets all available Maven POMs on the class path. */
public static List<POM> getAllPOMs() {
// find all META-INF/maven/ folders on the classpath
final String pomPrefix = "META-INF/maven/";
final ClassLoader classLoader = Context.getClassLoader();
final Enumeration<URL> resources;
try {
resources = classLoader.getResources(pomPrefix);
}
catch (final IOException exc) {
return null;
}
final ArrayList<POM> poms = new ArrayList<>();
// recursively list contents of META-INF/maven/ directories
for (final URL resource : new IteratorPlus<>(resources)) {
for (final URL url : FileUtils.listContents(resource)) {
// look for pom.xml files amongst the contents
if (url.getPath().endsWith("/pom.xml")) {
try {
poms.add(new POM(url));
}
catch (final IOException exc) {
// ignore and continue
}
catch (final ParserConfigurationException exc) {
// ignore and continue
}
catch (final SAXException exc) {
// ignore and continue
}
}
}
}
return poms;
}
/**
* Compares two version strings.
* <p>
* Given the variation between versioning styles, there is no single
* comparison method that can possibly be correct 100% of the time. So this
* method works on a "best effort" basis; YMMV.
* </p>
* <p>
* The algorithm is as follows:
* </p>
* <ul>
* <li>Split on non-alphameric characters.</li>
* <li>Compare each token one by one.</li>
* <li>Comparison is numerical when possible (i.e., when an integer can be
* parsed from the token), and lexicographic otherwise.</li>
* <li>If one version string runs out of tokens, the version with additional
* tokens remaining is considered <em>greater than</em> the version without
* additional tokens.</li>
* <li>There is one exception: if two version strings are identical except
* that one has a suffix beginning with a dash ({@code -}), the version with
* suffix will be considered <em>less than</em> the one without a suffix. The
* reason for this is to accommodate the <a
* href="https://semver.org/">SemVer</a> versioning scheme's usage of
* "prerelease" version suffixes. For example, {@code 2.0.0} will compare
* greater than {@code 2.0.0-beta-1}, whereas {@code 2.0.0} will compare less
* than {@code 2.0.0.1}.</li>
* </ul>
*
* @return a negative integer, zero, or a positive integer as the first
* argument is less than, equal to, or greater than the second.
* @see Comparator#compare(Object, Object)
*/
public static int compareVersions(final String v1, final String v2) {
final String[] t1 = v1.split("[^\\w]");
final String[] t2 = v2.split("[^\\w]");
final int size = Math.min(t1.length, t2.length);
for (int i = 0; i < size; i++) {
try {
final long n1 = Long.parseLong(t1[i]);
final long n2 = Long.parseLong(t2[i]);
if (n1 < n2) return -1;
if (n1 > n2) return 1;
}
catch (final NumberFormatException exc) {
final int result = t1[i].compareTo(t2[i]);
if (result != 0) return result;
}
}
if (t1.length == t2.length) return 0; // versions match
// check for SemVer prerelease versions
if (v1.startsWith(v2) && v1.charAt(v2.length()) == '-') {
// v1 is a prerelease version of v2
return -1;
}
if (v2.startsWith(v1) && v2.charAt(v1.length()) == '-') {
// v2 is a prerelease version of v1
return 1;
}
return t1.length < t2.length ? -1 : 1;
}
}