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.plugin.internal;
20  
21  import javax.inject.Named;
22  import javax.inject.Singleton;
23  
24  import org.apache.maven.plugin.MavenPluginPrerequisitesChecker;
25  import org.apache.maven.plugin.descriptor.PluginDescriptor;
26  import org.eclipse.aether.util.version.GenericVersionScheme;
27  import org.eclipse.aether.version.InvalidVersionSpecificationException;
28  import org.eclipse.aether.version.Version;
29  import org.eclipse.aether.version.VersionConstraint;
30  import org.eclipse.aether.version.VersionScheme;
31  
32  @Named
33  @Singleton
34  public class MavenPluginJavaPrerequisiteChecker implements MavenPluginPrerequisitesChecker {
35      private final VersionScheme versionScheme = new GenericVersionScheme();
36  
37      @Override
38      public void accept(PluginDescriptor pluginDescriptor) {
39          String requiredJavaVersion = pluginDescriptor.getRequiredJavaVersion();
40          if (requiredJavaVersion != null && !requiredJavaVersion.isEmpty()) {
41              String currentJavaVersion = System.getProperty("java.version");
42              if (!matchesVersion(requiredJavaVersion, currentJavaVersion)) {
43                  throw new IllegalStateException("Required Java version " + requiredJavaVersion
44                          + " is not met by current version: " + currentJavaVersion);
45              }
46          }
47      }
48  
49      boolean matchesVersion(String requiredVersion, String currentVersion) {
50          // Java 8 is consistent: it will always say "1.8...". Users are not consistent.
51          if (!requiredVersion.contains("1.8") && currentVersion.startsWith("1.8")) {
52              currentVersion = currentVersion.substring(2);
53          }
54          VersionConstraint constraint;
55          try {
56              constraint = versionScheme.parseVersionConstraint(requiredVersion);
57          } catch (InvalidVersionSpecificationException e) {
58              throw new IllegalArgumentException("Invalid 'requiredJavaVersion' given in plugin descriptor", e);
59          }
60          Version current;
61          try {
62              current = versionScheme.parseVersion(currentVersion);
63          } catch (InvalidVersionSpecificationException e) {
64              throw new IllegalStateException("Could not parse current Java version", e);
65          }
66          if (constraint.getRange() == null) {
67              return constraint.getVersion().compareTo(current) <= 0;
68          }
69          return constraint.containsVersion(current);
70      }
71  }