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