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