1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.rtinfo.internal;
20
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.util.Objects;
24 import java.util.Properties;
25
26 import org.apache.maven.rtinfo.RuntimeInformation;
27 import org.codehaus.plexus.component.annotations.Component;
28 import org.codehaus.plexus.component.annotations.Requirement;
29 import org.codehaus.plexus.logging.Logger;
30 import org.eclipse.aether.util.version.GenericVersionScheme;
31 import org.eclipse.aether.version.InvalidVersionSpecificationException;
32 import org.eclipse.aether.version.Version;
33 import org.eclipse.aether.version.VersionConstraint;
34 import org.eclipse.aether.version.VersionScheme;
35
36
37
38
39 @Component(role = RuntimeInformation.class)
40 public class DefaultRuntimeInformation implements RuntimeInformation {
41
42 @Requirement
43 private Logger logger;
44
45 private String mavenVersion;
46
47 public String getMavenVersion() {
48 if (mavenVersion == null) {
49 Properties props = new Properties();
50
51 String resource = "META-INF/maven/org.apache.maven/maven-core/pom.properties";
52
53 try (InputStream is = DefaultRuntimeInformation.class.getResourceAsStream("/" + resource)) {
54 if (is != null) {
55 props.load(is);
56 } else {
57 logger.warn(
58 "Could not locate " + resource + " on classpath, Maven runtime information not available");
59 }
60 } catch (IOException e) {
61 String msg = "Could not parse " + resource + ", Maven runtime information not available";
62 if (logger.isDebugEnabled()) {
63 logger.warn(msg, e);
64 } else {
65 logger.warn(msg);
66 }
67 }
68
69 String version = props.getProperty("version", "").trim();
70
71 if (!version.startsWith("${")) {
72 mavenVersion = version;
73 } else {
74 mavenVersion = "";
75 }
76 }
77
78 return mavenVersion;
79 }
80
81 private static final String VERSION_RANGE_ERROR_MESSAGE = "versionRange can neither be null, empty, nor blank";
82
83 public boolean isMavenVersion(String versionRange) {
84 if (Objects.requireNonNull(versionRange, VERSION_RANGE_ERROR_MESSAGE)
85 .trim()
86 .isEmpty()) {
87 throw new IllegalArgumentException(VERSION_RANGE_ERROR_MESSAGE);
88 }
89 VersionScheme versionScheme = new GenericVersionScheme();
90
91 VersionConstraint constraint;
92 try {
93 constraint = versionScheme.parseVersionConstraint(versionRange);
94 } catch (InvalidVersionSpecificationException e) {
95 throw new IllegalArgumentException(e.getMessage(), e);
96 }
97
98 Version current;
99 try {
100 String mavenVersion = getMavenVersion();
101 if (mavenVersion == null || mavenVersion.trim().isEmpty()) {
102 throw new IllegalStateException("Could not determine current Maven version");
103 }
104
105 current = versionScheme.parseVersion(mavenVersion);
106 } catch (InvalidVersionSpecificationException e) {
107 throw new IllegalStateException("Could not parse current Maven version: " + e.getMessage(), e);
108 }
109
110 if (constraint.getRange() == null) {
111 return constraint.getVersion().compareTo(current) <= 0;
112 }
113 return constraint.containsVersion(current);
114 }
115 }