View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.enforcer.rules;
20  
21  import javax.inject.Inject;
22  import javax.inject.Named;
23  
24  import java.util.List;
25  import java.util.Objects;
26  import java.util.function.Predicate;
27  
28  import org.apache.maven.artifact.Artifact;
29  import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
30  import org.apache.maven.enforcer.rules.utils.ArtifactUtils;
31  import org.apache.maven.execution.MavenSession;
32  
33  /**
34   * This rule checks that lists of plugins are not included.
35   *
36   * @author <a href="mailto:velo.br@gmail.com">Marvin Froeder</a>
37   */
38  @Named("bannedPlugins")
39  public final class BannedPlugins extends AbstractStandardEnforcerRule {
40  
41      /**
42       * Specify the banned plugins. This can be a list of plugins in the format
43       * <code>groupId[:artifactId][:version]</code>. Any of the sections can be a wildcard
44       * by using '*' (ie group:*:1.0) <br>
45       * The rule will fail if any plugin matches any exclude, unless it also matches
46       * an include rule.
47       */
48      private List<String> excludes = null;
49  
50      /**
51       * Specify the allowed plugins. This can be a list of plugins in the format
52       * <code>groupId[:artifactId][:version]</code>. Any of the sections can be a wildcard
53       * by using '*' (ie group:*:1.0) <br>
54       * Includes override the exclude rules. It is meant to allow wide exclusion rules
55       * with wildcards and still allow a
56       * smaller set of includes. <br>
57       * For example, to ban all xerces except xerces-api -&gt; exclude "xerces", include "xerces:xerces-api"
58       */
59      private List<String> includes = null;
60  
61      private final MavenSession session;
62  
63      @Inject
64      public BannedPlugins(MavenSession session) {
65          this.session = Objects.requireNonNull(session);
66      }
67  
68      @Override
69      public void execute() throws EnforcerRuleException {
70          Predicate<Artifact> shouldExclude = ArtifactUtils.prepareDependencyArtifactMatcher(excludes);
71          Predicate<Artifact> shouldInclude = ArtifactUtils.prepareDependencyArtifactMatcher(includes);
72          String result = session.getCurrentProject().getPluginArtifacts().stream()
73                  .filter(a -> !(!shouldExclude.test(a) || shouldInclude.test(a)))
74                  .collect(
75                          StringBuilder::new,
76                          (messageBuilder, node) ->
77                                  messageBuilder.append(node.getId()).append(" <--- banned plugin"),
78                          (m1, m2) -> m1.append(m2.toString()))
79                  .toString();
80          if (!result.isEmpty()) {
81              throw new EnforcerRuleException(result);
82          }
83      }
84  
85      @Override
86      public String toString() {
87          return String.format("BannedPlugins[excludes=%s, includes=%s]", excludes, includes);
88      }
89  }