Coverage Report - org.apache.maven.plugins.enforcer.AbstractBanDependencies
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractBanDependencies
0%
0/30
0%
0/10
2
 
 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.Set;
 23  
 
 24  
 import org.apache.maven.artifact.Artifact;
 25  
 import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
 26  
 import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
 27  
 import org.apache.maven.plugin.logging.Log;
 28  
 import org.apache.maven.project.MavenProject;
 29  
 import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
 30  
 
 31  
 /**
 32  
  * Abstract Rule for banning dependencies.
 33  
  *
 34  
  * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
 35  
  * @version $Id: AbstractBanDependencies.java 1345332 2012-06-01 20:14:13Z rfscholte $
 36  
  */
 37  0
 public abstract class AbstractBanDependencies
 38  
     extends AbstractNonCacheableEnforcerRule
 39  
 {
 40  
 
 41  
     /** Specify if transitive dependencies should be searched (default) or only look at direct dependencies. */
 42  0
     private boolean searchTransitive = true;
 43  
 
 44  
     /**
 45  
      * Execute the rule.
 46  
      *
 47  
      * @param helper the helper
 48  
      * @throws EnforcerRuleException the enforcer rule exception
 49  
      */
 50  
     public void execute( EnforcerRuleHelper helper )
 51  
         throws EnforcerRuleException
 52  
     {
 53  
 
 54  
         // get the project
 55  0
         MavenProject project = null;
 56  
         try
 57  
         {
 58  0
             project = (MavenProject) helper.evaluate( "${project}" );
 59  
         }
 60  0
         catch ( ExpressionEvaluationException eee )
 61  
         {
 62  0
             throw new EnforcerRuleException( "Unable to retrieve the MavenProject: ", eee );
 63  0
         }
 64  
 
 65  
         // get the correct list of dependencies
 66  0
         Set<Artifact> dependencies = getDependenciesToCheck( project );
 67  
 
 68  
         // look for banned dependencies
 69  0
         Set<Artifact> foundExcludes = checkDependencies( dependencies, helper.getLog() );
 70  
 
 71  
         // if any are found, fail the check but list all of them
 72  0
         if ( foundExcludes != null && !foundExcludes.isEmpty() )
 73  
         {
 74  0
             StringBuilder buf = new StringBuilder();
 75  0
             if ( message != null )
 76  
             {
 77  0
                 buf.append( message + "\n" );
 78  
             }
 79  0
             for ( Artifact artifact : foundExcludes )
 80  
             {
 81  0
                 buf.append( getErrorMessage( artifact ) );
 82  
             }
 83  0
             message = buf.toString()+ "Use 'mvn dependency:tree' to locate the source of the banned dependencies.";
 84  
 
 85  0
             throw new EnforcerRuleException( message );
 86  
         }
 87  
 
 88  0
     }
 89  
 
 90  
     protected CharSequence getErrorMessage( Artifact artifact )
 91  
     {
 92  0
         return "Found Banned Dependency: " + artifact.getId() + "\n";
 93  
     }
 94  
 
 95  
     @SuppressWarnings( "unchecked" )
 96  
     protected Set<Artifact> getDependenciesToCheck( MavenProject project )
 97  
     {
 98  0
         Set<Artifact> dependencies = null;
 99  0
         if ( searchTransitive )
 100  
         {
 101  0
             dependencies = project.getArtifacts();
 102  
         }
 103  
         else
 104  
         {
 105  0
             dependencies = project.getDependencyArtifacts();
 106  
         }
 107  0
         return dependencies;
 108  
     }
 109  
 
 110  
     /**
 111  
      * Checks the set of dependencies against the list of excludes.
 112  
      *
 113  
      * @param dependencies the dependencies
 114  
      * @param log the log
 115  
      * @return the sets the
 116  
      * @throws EnforcerRuleException the enforcer rule exception
 117  
      */
 118  
     abstract protected Set<Artifact> checkDependencies( Set<Artifact> dependencies, Log log )
 119  
         throws EnforcerRuleException;
 120  
 
 121  
     /**
 122  
      * Gets the message.
 123  
      *
 124  
      * @return the message
 125  
      */
 126  
     public String getMessage()
 127  
     {
 128  0
         return this.message;
 129  
     }
 130  
 
 131  
     /**
 132  
      * Sets the message.
 133  
      *
 134  
      * @param theMessage the message to set
 135  
      */
 136  
     public void setMessage( String theMessage )
 137  
     {
 138  0
         this.message = theMessage;
 139  0
     }
 140  
 
 141  
     /**
 142  
      * Checks if is search transitive.
 143  
      *
 144  
      * @return the searchTransitive
 145  
      */
 146  
     public boolean isSearchTransitive()
 147  
     {
 148  0
         return this.searchTransitive;
 149  
     }
 150  
 
 151  
     /**
 152  
      * Sets the search transitive.
 153  
      *
 154  
      * @param theSearchTransitive the searchTransitive to set
 155  
      */
 156  
     public void setSearchTransitive( boolean theSearchTransitive )
 157  
     {
 158  0
         this.searchTransitive = theSearchTransitive;
 159  0
     }
 160  
 
 161  
 }