1 | |
package org.apache.maven.plugins.enforcer; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
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 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | 0 | public abstract class AbstractBanDependencies |
38 | |
extends AbstractNonCacheableEnforcerRule |
39 | |
{ |
40 | |
|
41 | |
|
42 | 0 | private boolean searchTransitive = true; |
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
public void execute( EnforcerRuleHelper helper ) |
51 | |
throws EnforcerRuleException |
52 | |
{ |
53 | |
|
54 | |
|
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 | |
|
66 | 0 | Set<Artifact> dependencies = getDependenciesToCheck( project ); |
67 | |
|
68 | |
|
69 | 0 | Set<Artifact> foundExcludes = checkDependencies( dependencies, helper.getLog() ); |
70 | |
|
71 | |
|
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 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
abstract protected Set<Artifact> checkDependencies( Set<Artifact> dependencies, Log log ) |
119 | |
throws EnforcerRuleException; |
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
public String getMessage() |
127 | |
{ |
128 | 0 | return this.message; |
129 | |
} |
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
public void setMessage( String theMessage ) |
137 | |
{ |
138 | 0 | this.message = theMessage; |
139 | 0 | } |
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
public boolean isSearchTransitive() |
147 | |
{ |
148 | 0 | return this.searchTransitive; |
149 | |
} |
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
public void setSearchTransitive( boolean theSearchTransitive ) |
157 | |
{ |
158 | 0 | this.searchTransitive = theSearchTransitive; |
159 | 0 | } |
160 | |
|
161 | |
} |