001package org.apache.maven.plugins.enforcer;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.util.List;
023import java.util.Set;
024
025import org.apache.maven.artifact.Artifact;
026import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
027import org.apache.maven.plugin.logging.Log;
028import org.apache.maven.plugins.enforcer.utils.ArtifactUtils;
029
030/**
031 * This rule checks that lists of dependencies are not included.
032 * 
033 * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
034 */
035public class BannedDependencies
036    extends AbstractBanDependencies
037{
038
039    /**
040     * Specify the banned dependencies. This can be a list of artifacts in the format
041     * <code>groupId[:artifactId][:version]</code>. Any of the sections can be a wildcard 
042     * by using '*' (ie group:*:1.0) <br>
043     * The rule will fail if any dependency matches any exclude, unless it also matches 
044     * an include rule.
045     * 
046     * @see {@link #setExcludes(List)}
047     * @see {@link #getExcludes()}
048     */
049    private List<String> excludes = null;
050
051    /**
052     * Specify the allowed dependencies. This can be a list of artifacts in the format
053     * <code>groupId[:artifactId][:version]</code>. Any of the sections can be a wildcard 
054     * by using '*' (ie group:*:1.0) <br>
055     * Includes override the exclude rules. It is meant to allow wide exclusion rules 
056     * with wildcards and still allow a
057     * smaller set of includes. <br>
058     * For example, to ban all xerces except xerces-api -> exclude "xerces", include "xerces:xerces-api"
059     * 
060     * @see {@link #setIncludes(List)}
061     * @see {@link #getIncludes()}
062     */
063    private List<String> includes = null;
064
065    @Override
066    protected Set<Artifact> checkDependencies( Set<Artifact> theDependencies, Log log )
067        throws EnforcerRuleException
068    {
069
070        Set<Artifact> excluded = ArtifactUtils.checkDependencies( theDependencies, excludes );
071
072        // anything specifically included should be removed
073        // from the ban list.
074        if ( excluded != null )
075        {
076            Set<Artifact> included = ArtifactUtils.checkDependencies( theDependencies, includes );
077
078            if ( included != null )
079            {
080                excluded.removeAll( included );
081            }
082        }
083        return excluded;
084
085    }
086
087    /**
088     * Gets the excludes.
089     * 
090     * @return the excludes
091     */
092    public List<String> getExcludes()
093    {
094        return this.excludes;
095    }
096
097    /**
098     * Specify the banned dependencies. This can be a list of artifacts in the format
099     * <code>groupId[:artifactId][:version]</code>. Any of the sections can be a wildcard 
100     * by using '*' (ie group:*:1.0) <br>
101     * The rule will fail if any dependency matches any exclude, unless it also matches an 
102     * include rule.
103     * 
104     * @see #getExcludes()
105     * @param theExcludes the excludes to set
106     */
107    public void setExcludes( List<String> theExcludes )
108    {
109        this.excludes = theExcludes;
110    }
111
112    /**
113     * Gets the includes.
114     * 
115     * @return the includes
116     */
117    public List<String> getIncludes()
118    {
119        return this.includes;
120    }
121
122    /**
123     * Specify the allowed dependencies. This can be a list of artifacts in the format
124     * <code>groupId[:artifactId][:version]</code>. Any of the sections can be a wildcard 
125     * by using '*' (ie group:*:1.0) <br>
126     * Includes override the exclude rules. It is meant to allow wide exclusion rules with 
127     * wildcards and still allow a
128     * smaller set of includes. <br>
129     * For example, to ban all xerces except xerces-api → exclude "xerces",
130     * include "xerces:xerces-api"
131     * 
132     * @see #setIncludes(List)
133     * @param theIncludes the includes to set
134     */
135    public void setIncludes( List<String> theIncludes )
136    {
137        this.includes = theIncludes;
138    }
139
140}