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 27 import org.apache.maven.artifact.Artifact; 28 import org.apache.maven.enforcer.rule.api.EnforcerRuleException; 29 import org.apache.maven.enforcer.rules.utils.ArtifactUtils; 30 import org.apache.maven.execution.MavenSession; 31 32 /** 33 * This rule checks that lists of plugins are not included. 34 * 35 * @author <a href="mailto:velo.br@gmail.com">Marvin Froeder</a> 36 */ 37 @Named("bannedPlugins") 38 public final class BannedPlugins extends AbstractStandardEnforcerRule { 39 40 /** 41 * Specify the banned plugins. This can be a list of plugins in the format 42 * <code>groupId[:artifactId][:version]</code>. Any of the sections can be a wildcard 43 * by using '*' (ie group:*:1.0) <br> 44 * The rule will fail if any plugin matches any exclude, unless it also matches 45 * an include rule. 46 */ 47 private List<String> excludes = null; 48 49 /** 50 * Specify the allowed plugins. This can be a list of plugins in the format 51 * <code>groupId[:artifactId][:version]</code>. Any of the sections can be a wildcard 52 * by using '*' (ie group:*:1.0) <br> 53 * Includes override the exclude rules. It is meant to allow wide exclusion rules 54 * with wildcards and still allow a 55 * smaller set of includes. <br> 56 * For example, to ban all xerces except xerces-api -> exclude "xerces", include "xerces:xerces-api" 57 */ 58 private List<String> includes = null; 59 60 private final MavenSession session; 61 62 @Inject 63 public BannedPlugins(MavenSession session) { 64 this.session = Objects.requireNonNull(session); 65 } 66 67 @Override 68 public void execute() throws EnforcerRuleException { 69 70 String result = session.getCurrentProject().getPluginArtifacts().stream() 71 .filter(a -> !validate(a)) 72 .collect( 73 StringBuilder::new, 74 (messageBuilder, node) -> 75 messageBuilder.append(node.getId()).append(" <--- banned plugin"), 76 (m1, m2) -> m1.append(m2.toString())) 77 .toString(); 78 if (!result.isEmpty()) { 79 throw new EnforcerRuleException(result); 80 } 81 } 82 83 private boolean validate(Artifact artifact) { 84 return !ArtifactUtils.matchDependencyArtifact(artifact, excludes) 85 || ArtifactUtils.matchDependencyArtifact(artifact, includes); 86 } 87 88 @Override 89 public String toString() { 90 return String.format("BannedPlugins[excludes=%s, includes=%s]", excludes, includes); 91 } 92 }