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