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.apache.maven.rtinfo.RuntimeInformation;
28  import org.slf4j.Logger;
29  import org.slf4j.LoggerFactory;
30  
31  @Named
32  @Singleton
33  public class MavenPluginMavenPrerequisiteChecker implements MavenPluginPrerequisitesChecker {
34      private final Logger logger = LoggerFactory.getLogger(getClass());
35      private final RuntimeInformation runtimeInformation;
36  
37      @Inject
38      public MavenPluginMavenPrerequisiteChecker(RuntimeInformation runtimeInformation) {
39          super();
40          this.runtimeInformation = runtimeInformation;
41      }
42  
43      @Override
44      public void accept(PluginDescriptor pluginDescriptor) {
45          String requiredMavenVersion = pluginDescriptor.getRequiredMavenVersion();
46  
47          boolean isBlankVersion =
48                  requiredMavenVersion == null || requiredMavenVersion.trim().isEmpty();
49  
50          if (!isBlankVersion) {
51              boolean isRequirementMet = false;
52              try {
53                  isRequirementMet = runtimeInformation.isMavenVersion(requiredMavenVersion);
54              } catch (IllegalArgumentException e) {
55                  logger.warn(
56                          "Could not verify plugin's Maven prerequisite as an invalid version is given in "
57                                  + requiredMavenVersion,
58                          e);
59                  return;
60              }
61              if (!isRequirementMet) {
62                  throw new IllegalStateException("Required Maven version " + requiredMavenVersion
63                          + " is not met by current version " + runtimeInformation.getMavenVersion());
64              }
65          }
66      }
67  }