View Javadoc
1   package org.apache.maven.plugins.enforcer;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.List;
23  
24  import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
25  import org.apache.maven.artifact.versioning.VersionRange;
26  import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
27  import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
28  import org.apache.maven.model.Prerequisites;
29  import org.apache.maven.project.MavenProject;
30  import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
31  
32  /**
33   * @author Robert Scholte
34   * @since 1.3
35   */
36  public class RequirePrerequisite
37      extends AbstractNonCacheableEnforcerRule
38  {
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      /**
52       * Set the mavenVersion Can either be version or a range, e.g. {@code 2.2.1} or {@code [2.2.1,)}
53       * 
54       * @param mavenVersion the version or {@code null}
55       */
56      public void setMavenVersion( String mavenVersion )
57      {
58          this.mavenVersion = mavenVersion;
59      }
60  
61      /**
62       * Only the projects with one of these packagings will be enforced to have the correct prerequisite.
63       * 
64       * @since 1.4
65       * @param packagings the list of packagings
66       */
67      public void setPackagings( List<String> packagings )
68      {
69          this.packagings = packagings;
70      }
71  
72      @Override
73      public void execute( EnforcerRuleHelper helper )
74          throws EnforcerRuleException
75      {
76          try
77          {
78              MavenProject project = (MavenProject) helper.evaluate( "${project}" );
79  
80              if ( "pom".equals( project.getPackaging() ) )
81              {
82                  helper.getLog().debug( "Packaging is pom, skipping requirePrerequisite rule" );
83                  return;
84              }
85  
86              if ( packagings != null && !packagings.contains( project.getPackaging() ) )
87              {
88                  // CHECKSTYLE_OFF: LineLength
89                  helper.getLog().debug( "Packaging is " + project.getPackaging() + ", skipping requirePrerequisite rule" );
90                  return;
91                  // CHECKSTYLE_ON: LineLength
92              }
93  
94              Prerequisites prerequisites = project.getPrerequisites();
95  
96              if ( prerequisites == null )
97              {
98                  throw new EnforcerRuleException( "Requires prerequisite not set" );
99              }
100 
101             if ( mavenVersion != null )
102             {
103 
104                 VersionRange requiredVersionRange = VersionRange.createFromVersionSpec( mavenVersion );
105 
106                 if ( !requiredVersionRange.hasRestrictions() )
107                 {
108                     requiredVersionRange = VersionRange.createFromVersionSpec( "[" + mavenVersion + ",)" );
109                 }
110 
111                 VersionRange specifiedVersion = VersionRange.createFromVersionSpec( prerequisites.getMaven() );
112 
113                 VersionRange restrictedVersionRange = requiredVersionRange.restrict( specifiedVersion );
114 
115                 if ( restrictedVersionRange.getRecommendedVersion() == null )
116                 {
117                     throw new EnforcerRuleException( "The specified Maven prerequisite( " + specifiedVersion
118                         + " ) doesn't match the required version: " + mavenVersion );
119                 }
120             }
121         }
122         catch ( ExpressionEvaluationException e )
123         {
124             throw new EnforcerRuleException( e.getMessage(), e );
125         }
126         catch ( InvalidVersionSpecificationException e )
127         {
128             throw new EnforcerRuleException( e.getMessage(), e );
129         }
130     }
131 
132 }