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.HashSet;
23  import java.util.List;
24  import java.util.Set;
25  
26  import org.apache.maven.artifact.Artifact;
27  import org.apache.maven.artifact.resolver.filter.AndArtifactFilter;
28  import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
29  import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
30  import org.apache.maven.plugin.logging.Log;
31  import org.apache.maven.project.MavenProject;
32  import org.apache.maven.shared.artifact.filter.StrictPatternExcludesArtifactFilter;
33  import org.apache.maven.shared.artifact.filter.StrictPatternIncludesArtifactFilter;
34  import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
35  
36  /**
37   * This rule checks that no snapshots are included.
38   *
39   * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
40   * @version $Id: RequireReleaseDeps.java 1634140 2014-10-24 21:23:01Z khmarbaise $
41   */
42  public class RequireReleaseDeps
43      extends AbstractBanDependencies
44  {
45  
46      /**
47       * Allows this rule to execute only when this project is a release.
48       *
49       * @parameter
50       * 
51       * @see {@link #setOnlyWhenRelease(boolean)}
52       * @see {@link #isOnlyWhenRelease()}
53  
54       */
55      private boolean onlyWhenRelease = false;
56  
57      /**
58       * Allows this rule to fail when the parent is defined as a snapshot.
59       *
60       * @parameter
61       * 
62       * @see {@link #setFailWhenParentIsSnapshot(boolean)}
63       * @see {@link #isFailWhenParentIsSnapshot()}
64       */
65      private boolean failWhenParentIsSnapshot = true;
66  
67      /**
68       * Dependencies to ignore when checking for release versions.  For example, inter-module dependencies 
69       * can be excluded from the check and therefore allowed to contain snapshot versions.
70       * 
71       * @see {@link #setExcludes(List)}
72       * @see {@link #getExcludes()}
73       */
74      private List<String> excludes = null;
75  
76      /**
77       * Dependencies to include when checking for release versions.  If any of the included dependencies
78       * have snapshot versions, the rule will fail.
79       * 
80       * @see {@link #setIncludes(List)}
81       * @see {@link #getIncludes()}
82       */
83      private List<String> includes = null;
84  
85      /**
86       * Override parent to allow optional ignore of this rule.
87       * 
88       * @param helper the enforcerRuleHelper
89       * @throws EnforcerRuleException when an exception occurs
90       */
91      public void execute( EnforcerRuleHelper helper )
92          throws EnforcerRuleException
93      {
94          boolean callSuper;
95          MavenProject project = null;
96          if ( onlyWhenRelease )
97          {
98              // get the project
99              project = getProject( helper );
100 
101             // only call super if this project is a release
102             callSuper = !project.getArtifact().isSnapshot();
103         }
104         else
105         {
106             callSuper = true;
107         }
108         if ( callSuper )
109         {
110             super.execute( helper );
111             if ( failWhenParentIsSnapshot )
112             {
113                 if ( project == null )
114                 {
115                     project = getProject( helper );
116                 }
117                 Artifact parentArtifact = project.getParentArtifact();
118                 if ( parentArtifact != null && parentArtifact.isSnapshot() )
119                 {
120                     throw new EnforcerRuleException( "Parent Cannot be a snapshot: " + parentArtifact.getId() );
121                 }
122             }
123         }
124     }
125 
126     /**
127      * @param helper
128      * @return
129      * @throws EnforcerRuleException
130      */
131     private MavenProject getProject( EnforcerRuleHelper helper )
132         throws EnforcerRuleException
133     {
134         try
135         {
136             return (MavenProject) helper.evaluate( "${project}" );
137         }
138         catch ( ExpressionEvaluationException eee )
139         {
140             throw new EnforcerRuleException( "Unable to retrieve the MavenProject: ", eee );
141         }
142     }
143 
144 
145     /**
146      * {@inheritDoc}
147      */
148     protected Set<Artifact> checkDependencies( Set<Artifact> dependencies, Log log )
149         throws EnforcerRuleException
150     {
151         Set<Artifact> foundSnapshots = new HashSet<Artifact>();
152 
153         Set<Artifact> filteredDependencies = filterArtifacts( dependencies );
154         
155         for ( Artifact artifact : filteredDependencies )
156         {
157             if ( artifact.isSnapshot() )
158             {
159                 foundSnapshots.add( artifact );
160             }
161         }
162 
163         return foundSnapshots;
164     }
165     
166     /*
167      * Filter the dependency artifacts according to the includes and excludes
168      * If includes and excludes are both null, the original set is returned.
169      * 
170      * @param dependencies the list of dependencies to filter
171      * @return the resulting set of dependencies
172      */
173     public Set<Artifact> filterArtifacts( Set<Artifact> dependencies )
174     {
175         if ( includes == null && excludes == null )
176         {
177             return dependencies;
178         }
179         
180         AndArtifactFilter filter = new AndArtifactFilter( );
181         if ( includes != null )
182         {
183             filter.add( new StrictPatternIncludesArtifactFilter( includes ) );
184         }
185         if ( excludes != null )
186         {
187             filter.add( new StrictPatternExcludesArtifactFilter( excludes ) );
188         }
189         
190         Set<Artifact> result = new HashSet<Artifact>();
191         for ( Artifact artifact : dependencies )
192         {
193             if ( filter.include( artifact ) )
194             {
195                 result.add( artifact );
196             }
197         }
198         return result;
199     }
200 
201     public final boolean isOnlyWhenRelease()
202     {
203         return onlyWhenRelease;
204     }
205 
206     public final void setOnlyWhenRelease( boolean onlyWhenRelease )
207     {
208         this.onlyWhenRelease = onlyWhenRelease;
209     }
210 
211     public final boolean isFailWhenParentIsSnapshot()
212     {
213         return failWhenParentIsSnapshot;
214     }
215 
216     public final void setFailWhenParentIsSnapshot( boolean failWhenParentIsSnapshot )
217     {
218         this.failWhenParentIsSnapshot = failWhenParentIsSnapshot;
219     }
220     
221     public final void setExcludes( List<String> excludes )
222     {
223         this.excludes = excludes;
224     }
225     
226     public final List<String> getExcludes()
227     {
228         return excludes;
229     }
230     
231     public void setIncludes( List<String> includes )
232     {
233         this.includes = includes;
234     }
235     
236     public List<String> getIncludes()
237     {
238         return includes;
239     }
240 }