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