001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.maven.enforcer.rules; 020 021import javax.inject.Inject; 022import javax.inject.Named; 023 024import java.util.List; 025import java.util.Objects; 026 027import org.apache.maven.artifact.Artifact; 028import org.apache.maven.enforcer.rule.api.EnforcerRuleException; 029import org.apache.maven.enforcer.rules.utils.ArtifactUtils; 030import org.apache.maven.execution.MavenSession; 031 032/** 033 * This rule checks that lists of plugins are not included. 034 * 035 * @author <a href="mailto:velo.br@gmail.com">Marvin Froeder</a> 036 */ 037@Named("bannedPlugins") 038public final class BannedPlugins extends AbstractStandardEnforcerRule { 039 040 /** 041 * Specify the banned plugins. This can be a list of plugins in the format 042 * <code>groupId[:artifactId][:version]</code>. Any of the sections can be a wildcard 043 * by using '*' (ie group:*:1.0) <br> 044 * The rule will fail if any plugin matches any exclude, unless it also matches 045 * an include rule. 046 */ 047 private List<String> excludes = null; 048 049 /** 050 * Specify the allowed plugins. This can be a list of plugins in the format 051 * <code>groupId[:artifactId][:version]</code>. Any of the sections can be a wildcard 052 * by using '*' (ie group:*:1.0) <br> 053 * Includes override the exclude rules. It is meant to allow wide exclusion rules 054 * with wildcards and still allow a 055 * smaller set of includes. <br> 056 * For example, to ban all xerces except xerces-api -> exclude "xerces", include "xerces:xerces-api" 057 */ 058 private List<String> includes = null; 059 060 private final MavenSession session; 061 062 @Inject 063 public BannedPlugins(MavenSession session) { 064 this.session = Objects.requireNonNull(session); 065 } 066 067 @Override 068 public void execute() throws EnforcerRuleException { 069 070 String result = session.getCurrentProject().getPluginArtifacts().stream() 071 .filter(a -> !validate(a)) 072 .collect( 073 StringBuilder::new, 074 (messageBuilder, node) -> 075 messageBuilder.append(node.getId()).append(" <--- banned plugin"), 076 (m1, m2) -> m1.append(m2.toString())) 077 .toString(); 078 if (!result.isEmpty()) { 079 throw new EnforcerRuleException(result); 080 } 081 } 082 083 private boolean validate(Artifact artifact) { 084 return !ArtifactUtils.matchDependencyArtifact(artifact, excludes) 085 || ArtifactUtils.matchDependencyArtifact(artifact, includes); 086 } 087 088 @Override 089 public String toString() { 090 return String.format("BannedPlugins[excludes=%s, includes=%s]", excludes, includes); 091 } 092}