/
BanTransitiveDependencies.java
202 lines (171 loc) · 7.22 KB
/
BanTransitiveDependencies.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package org.apache.maven.plugins.enforcer;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import java.util.Collections;
import java.util.List;
import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
import org.apache.maven.enforcer.rule.api.EnforcerRule;
import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
import org.apache.maven.plugins.enforcer.utils.ArtifactMatcher;
import org.apache.maven.project.MavenProject;
import org.apache.maven.shared.dependency.graph.DependencyGraphBuilder;
import org.apache.maven.shared.dependency.graph.DependencyNode;
import org.apache.maven.shared.dependency.graph.internal.DefaultDependencyGraphBuilder;
import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
import org.codehaus.plexus.logging.console.ConsoleLogger;
/**
* This rule bans all transitive dependencies. There is a configuration option to exclude certain artifacts from being
* checked.
*
* @author Jakub Senko
*/
public class BanTransitiveDependencies
extends AbstractNonCacheableEnforcerRule
implements EnforcerRule
{
private EnforcerRuleHelper helper;
/**
* Specify the dependencies that will be ignored. This can be a list of artifacts in the format
* <code>groupId[:artifactId][:version][:type][:scope]</code>. Wildcard '*' can be used to in place of specific
* section (ie group:*:1.0 will match both 'group:artifact:1.0' and 'group:anotherArtifact:1.0') <br>
* You can override this patterns by using includes. Version is a string representing standard maven version range.
* Empty patterns will be ignored.
*/
private List<String> excludes;
/**
* Specify the dependencies that will be checked. These are exceptions to excludes intended for more convenient and
* finer settings. This can be a list of artifacts in the format
* <code>groupId[:artifactId][:version][:type][:scope]</code>. Wildcard '*' can be used to in place of specific
* section (ie group:*:1.0 will match both 'group:artifact:1.0' and 'group:anotherArtifact:1.0') <br>
* Version is a string representing standard maven version range. Empty patterns will be ignored.
*/
private List<String> includes;
/**
* Searches dependency tree recursively for transitive dependencies that are not excluded, while generating nice
* info message along the way.
*
* @throws InvalidVersionSpecificationException
*/
private static boolean searchTree( DependencyNode node, int level, ArtifactMatcher excludes, StringBuilder message )
throws InvalidVersionSpecificationException
{
List<DependencyNode> children = node.getChildren();
/*
* if the node is deeper than direct dependency and is empty, it is transitive.
*/
boolean hasTransitiveDependencies = level > 1;
boolean excluded = false;
/*
* holds recursive message from children, will be appended to current message if this node has any transitive
* descendants if message is null, don't generate recursive message.
*/
StringBuilder messageFromChildren = message == null ? null : new StringBuilder();
if ( excludes.match( node.getArtifact() ) )
{
// is excluded, we don't care about descendants
excluded = true;
hasTransitiveDependencies = false;
}
else
{
for ( DependencyNode childNode : children )
{
/*
* if any of the children has transitive d. so does the parent
*/
hasTransitiveDependencies =
( searchTree( childNode, level + 1, excludes, messageFromChildren ) || hasTransitiveDependencies );
}
}
if ( ( excluded || hasTransitiveDependencies ) && message != null ) // then generate message
{
for ( int i = 0; i < level; i++ )
{
message.append( " " );
}
message.append( node.getArtifact() );
if ( excluded )
{
message.append( " [excluded]\n" );
}
if ( hasTransitiveDependencies )
{
if ( level == 1 )
{
message.append( " has transitive dependencies:" );
}
message.append( "\n" ).append( messageFromChildren );
}
}
return hasTransitiveDependencies;
}
public void execute( EnforcerRuleHelper helper )
throws EnforcerRuleException
{
this.helper = helper;
if ( excludes == null )
{
excludes = Collections.emptyList();
}
if ( includes == null )
{
includes = Collections.emptyList();
}
final ArtifactMatcher exclusions = new ArtifactMatcher( excludes, includes );
DependencyNode rootNode = null;
try
{
MavenProject project = (MavenProject) helper.evaluate( "${project}" );
rootNode = createDependencyGraphBuilder().buildDependencyGraph( project, null );
}
catch ( Exception e )
{
throw new EnforcerRuleException( "Error: Could not construct dependency tree.", e );
}
String message = getMessage();
StringBuilder generatedMessage = null;
if ( message == null )
{
generatedMessage = new StringBuilder();
}
try
{
if ( searchTree( rootNode, 0, exclusions, generatedMessage ) )
{
throw new EnforcerRuleException( message == null ? generatedMessage.toString() : message );
}
}
catch ( InvalidVersionSpecificationException e )
{
throw new EnforcerRuleException( "Error: Invalid version range.", e );
}
}
private DependencyGraphBuilder createDependencyGraphBuilder()
throws ComponentLookupException
{
// CHECKSTYLE_OFF: LineLength
DefaultDependencyGraphBuilder builder =
(DefaultDependencyGraphBuilder) helper.getContainer().lookup( DependencyGraphBuilder.class.getCanonicalName(),
"default" );
// CHECKSTYLE_ON: LineLength
builder.enableLogging( new ConsoleLogger( ConsoleLogger.LEVEL_DISABLED, "DefaultDependencyGraphBuilder" ) );
return builder;
}
}