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      /**
73       * {@inheritDoc}
74       */
75      public void execute( EnforcerRuleHelper helper )
76          throws EnforcerRuleException
77      {
78          try
79          {
80              MavenProject project = (MavenProject) helper.evaluate( "${project}" );
81  
82              if ( "pom".equals( project.getPackaging() ) )
83              {
84                  helper.getLog().debug( "Packaging is pom, skipping requirePrerequisite rule" );
85                  return;
86              }
87  
88              if ( packagings != null && !packagings.contains( project.getPackaging() ) )
89              {
90                  // CHECKSTYLE_OFF: LineLength
91                  helper.getLog().debug( "Packaging is " + project.getPackaging() + ", skipping requirePrerequisite rule" );
92                  return;
93                  // CHECKSTYLE_ON: LineLength
94              }
95  
96              Prerequisites prerequisites = project.getPrerequisites();
97  
98              if ( prerequisites == null )
99              {
100                 throw new EnforcerRuleException( "Requires prerequisite not set" );
101             }
102 
103             if ( mavenVersion != null )
104             {
105 
106                 VersionRange requiredVersionRange = VersionRange.createFromVersionSpec( mavenVersion );
107 
108                 if ( !requiredVersionRange.hasRestrictions() )
109                 {
110                     requiredVersionRange = VersionRange.createFromVersionSpec( "[" + mavenVersion + ",)" );
111                 }
112 
113                 VersionRange specifiedVersion = VersionRange.createFromVersionSpec( prerequisites.getMaven() );
114 
115                 VersionRange restrictedVersionRange = requiredVersionRange.restrict( specifiedVersion );
116 
117                 if ( restrictedVersionRange.getRecommendedVersion() == null )
118                 {
119                     throw new EnforcerRuleException( "The specified Maven prerequisite( " + specifiedVersion
120                         + " ) doesn't match the required version: " + mavenVersion );
121                 }
122             }
123         }
124         catch ( ExpressionEvaluationException e )
125         {
126             throw new EnforcerRuleException( e.getMessage(), e );
127         }
128         catch ( InvalidVersionSpecificationException e )
129         {
130             throw new EnforcerRuleException( e.getMessage(), e );
131         }
132     }
133 
134 }