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