View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * Provides information about the current Maven runtime.
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 }