Commit c8676cff authored by Thomas Mortagne's avatar Thomas Mortagne
Browse files

XWIKI-19903: Introduce an enforcer to forbidd specific type of dependencies

* add support for more conditions
parent 1958b913
......@@ -36,7 +36,9 @@
import static org.hamcrest.CoreMatchers.sameInstance;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
@ComponentTest
public class DefaultBlameManagerTest
......
......@@ -26,8 +26,8 @@
/**
* Performs checks on the type specified for dependencies in pom.xml files. For example in XWiki Standard we want to
* prevent extension with package {@code jar} and {@code webjar} to depend on {@code xar} extensions. To achieve this
* you would use:
* prevent extension with package {@code jar} and {@code webjar} to depend on {@code xar} extensions but only if the
* module does not ends with -test-docker or -test-tests. To achieve this you would use:
*
* <pre>
* <code>
......@@ -51,6 +51,18 @@
*/
public class BannedDependencyType extends AbstractPomCheck
{
private static final String JAR = "jar";
/**
* The group id pattern of the project to check.
*/
private String projectGroupId;
/**
* The artifact id pattern of the project to check.
*/
private String projectArtifactId;
/**
* The packaging of the project to check.
*/
......@@ -63,32 +75,68 @@ public void execute(EnforcerRuleHelper helper) throws EnforcerRuleException
{
Model model = getModel(helper);
if (this.projectPackaging == null || model.getPackaging().equals(projectPackaging)) {
for (Dependency dependency : model.getDependencies()) {
if (isRuntime(dependency) && getType(dependency).equals(this.dependencyType)) {
StringBuilder builder = new StringBuilder("Found dependency with banned type [");
builder.append(this.dependencyType);
builder.append("]");
if (this.projectPackaging != null) {
builder.append(" for a project with packaging [");
builder.append(this.projectPackaging);
builder.append("]");
}
builder.append(": ");
builder.append(dependency);
throw new EnforcerRuleException(builder.toString());
}
// Check the packaging
if (skipProjectPackaging(model)) {
helper.getLog().info("Skipping as the packaging does not match [" + this.projectPackaging + "]");
return;
}
// Check the group id
if (skipProjectGroupId(model)) {
helper.getLog().info("Skipping as the group id does not match [" + this.projectGroupId + "]");
return;
}
// Check the artifact id
if (skiphProjectArtifactId(model)) {
helper.getLog().info("Skipping as the artifact id does not match [" + this.projectArtifactId + "]");
return;
}
for (Dependency dependency : model.getDependencies()) {
if (isRuntime(dependency) && getType(dependency).equals(this.dependencyType)) {
throw new EnforcerRuleException(
"Found dependency with banned type [" + this.dependencyType + "]: " + dependency);
}
}
}
private boolean skipPattern(String value, String pattern)
{
return pattern != null && !value.matches(pattern);
}
private boolean skipProjectPackaging(Model model)
{
return skipPattern(getPackaging(model), this.projectPackaging);
}
private boolean skipProjectGroupId(Model model)
{
return skipPattern(model.getGroupId(), this.projectGroupId);
}
private boolean skiphProjectArtifactId(Model model)
{
return skipPattern(model.getArtifactId(), this.projectArtifactId);
}
private boolean isRuntime(Dependency dependency)
{
return dependency.getScope() == null || dependency.getScope().equals("runtime");
return dependency.getScope() == null || dependency.getScope().equals("runtime")
|| dependency.getScope().equals("build");
}
private String getPackaging(Model model)
{
return model.getPackaging() != null ? model.getPackaging() : JAR;
}
private String getType(Dependency dependency)
{
return dependency.getType() != null ? dependency.getType() : "jar";
return dependency.getType() != null ? dependency.getType() : JAR;
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment