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 import javax.inject.Inject;
25 import javax.inject.Named;
26 import javax.inject.Singleton;
27 import org.apache.commons.lang3.StringUtils;
28 import org.apache.commons.lang3.Validate;
29 import org.apache.maven.rtinfo.RuntimeInformation;
30 import org.eclipse.aether.version.InvalidVersionSpecificationException;
31 import org.eclipse.aether.version.Version;
32 import org.eclipse.aether.version.VersionConstraint;
33 import org.eclipse.aether.version.VersionScheme;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37
38
39
40 @Named
41 @Singleton
42 public class DefaultRuntimeInformation implements RuntimeInformation {
43 private final Logger logger = LoggerFactory.getLogger(getClass());
44
45 private final VersionScheme versionScheme;
46
47 private final String mavenVersion;
48
49 @Inject
50 public DefaultRuntimeInformation(final VersionScheme versionScheme) {
51 this.versionScheme = versionScheme;
52 this.mavenVersion = loadMavenVersion();
53 }
54
55 @Override
56 public String getMavenVersion() {
57 return mavenVersion;
58 }
59
60 private String loadMavenVersion() {
61 Properties props = new Properties();
62
63 String resource = "META-INF/maven/org.apache.maven/maven-core/pom.properties";
64
65 try (InputStream is = DefaultRuntimeInformation.class.getResourceAsStream("/" + resource)) {
66 if (is != null) {
67 props.load(is);
68 } else {
69 logger.warn("Could not locate " + resource + " on classpath, Maven runtime information not available");
70 }
71 } catch (IOException e) {
72 String msg = "Could not parse " + resource + ", Maven runtime information not available";
73 if (logger.isDebugEnabled()) {
74 logger.warn(msg, e);
75 } else {
76 logger.warn(msg);
77 }
78 }
79
80 String version = props.getProperty("version", "").trim();
81
82 if (!version.startsWith("${")) {
83 return version;
84 } else {
85 return "";
86 }
87 }
88
89 @Override
90 public boolean isMavenVersion(String versionRange) {
91 Validate.notBlank(versionRange, "versionRange can neither be null, empty nor blank");
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 Validate.validState(StringUtils.isNotEmpty(mavenVersion), "Could not determine current Maven version");
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 }