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 javax.inject.Named;
22 import javax.inject.Singleton;
23
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.util.Objects;
27 import java.util.Properties;
28
29 import org.apache.maven.rtinfo.RuntimeInformation;
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 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38
39
40
41 @Singleton
42 @Named
43 public class DefaultRuntimeInformation implements RuntimeInformation {
44
45 private final Logger logger = LoggerFactory.getLogger(getClass());
46
47 private String mavenVersion;
48
49 public String getMavenVersion() {
50 if (mavenVersion == null) {
51 Properties props = new Properties();
52
53 String resource = "META-INF/maven/org.apache.maven/maven-core/pom.properties";
54
55 try (InputStream is = DefaultRuntimeInformation.class.getResourceAsStream("/" + resource)) {
56 if (is != null) {
57 props.load(is);
58 } else {
59 logger.warn(
60 "Could not locate " + resource + " on classpath, Maven runtime information not available");
61 }
62 } catch (IOException e) {
63 String msg = "Could not parse " + resource + ", Maven runtime information not available";
64 if (logger.isDebugEnabled()) {
65 logger.warn(msg, e);
66 } else {
67 logger.warn(msg);
68 }
69 }
70
71 String version = props.getProperty("version", "").trim();
72
73 if (!version.startsWith("${")) {
74 mavenVersion = version;
75 } else {
76 mavenVersion = "";
77 }
78 }
79
80 return mavenVersion;
81 }
82
83 private static final String VERSION_RANGE_ERROR_MESSAGE = "versionRange can neither be null, empty, nor blank";
84
85 public boolean isMavenVersion(String versionRange) {
86 if (Objects.requireNonNull(versionRange, VERSION_RANGE_ERROR_MESSAGE)
87 .trim()
88 .isEmpty()) {
89 throw new IllegalArgumentException(VERSION_RANGE_ERROR_MESSAGE);
90 }
91 VersionScheme versionScheme = new GenericVersionScheme();
92
93 VersionConstraint constraint;
94 try {
95 constraint = versionScheme.parseVersionConstraint(versionRange);
96 } catch (InvalidVersionSpecificationException e) {
97 throw new IllegalArgumentException(e.getMessage(), e);
98 }
99
100 Version current;
101 try {
102 String mavenVersion = getMavenVersion();
103 if (mavenVersion == null || mavenVersion.trim().isEmpty()) {
104 throw new IllegalStateException("Could not determine current Maven version");
105 }
106
107 current = versionScheme.parseVersion(mavenVersion);
108 } catch (InvalidVersionSpecificationException e) {
109 throw new IllegalStateException("Could not parse current Maven version: " + e.getMessage(), e);
110 }
111
112 if (constraint.getRange() == null) {
113 return constraint.getVersion().compareTo(current) <= 0;
114 }
115 return constraint.containsVersion(current);
116 }
117 }