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