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