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.
/*
* Copyright (c) 2013 by Stefan Ferstl
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.ferstl.maven.pomenforcers;
import java.util.HashSet;
import java.util.Set;
import org.apache.maven.project.MavenProject;
import com.github.ferstl.maven.pomenforcers.model.ArtifactModel;
import com.github.ferstl.maven.pomenforcers.util.EnforcerRuleUtils;
import static com.github.ferstl.maven.pomenforcers.ErrorReport.toList;
import static com.github.ferstl.maven.pomenforcers.model.functions.StringToArtifactTransformer.stringToArtifactModel;
import static com.github.ferstl.maven.pomenforcers.util.CommaSeparatorUtils.splitAndAddToCollection;
/**
* Enforces that only a well-defined set of POMs may declare dependency 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 = new HashSet<>();
}
/**
* 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) {
splitAndAddToCollection(dependencyManagingPoms, this.dependencyManagingPoms, stringToArtifactModel());
}
@Override
protected PedanticEnforcerRule getDescription() {
return PedanticEnforcerRule.DEPENDENCY_MANAGEMENT_LOCATION;
}
@Override
protected void accept(PedanticEnforcerVisitor visitor) {
visitor.visit(this);
}
@Override
protected void doEnforce(ErrorReport report) {
MavenProject mavenProject = EnforcerRuleUtils.getMavenProject(getHelper());
if (containsDependencyManagement() && !isDependencyManagementAllowed(mavenProject)) {
report.addLine("Only these POMs are allowed to manage dependencies:")
.addLine(toList(this.dependencyManagingPoms));
}
}
private boolean containsDependencyManagement() {
return !getProjectModel().getManagedDependencies().isEmpty();
}
private boolean isDependencyManagementAllowed(MavenProject project) {
ArtifactModel projectInfo = new ArtifactModel(project.getGroupId(), project.getArtifactId(), project.getVersion());
return this.dependencyManagingPoms.isEmpty() || this.dependencyManagingPoms.contains(projectInfo);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy