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