Commit e02be657 authored by Brian Long's avatar Brian Long
Browse files

added purge goal

parent 7f8baa6f
Loading
Loading
Loading
Loading
+0 −1
Original line number Diff line number Diff line
@@ -146,7 +146,6 @@
				<configuration>
					<projectsDirectory>${basedir}/src/it</projectsDirectory>
					<cloneProjectsTo>${project.build.directory}/it</cloneProjectsTo>
					<localRepositoryPath>${project.build.directory}/it-repo</localRepositoryPath>
					<mavenHome>${env.MAVEN_HOME}</mavenHome>
					<debug>true</debug>
					<ignoreFailures>true</ignoreFailures>
+56 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0"
		xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.inteligr8</groupId>
	<artifactId>ban-maven-plugin-log4j-old</artifactId>
	<version>@pom.version@</version>
	<packaging>jar</packaging>

	<name>Log4j Ban Plugin Tests</name>
	
	<dependencies>
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.17</version>
		</dependency>
	</dependencies>
	
	<build>
		<plugins>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.11.0</version>
			</plugin>
			<plugin>
				<groupId>${project.groupId}</groupId>
				<artifactId>ban-maven-plugin</artifactId>
				<version>@pom.version@</version>
				<extensions>true</extensions>
				<configuration>
					<includes>
						<artifact>
							<groupId>log4j</groupId>
							<artifactId>log4j</artifactId>
						</artifact>
					</includes>
					<excludes>
						<artifact>log4j:log.+:[1.2.17,)</artifact>
					</excludes>
				</configuration>
				<executions>
					<execution>
						<id>purge</id>
						<phase>prepare-package</phase>
						<goals><goal>purge-repo</goal></goals>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>

</project>
+8 −62
Original line number Diff line number Diff line
@@ -21,20 +21,16 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;
import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
import org.apache.maven.artifact.versioning.VersionRange;
import org.apache.maven.model.Plugin;
import org.codehaus.plexus.util.xml.Xpp3Dom;
import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.graph.DependencyFilter;
import org.eclipse.aether.graph.DependencyNode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.inteligr8.maven.model.ArtifactFilter;

public class BanConfigurationParser implements DependencyFilter {
public class BanConfigurationParser {
    
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    private final Pattern artifactPattern = Pattern.compile("^([^:]+):([^:]+)(:([^:]+))?$");
@@ -42,7 +38,6 @@ public class BanConfigurationParser implements DependencyFilter {
    
    private final List<ArtifactFilter> includeArtifacts;
    private final List<ArtifactFilter> excludeArtifacts;
    private boolean failFast = false;
    
    public BanConfigurationParser(Plugin plugin) {
        Xpp3Dom rootDom = (Xpp3Dom) plugin.getConfiguration();
@@ -69,8 +64,12 @@ public class BanConfigurationParser implements DependencyFilter {
        }
    }
    
    public void setFailFast(boolean failFast) {
        this.failFast = failFast;
    public List<ArtifactFilter> getIncludeArtifacts() {
        return this.includeArtifacts;
    }
    
    public List<ArtifactFilter> getExcludeArtifacts() {
        return this.excludeArtifacts;
    }
    
    private List<ArtifactFilter> parseArtifacts(Xpp3Dom artifactsDom) {
@@ -92,7 +91,7 @@ public class BanConfigurationParser implements DependencyFilter {
                    filter.setGroupIdRegex(StringUtils.trimToNull(matcher.group(1)));
                }
                
                if (this.notRegexPattern.matcher(matcher.group(1)).matches()) {
                if (this.notRegexPattern.matcher(matcher.group(2)).matches()) {
                    filter.setArtifactId(StringUtils.trimToNull(matcher.group(2)));
                } else {
                    filter.setArtifactIdRegex(StringUtils.trimToNull(matcher.group(2)));
@@ -129,57 +128,4 @@ public class BanConfigurationParser implements DependencyFilter {
        return childDom == null ? null : StringUtils.trimToNull(childDom.getValue());
    }
    
    @Override
    public boolean accept(DependencyNode node, List<DependencyNode> parents) {
        this.logger.debug("Evaluating dependency '{}'", node);
        boolean ban = false;
        
        for (ArtifactFilter afilter : this.includeArtifacts) {
            Artifact depArtifact = node.getArtifact();
            if (this.matches(afilter.getGroupId(), afilter.getGroupIdRegex(), depArtifact.getGroupId()) &&
                    this.matches(afilter.getArtifactId(), afilter.getArtifactIdRegex(), depArtifact.getArtifactId()) &&
                    this.withinRange(afilter.getVersionRange(), depArtifact.getVersion())) {
                this.logger.debug("The dependency '{}' matches the ban inclusion filter", depArtifact);
                ban = true;
                break;
            }
        }
        
        if (!ban)
            return false;
        
        for (ArtifactFilter afilter : this.excludeArtifacts) {
            Artifact depArtifact = node.getArtifact();
            if (this.matches(afilter.getGroupId(), afilter.getGroupIdRegex(), depArtifact.getGroupId()) &&
                    this.matches(afilter.getArtifactId(), afilter.getArtifactIdRegex(), depArtifact.getArtifactId()) &&
                    this.withinRange(afilter.getVersionRange(), depArtifact.getVersion())) {
                this.logger.debug("The dependency '{}' matches the ban exlusion filter", depArtifact);
                return false;
            }
        }
        
        if (this.failFast) {
            // plugin resolution downloads banned dependencies unless we fail now; not later
            throw new RuntimeException("Banned dependency detected: " + node + " => " + parents);
        } else {
            return true;
        }
    }
    
    private boolean matches(String exactFilter, String regexFilter, String value) {
        if (exactFilter == null && regexFilter == null) {
            return true;
        } else if (exactFilter != null) {
            return exactFilter.equals(value);
        } else {
            Pattern filterPattern = Pattern.compile(regexFilter);
            Matcher matcher = filterPattern.matcher(value);
            return matcher.matches();
        }
    }
    
    private boolean withinRange(VersionRange versionRange, String version) {
        return versionRange == null || versionRange.containsVersion(new DefaultArtifactVersion(version));
    }
    
}
+102 −0
Original line number Diff line number Diff line
/*
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or (at your
 * option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <https://www.gnu.org/licenses/>.
 */
package com.inteligr8.maven.ban;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
import org.apache.maven.artifact.versioning.VersionRange;
import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.graph.DependencyFilter;
import org.eclipse.aether.graph.DependencyNode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.inteligr8.maven.model.ArtifactFilter;

public class BanDependencyFilter implements DependencyFilter {
    
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    
    private final List<ArtifactFilter> includeArtifacts;
    private final List<ArtifactFilter> excludeArtifacts;
    private boolean failFast = false;
    
    public BanDependencyFilter(List<ArtifactFilter> includeArtifacts, List<ArtifactFilter> excludeArtifacts) {
        this.includeArtifacts = includeArtifacts;
        this.excludeArtifacts = excludeArtifacts;
    }
    
    public void setFailFast(boolean failFast) {
        this.failFast = failFast;
    }
    
    @Override
    public boolean accept(DependencyNode node, List<DependencyNode> parents) {
        this.logger.debug("Evaluating dependency '{}'", node);
        Artifact depArtifact = node.getArtifact();
        boolean ban = false;
        
        for (ArtifactFilter afilter : this.includeArtifacts) {
            if (this.matches(afilter.getGroupId(), afilter.getGroupIdRegex(), depArtifact.getGroupId()) &&
                    this.matches(afilter.getArtifactId(), afilter.getArtifactIdRegex(), depArtifact.getArtifactId()) &&
                    this.withinRange(afilter.getVersionRange(), depArtifact.getVersion())) {
                this.logger.debug("The dependency '{}' matches the ban inclusion filter", depArtifact);
                ban = true;
                break;
            }
        }
        
        if (!ban)
            return false;
        
        for (ArtifactFilter afilter : this.excludeArtifacts) {
            if (this.matches(afilter.getGroupId(), afilter.getGroupIdRegex(), depArtifact.getGroupId()) &&
                    this.matches(afilter.getArtifactId(), afilter.getArtifactIdRegex(), depArtifact.getArtifactId()) &&
                    this.withinRange(afilter.getVersionRange(), depArtifact.getVersion())) {
                this.logger.debug("The dependency '{}' matches the ban exlusion filter", depArtifact);
                return false;
            }
        }
        
        if (this.failFast) {
            // plugin resolution downloads banned dependencies unless we fail now; not later
            throw new RuntimeException("Banned dependency detected: " + node + " => " + parents);
        } else {
            return true;
        }
    }
    
    private boolean matches(String exactFilter, String regexFilter, String value) {
        if (exactFilter == null && regexFilter == null) {
            return true;
        } else if (exactFilter != null) {
            return exactFilter.equals(value);
        } else {
            Pattern filterPattern = Pattern.compile(regexFilter);
            Matcher matcher = filterPattern.matcher(value);
            return matcher.matches();
        }
    }
    
    private boolean withinRange(VersionRange versionRange, String version) {
        boolean within = versionRange == null || versionRange.containsVersion(new DefaultArtifactVersion(version));
        this.logger.debug("Tested version range: {} <=> {}: {}", versionRange, version, within);
        return within;
    }
    
}
+10 −8
Original line number Diff line number Diff line
@@ -44,9 +44,9 @@ import org.slf4j.LoggerFactory;
@Singleton
public class BanExtension extends AbstractMavenLifecycleParticipant {

    private static final String THIS_PLUGIN_GROUP_ID = "com.inteligr8";    
    private static final String THIS_PLUGIN_ARTIFACT_ID = "ban-maven-plugin";
    private static final String THIS_PLUGIN_KEY = THIS_PLUGIN_GROUP_ID + ":" + THIS_PLUGIN_ARTIFACT_ID;
    public static final String THIS_PLUGIN_GROUP_ID = "com.inteligr8";    
    public static final String THIS_PLUGIN_ARTIFACT_ID = "ban-maven-plugin";
    public static final String THIS_PLUGIN_KEY = THIS_PLUGIN_GROUP_ID + ":" + THIS_PLUGIN_ARTIFACT_ID;
    
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    
@@ -62,15 +62,17 @@ public class BanExtension extends AbstractMavenLifecycleParticipant {
        BanConfigurationParser config = this.getConfiguration(project);
        if (config == null)
            return;
        config.setFailFast(true);
        
        BanDependencyFilter depFilter = new BanDependencyFilter(config.getIncludeArtifacts(), config.getExcludeArtifacts());
        depFilter.setFailFast(true);
        
        try {
            for (Plugin plugin : project.getBuildPlugins()) {
                this.logger.debug("Evaluating plugin dependencies: {}", plugin);
                
                Artifact artifact = new DefaultArtifact(plugin.getId());
                DependencyNode depNodeRoot = this.pluginDepResolver.resolve(plugin, artifact, config, project.getRemotePluginRepositories(), session.getRepositorySession());
                List<Dependency> bannedDependencies = this.crawlDependencyTree(depNodeRoot, config);
                DependencyNode depNodeRoot = this.pluginDepResolver.resolve(plugin, artifact, depFilter, project.getRemotePluginRepositories(), session.getRepositorySession());
                List<Dependency> bannedDependencies = this.crawlDependencyTree(depNodeRoot, depFilter);
                if (!bannedDependencies.isEmpty())
                    throw new MavenExecutionException("Banned dependencies were detected in plugin '" + plugin + "': " + bannedDependencies, project.getFile());
            }
@@ -78,10 +80,10 @@ public class BanExtension extends AbstractMavenLifecycleParticipant {
            throw new MavenExecutionException(pre.getMessage(), pre);
        }

        config.setFailFast(false);
        depFilter.setFailFast(false);

        DefaultDependencyResolutionRequest request = new DefaultDependencyResolutionRequest(project, session.getRepositorySession());
        request.setResolutionFilter(config);
        request.setResolutionFilter(depFilter);
        
        try {
            DependencyResolutionResult result = this.projDepResolver.resolve(request);
Loading