Skip to content
Snippets Groups Projects
Commit 8690128b 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

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