001package org.apache.maven.plugins.enforcer; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import java.util.HashSet; 023import java.util.Set; 024 025import org.apache.maven.artifact.Artifact; 026import org.apache.maven.enforcer.rule.api.EnforcerRuleException; 027import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper; 028import org.apache.maven.plugin.logging.Log; 029import org.apache.maven.project.MavenProject; 030import org.apache.maven.shared.dependency.graph.DependencyGraphBuilder; 031import org.apache.maven.shared.dependency.graph.DependencyGraphBuilderException; 032import org.apache.maven.shared.dependency.graph.DependencyNode; 033import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException; 034import org.codehaus.plexus.component.repository.exception.ComponentLookupException; 035 036/** 037 * Abstract Rule for banning dependencies. 038 * 039 * @author <a href="mailto:brianf@apache.org">Brian Fox</a> 040 * @version $Id: AbstractBanDependencies.java 1495697 2013-06-22 10:05:58Z rfscholte $ 041 */ 042public abstract class AbstractBanDependencies 043 extends AbstractNonCacheableEnforcerRule 044{ 045 046 /** Specify if transitive dependencies should be searched (default) or only look at direct dependencies. */ 047 private boolean searchTransitive = true; 048 049 private transient DependencyGraphBuilder graphBuilder; 050 051 /** 052 * Execute the rule. 053 * 054 * @param helper the helper 055 * @throws EnforcerRuleException the enforcer rule exception 056 */ 057 public void execute( EnforcerRuleHelper helper ) 058 throws EnforcerRuleException 059 { 060 061 // get the project 062 MavenProject project = null; 063 try 064 { 065 project = (MavenProject) helper.evaluate( "${project}" ); 066 } 067 catch ( ExpressionEvaluationException eee ) 068 { 069 throw new EnforcerRuleException( "Unable to retrieve the MavenProject: ", eee ); 070 } 071 072 try 073 { 074 graphBuilder = (DependencyGraphBuilder) helper.getComponent( DependencyGraphBuilder.class ); 075 } 076 catch ( ComponentLookupException e ) 077 { 078 // real cause is probably that one of the Maven3 graph builder could not be initiated and fails with a ClassNotFoundException 079 try 080 { 081 graphBuilder = (DependencyGraphBuilder) helper.getComponent( DependencyGraphBuilder.class.getName(), "maven2" ); 082 } 083 catch ( ComponentLookupException e1 ) 084 { 085 throw new EnforcerRuleException( "Unable to lookup DependencyGraphBuilder: ", e ); 086 } 087 } 088 089 // get the correct list of dependencies 090 Set<Artifact> dependencies = getDependenciesToCheck( project ); 091 092 // look for banned dependencies 093 Set<Artifact> foundExcludes = checkDependencies( dependencies, helper.getLog() ); 094 095 // if any are found, fail the check but list all of them 096 if ( foundExcludes != null && !foundExcludes.isEmpty() ) 097 { 098 String message = getMessage(); 099 100 StringBuilder buf = new StringBuilder(); 101 if ( message != null ) 102 { 103 buf.append( message + "\n" ); 104 } 105 for ( Artifact artifact : foundExcludes ) 106 { 107 buf.append( getErrorMessage( artifact ) ); 108 } 109 message = buf.toString() + "Use 'mvn dependency:tree' to locate the source of the banned dependencies."; 110 111 throw new EnforcerRuleException( message ); 112 } 113 114 } 115 116 protected CharSequence getErrorMessage( Artifact artifact ) 117 { 118 return "Found Banned Dependency: " + artifact.getId() + "\n"; 119 } 120 121 protected Set<Artifact> getDependenciesToCheck( MavenProject project ) 122 { 123 Set<Artifact> dependencies = null; 124 try 125 { 126 DependencyNode node = graphBuilder.buildDependencyGraph( project, null ); 127 if( searchTransitive ) 128 { 129 dependencies = getAllDescendants( node ); 130 } 131 else if ( node.getChildren() != null ) 132 { 133 dependencies = new HashSet<Artifact>(); 134 for( DependencyNode depNode : node.getChildren() ) 135 { 136 dependencies.add( depNode.getArtifact() ); 137 } 138 } 139 } 140 catch ( DependencyGraphBuilderException e ) 141 { 142 // otherwise we need to change the signature of this protected method 143 throw new RuntimeException( e ); 144 } 145 return dependencies; 146 } 147 148 private Set<Artifact> getAllDescendants( DependencyNode node ) 149 { 150 Set<Artifact> children = null; 151 if( node.getChildren() != null ) 152 { 153 children = new HashSet<Artifact>(); 154 for( DependencyNode depNode : node.getChildren() ) 155 { 156 children.add( depNode.getArtifact() ); 157 Set<Artifact> subNodes = getAllDescendants( depNode ); 158 if( subNodes != null ) 159 { 160 children.addAll( subNodes ); 161 } 162 } 163 } 164 return children; 165 } 166 167 /** 168 * Checks the set of dependencies against the list of excludes. 169 * 170 * @param dependencies the dependencies 171 * @param log the log 172 * @return the sets the 173 * @throws EnforcerRuleException the enforcer rule exception 174 */ 175 protected abstract Set<Artifact> checkDependencies( Set<Artifact> dependencies, Log log ) 176 throws EnforcerRuleException; 177 178 /** 179 * Checks if is search transitive. 180 * 181 * @return the searchTransitive 182 */ 183 public boolean isSearchTransitive() 184 { 185 return this.searchTransitive; 186 } 187 188 /** 189 * Sets the search transitive. 190 * 191 * @param theSearchTransitive the searchTransitive to set 192 */ 193 public void setSearchTransitive( boolean theSearchTransitive ) 194 { 195 this.searchTransitive = theSearchTransitive; 196 } 197 198}