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 org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
23  import org.apache.maven.artifact.versioning.VersionRange;
24  import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
25  import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
26  import org.apache.maven.model.Prerequisites;
27  import org.apache.maven.project.MavenProject;
28  import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
29  
30  /**
31   * 
32   * @author Robert Scholte
33   * @since 1.3
34   */
35  public class RequirePrerequisite extends AbstractNonCacheableEnforcerRule
36  {
37      /**
38       * Can either be version or a range, e.g. {@code 2.2.1} or {@code [2.2.1,)}
39       */
40      private String mavenVersion;
41  
42      /**
43       * Set the mavenVersion
44       * 
45       * Can either be version or a range, e.g. {@code 2.2.1} or {@code [2.2.1,)}
46       * 
47       * @param mavenVersion the version or {@code null}
48       */
49      public void setMavenVersion( String mavenVersion )
50      {
51          this.mavenVersion = mavenVersion;
52      }
53      
54      /**
55       * {@inheritDoc}
56       */
57      public void execute( EnforcerRuleHelper helper )
58          throws EnforcerRuleException
59      {
60          try
61          {
62              MavenProject project = (MavenProject) helper.evaluate( "${project}" );
63              
64              Prerequisites prerequisites = project.getPrerequisites(); 
65              
66              if( prerequisites == null )
67              {
68                  throw new EnforcerRuleException( "Requires prerequisite not set" );
69              }
70  
71              if( mavenVersion != null )
72              {
73                  
74                  VersionRange requiredVersionRange = VersionRange.createFromVersionSpec( mavenVersion );
75                  
76                  if( !requiredVersionRange.hasRestrictions() )
77                  {
78                      requiredVersionRange = VersionRange.createFromVersionSpec( "[" + mavenVersion + ",)" );
79                  }
80                  
81                  VersionRange specifiedVersion = VersionRange.createFromVersionSpec( prerequisites.getMaven() );
82                  
83                  VersionRange restrictedVersionRange = requiredVersionRange.restrict( specifiedVersion );
84                  
85                  if ( restrictedVersionRange.getRecommendedVersion() == null )
86                  {
87                      throw new EnforcerRuleException( "The specified Maven prerequisite( " + specifiedVersion + " ) doesn't match the required version: " + mavenVersion );
88                  }
89              }
90          }
91          catch ( ExpressionEvaluationException e )
92          {
93              throw new EnforcerRuleException( e.getMessage(), e );
94          }
95          catch ( InvalidVersionSpecificationException e )
96          {
97              throw new EnforcerRuleException( e.getMessage(), e );
98          }
99      }
100 
101 }