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.enforcer.rules;
20  
21  import javax.inject.Inject;
22  import javax.inject.Named;
23  
24  import java.util.List;
25  import java.util.Objects;
26  
27  import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
28  import org.apache.maven.artifact.versioning.VersionRange;
29  import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
30  import org.apache.maven.model.Prerequisites;
31  import org.apache.maven.project.MavenProject;
32  
33  /**
34   * @author Robert Scholte
35   * @since 1.3
36   */
37  @Named("requirePrerequisite")
38  public final class RequirePrerequisite extends AbstractStandardEnforcerRule {
39      /**
40       * Only the projects with one of these packagings will be enforced to have the correct prerequisite.
41       *
42       * @since 1.4
43       */
44      private List<String> packagings;
45  
46      /**
47       * Can either be version or a range, e.g. {@code 2.2.1} or {@code [2.2.1,)}
48       */
49      private String mavenVersion;
50  
51      private final MavenProject project;
52  
53      @Inject
54      public RequirePrerequisite(MavenProject project) {
55          this.project = Objects.requireNonNull(project);
56      }
57  
58      /**
59       * Set the mavenVersion Can either be version or a range, e.g. {@code 2.2.1} or {@code [2.2.1,)}
60       *
61       * @param mavenVersion the version or {@code null}
62       */
63      public void setMavenVersion(String mavenVersion) {
64          this.mavenVersion = mavenVersion;
65      }
66  
67      /**
68       * Only the projects with one of these packagings will be enforced to have the correct prerequisite.
69       *
70       * @since 1.4
71       * @param packagings the list of packagings
72       */
73      public void setPackagings(List<String> packagings) {
74          this.packagings = packagings;
75      }
76  
77      @Override
78      public void execute() throws EnforcerRuleException {
79          try {
80  
81              if ("pom".equals(project.getPackaging())) {
82                  getLog().debug("Packaging is pom, skipping requirePrerequisite rule");
83                  return;
84              }
85  
86              if (packagings != null && !packagings.contains(project.getPackaging())) {
87                  getLog().debug("Packaging is " + project.getPackaging() + ", skipping requirePrerequisite rule");
88                  return;
89              }
90  
91              Prerequisites prerequisites = project.getPrerequisites();
92  
93              if (prerequisites == null) {
94                  throw new EnforcerRuleException("Requires prerequisite not set");
95              }
96  
97              if (mavenVersion != null) {
98  
99                  VersionRange requiredVersionRange = VersionRange.createFromVersionSpec(mavenVersion);
100 
101                 if (!requiredVersionRange.hasRestrictions()) {
102                     requiredVersionRange = VersionRange.createFromVersionSpec("[" + mavenVersion + ",)");
103                 }
104 
105                 VersionRange specifiedVersion = VersionRange.createFromVersionSpec(prerequisites.getMaven());
106 
107                 VersionRange restrictedVersionRange = requiredVersionRange.restrict(specifiedVersion);
108 
109                 if (restrictedVersionRange.getRecommendedVersion() == null) {
110                     throw new EnforcerRuleException("The specified Maven prerequisite( " + specifiedVersion
111                             + " ) doesn't match the required version: " + mavenVersion);
112                 }
113             }
114         } catch (InvalidVersionSpecificationException e) {
115             throw new EnforcerRuleException(e.getMessage(), e);
116         }
117     }
118 
119     @Override
120     public String toString() {
121         return String.format("RequirePrerequisite[packagings=%s, mavenVersion=%s]", packagings, mavenVersion);
122     }
123 }