All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.ferstl.maven.pomenforcers.PedanticDependencyScopeEnforcer Maven / Gradle / Ivy

Go to download

The Pedantic POM Enforcers consist of serveral Maven enforcer rules that help you keep your project setup consistent and organized.

There is a newer version: 2.2.0
Show newest version
package com.github.ferstl.maven.pomenforcers;

import java.util.Collection;
import java.util.Set;

import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
import org.apache.maven.model.Dependency;
import org.apache.maven.plugin.logging.Log;
import org.w3c.dom.Document;

import com.github.ferstl.maven.pomenforcers.artifact.ArtifactInfo;
import com.github.ferstl.maven.pomenforcers.artifact.ArtifactInfoTransformer;
import com.github.ferstl.maven.pomenforcers.util.CommaSeparatorUtils;
import com.github.ferstl.maven.pomenforcers.util.EnforcerRuleUtils;
import com.google.common.base.Function;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
import com.google.common.collect.Sets;

import static com.github.ferstl.maven.pomenforcers.DependencyScope.COMPILE;
import static com.github.ferstl.maven.pomenforcers.DependencyScope.IMPORT;
import static com.github.ferstl.maven.pomenforcers.DependencyScope.PROVIDED;
import static com.github.ferstl.maven.pomenforcers.DependencyScope.RUNTIME;
import static com.github.ferstl.maven.pomenforcers.DependencyScope.SYSTEM;
import static com.github.ferstl.maven.pomenforcers.DependencyScope.TEST;

/**
 * Enforces that the configured dependencies have to be defined within a specific scope.
 * 
 * ### Example
 *     <rules>
 *       <dependencyScope implementation="com.github.ferstl.maven.pomenforcers.PedanticDependencyScopeEnforcer">
 *         <!-- These dependencies can only be defined in test scope -->
 *         <testDependencies>junit:junit,org.hamcrest:hamcrest-library,org.mockito:mockito-core</testDependencies>
 *
 *         <!-- These dependencies can only be defined in provided scope -->
 *         <providedDependencies>javax.servlet:servlet-api</providedDependencies>
 *       </dependencyScope>
 *     </rules>
 * 
* * @id {@link PedanticEnforcerRule#DEPENDENCY_SCOPE} */ public class PedanticDependencyScopeEnforcer extends AbstractPedanticEnforcer { private final Multimap scopedDependencies; private final DependencyToArtifactInfoTransformer dependencyToArtifactInfoTransformer; public PedanticDependencyScopeEnforcer() { this.scopedDependencies = HashMultimap.create(); this.dependencyToArtifactInfoTransformer = new DependencyToArtifactInfoTransformer(); } /** * Comma-separated list of compile scope dependencies in the format groupId:artifactId. * @param compileDependencies Comma-separated list of compile scope dependencies. * @configParam */ public void setCompileDependencies(String compileDependencies) { addToArtifactinfoMap(createDependencyInfo(compileDependencies), COMPILE); } /** * Comma-separated list of provided scope dependencies in the format groupId:artifactId. * @param providedDependencies Comma-separated list of provided scope dependencies. * @configParam */ public void setProvidedDependencies(String providedDependencies) { addToArtifactinfoMap(createDependencyInfo(providedDependencies), PROVIDED); } /** * Comma-separated list of runtime scope dependencies in the format groupId:artifactId. * @param runtimeDependencies Comma-separated list of runtime scope dependencies. * @configParam */ public void setRuntimeDependencies(String runtimeDependencies) { addToArtifactinfoMap(createDependencyInfo(runtimeDependencies), RUNTIME); } /** * Comma-separated list of system scope dependencies in the format groupId:artifactId. * @param systemDependencies Comma-separated list of system scope dependencies. * @configParam */ public void setSystemDependencies(String systemDependencies) { addToArtifactinfoMap(createDependencyInfo(systemDependencies), SYSTEM); } /** * Comma-separated list of test scope dependencies in the format groupId:artifactId. * @param testDependencies Comma-separated list of test scope dependencies. * @configParam */ public void setTestDependencies(String testDependencies) { addToArtifactinfoMap(createDependencyInfo(testDependencies), TEST); } /** * Comma-separated list of import scope dependencies in the format groupId:artifactId. * @param importDependencies Comma-separated list of import scope dependencies. * @configParam */ public void setImportDependencies(String importDependencies) { addToArtifactinfoMap(createDependencyInfo(importDependencies), IMPORT); } @Override protected void doEnforce(EnforcerRuleHelper helper, Document pom) throws EnforcerRuleException { Log log = helper.getLog(); log.info("Enforcing dependency scopes."); Collection dependencies = EnforcerRuleUtils.getMavenProject(helper).getDependencies(); for (Dependency dependency : dependencies) { ArtifactInfo artifactInfo = this.dependencyToArtifactInfoTransformer.apply(dependency); Collection allowedScopes = this.scopedDependencies.get(artifactInfo); DependencyScope dependencyScope = getScope(dependency); if (allowedScopes.size() > 0 && !allowedScopes.contains(dependencyScope)) { throw new EnforcerRuleException("One does not simply declare '" + dependencyScope.getScopeName() + "' scoped dependencies! Dependency " + dependency + " has to be declared in these scopes: " + allowedScopes); } } } @Override protected void accept(PedanticEnforcerVisitor visitor) { visitor.visit(this); } private Set createDependencyInfo(String dependencies) { Set dependencyInfoSet = Sets.newHashSet(); CommaSeparatorUtils.splitAndAddToCollection(dependencies, dependencyInfoSet, new ArtifactInfoTransformer()); return dependencyInfoSet; } private void addToArtifactinfoMap(Iterable artifactInfos, DependencyScope scope) { for (ArtifactInfo artifactInfo : artifactInfos) { this.scopedDependencies.put(artifactInfo, scope); } } private DependencyScope getScope(Dependency dependency) { if (dependency.getScope() == null) { return COMPILE; } return DependencyScope.getByScopeName(dependency.getScope()); } private class DependencyToArtifactInfoTransformer implements Function { @Override public ArtifactInfo apply(Dependency input) { return new ArtifactInfo(input.getGroupId(), input.getArtifactId()); } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy