Coverage Report - org.apache.maven.plugins.enforcer.RequireReleaseDeps
 
Classes in this File Line Coverage Branch Coverage Complexity
RequireReleaseDeps
0%
0/46
0%
0/30
3.375
 
 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  0
 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  0
     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  0
     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  0
     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  0
     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  0
         MavenProject project = null;
 80  0
         if ( onlyWhenRelease )
 81  
         {
 82  
             // get the project
 83  0
             project = getProject( helper );
 84  
 
 85  
             // only call super if this project is a release
 86  0
             callSuper = !project.getArtifact().isSnapshot();
 87  
         }
 88  
         else
 89  
         {
 90  0
             callSuper = true;
 91  
         }
 92  0
         if ( callSuper )
 93  
         {
 94  0
             super.execute( helper );
 95  0
             if ( failWhenParentIsSnapshot )
 96  
             {
 97  0
                 if ( project == null )
 98  
                 {
 99  0
                     project = getProject( helper );
 100  
                 }
 101  0
                 Artifact parentArtifact = project.getParentArtifact();
 102  0
                 if ( parentArtifact != null && parentArtifact.isSnapshot() )
 103  
                 {
 104  0
                     throw new EnforcerRuleException( "Parent Cannot be a snapshot: " + parentArtifact.getId() );
 105  
                 }
 106  
             }
 107  
         }
 108  0
     }
 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  0
             return (MavenProject) helper.evaluate( "${project}" );
 121  
         }
 122  0
         catch ( ExpressionEvaluationException eee )
 123  
         {
 124  0
             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  0
         Set<Artifact> foundSnapshots = new HashSet<Artifact>();
 136  
 
 137  0
         Set<Artifact> filteredDependencies = filterArtifacts( dependencies );
 138  
         
 139  0
         for ( Artifact artifact : filteredDependencies )
 140  
         {
 141  0
             if ( artifact.isSnapshot() )
 142  
             {
 143  0
                 foundSnapshots.add( artifact );
 144  
             }
 145  
         }
 146  
 
 147  0
         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  0
         if ( includes == null && excludes == null )
 160  
         {
 161  0
             return dependencies;
 162  
         }
 163  
         
 164  0
         AndArtifactFilter filter = new AndArtifactFilter( );
 165  0
         if ( includes != null )
 166  
         {
 167  0
             filter.add( new StrictPatternIncludesArtifactFilter( includes ) );
 168  
         }
 169  0
         if ( excludes != null )
 170  
         {
 171  0
             filter.add( new StrictPatternExcludesArtifactFilter( excludes ) );
 172  
         }
 173  
         
 174  0
         Set<Artifact> result = new HashSet<Artifact>();
 175  0
         for ( Artifact artifact : dependencies )
 176  
         {
 177  0
             if ( filter.include( artifact ) )
 178  
             {
 179  0
                 result.add( artifact );
 180  
             }
 181  
         }
 182  0
         return result;
 183  
     }
 184  
 
 185  
     public boolean isOnlyWhenRelease()
 186  
     {
 187  0
         return onlyWhenRelease;
 188  
     }
 189  
 
 190  
     public void setOnlyWhenRelease( boolean onlyWhenRelease )
 191  
     {
 192  0
         this.onlyWhenRelease = onlyWhenRelease;
 193  0
     }
 194  
 
 195  
     public boolean isFailWhenParentIsSnapshot()
 196  
     {
 197  0
         return failWhenParentIsSnapshot;
 198  
     }
 199  
 
 200  
     public void setFailWhenParentIsSnapshot( boolean failWhenParentIsSnapshot )
 201  
     {
 202  0
         this.failWhenParentIsSnapshot = failWhenParentIsSnapshot;
 203  0
     }
 204  
 }