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 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   * Provides information about the current Maven runtime.
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 }