com.github.ferstl.maven.pomenforcers.PedanticDependencyManagementLocationEnforcer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of pedantic-pom-enforcers Show documentation
Show all versions of pedantic-pom-enforcers Show documentation
The Pedantic POM Enforcers consist of serveral Maven enforcer rules that help you keep your
project setup consistent and organized.
package com.github.ferstl.maven.pomenforcers;
import java.util.Set;
import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
import org.apache.maven.project.MavenProject;
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.EnforcerRuleUtils;
import com.github.ferstl.maven.pomenforcers.util.XmlUtils;
import com.google.common.collect.Sets;
import static com.github.ferstl.maven.pomenforcers.util.CommaSeparatorUtils.splitAndAddToCollection;
/**
* Enforces that only a well-defined set of POMs may declare plugin management.
* * ### Example
* <rules>
* <dependencyManagementLocation implementation="com.github.ferstl.maven.pomenforcers.PedanticDependencyManagementLocationEnforcer">
* <!-- Only these POMs may declare dependency management -->
* <dependencyManagingPoms>com.example.myproject:parent,com.example.myproject:subparent</dependencyManagingPoms>
* </dependencyManagementLocation>
* </rules>
*
* @id {@link PedanticEnforcerRule#DEPENDENCY_MANAGEMENT_LOCATION}
*/
public class PedanticDependencyManagementLocationEnforcer extends AbstractPedanticEnforcer {
private final Set dependencyManagingPoms;
public PedanticDependencyManagementLocationEnforcer() {
this.dependencyManagingPoms = Sets.newHashSet();
}
@Override
protected void doEnforce(EnforcerRuleHelper helper, Document pom) throws EnforcerRuleException {
MavenProject mavenProject = EnforcerRuleUtils.getMavenProject(helper);
if (containsDependencyManagement(pom) && !isDependencyManagementAllowed(mavenProject)) {
throw new EnforcerRuleException("One does not simply declare dependency management. " +
"Only these POMs are allowed to manage dependencies: " + this.dependencyManagingPoms);
}
}
/**
* Comma separated list of POMs that may declare <dependencyManagement>
.
* Each POM has to be defined in the format groupId:artifactId
.
* @param dependencyManagingPoms Comma separated list of POMs that may declare plugin management.
* @configParam
* @default n/a
*/
public void setDependencyManagingPoms(String dependencyManagingPoms) {
ArtifactInfoTransformer artifactInfoTransformer = new ArtifactInfoTransformer();
splitAndAddToCollection(dependencyManagingPoms, this.dependencyManagingPoms, artifactInfoTransformer);
}
private boolean containsDependencyManagement(Document pom) {
return XmlUtils.evaluateXPathAsElement("/project/dependencyManagement", pom) != null;
}
private boolean isDependencyManagementAllowed(MavenProject project) {
ArtifactInfo projectInfo = new ArtifactInfo(project.getGroupId(), project.getArtifactId());
return this.dependencyManagingPoms.contains(projectInfo);
}
@Override
protected void accept(PedanticEnforcerVisitor visitor) {
visitor.visit(this);
}
}