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.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  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      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          MavenProject project = null;
56          try
57          {
58              project = (MavenProject) helper.evaluate( "${project}" );
59          }
60          catch ( ExpressionEvaluationException eee )
61          {
62              throw new EnforcerRuleException( "Unable to retrieve the MavenProject: ", eee );
63          }
64  
65          // get the correct list of dependencies
66          Set<Artifact> dependencies = getDependenciesToCheck( project );
67  
68          // look for banned dependencies
69          Set<Artifact> foundExcludes = checkDependencies( dependencies, helper.getLog() );
70  
71          // if any are found, fail the check but list all of them
72          if ( foundExcludes != null && !foundExcludes.isEmpty() )
73          {
74              StringBuilder buf = new StringBuilder();
75              if ( message != null )
76              {
77                  buf.append( message + "\n" );
78              }
79              for ( Artifact artifact : foundExcludes )
80              {
81                  buf.append( getErrorMessage( artifact ) );
82              }
83              message = buf.toString()+ "Use 'mvn dependency:tree' to locate the source of the banned dependencies.";
84  
85              throw new EnforcerRuleException( message );
86          }
87  
88      }
89  
90      protected CharSequence getErrorMessage( Artifact artifact )
91      {
92          return "Found Banned Dependency: " + artifact.getId() + "\n";
93      }
94  
95      @SuppressWarnings( "unchecked" )
96      protected Set<Artifact> getDependenciesToCheck( MavenProject project )
97      {
98          Set<Artifact> dependencies = null;
99          if ( searchTransitive )
100         {
101             dependencies = project.getArtifacts();
102         }
103         else
104         {
105             dependencies = project.getDependencyArtifacts();
106         }
107         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         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         this.message = theMessage;
139     }
140 
141     /**
142      * Checks if is search transitive.
143      *
144      * @return the searchTransitive
145      */
146     public boolean isSearchTransitive()
147     {
148         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         this.searchTransitive = theSearchTransitive;
159     }
160 
161 }