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