Skip to content

Commit fba5896

Browse files
[MENFORCER-458] Move Build-in rules to new API
Moved rules: - banDependencyManagementScope - banDistributionManagement - banDuplicatePomDependencyVersions - banDynamicVersions - bannedPlugins - bannedRepositories - evaluateBeanshell - reactorModuleConvergence - requireActiveProfile - requireExplicitDependencyScope - requirePluginVersions - requirePrerequisite - requireProfileIdsExist - requireReleaseVersion - requireSameVersions - requireSnapshotVersion
1 parent 2a02ddd commit fba5896

File tree

77 files changed

+1875
-1979
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+1875
-1979
lines changed

enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/AbstractStandardEnforcerRule.java

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
package org.apache.maven.enforcer.rules;
2020

2121
import org.apache.maven.enforcer.rule.api.AbstractEnforcerRule;
22+
import org.apache.maven.model.InputLocation;
23+
import org.apache.maven.project.MavenProject;
2224

2325
/**
2426
* Abstract help rule.
@@ -37,4 +39,66 @@ public String getMessage() {
3739
public void setMessage(String message) {
3840
this.message = message;
3941
}
42+
43+
/**
44+
* Returns an identifier of a given project.
45+
* @param project the project
46+
* @return the identifier of the project in the format {@code <groupId>:<artifactId>:<version>}
47+
*/
48+
private static String getProjectId(MavenProject project) {
49+
StringBuilder buffer = new StringBuilder(128);
50+
51+
buffer.append(
52+
(project.getGroupId() != null && project.getGroupId().length() > 0)
53+
? project.getGroupId()
54+
: "[unknown-group-id]");
55+
buffer.append(':');
56+
buffer.append(
57+
(project.getArtifactId() != null && project.getArtifactId().length() > 0)
58+
? project.getArtifactId()
59+
: "[unknown-artifact-id]");
60+
buffer.append(':');
61+
buffer.append(
62+
(project.getVersion() != null && project.getVersion().length() > 0)
63+
? project.getVersion()
64+
: "[unknown-version]");
65+
66+
return buffer.toString();
67+
}
68+
69+
/**
70+
* Creates a string with line/column information for problems originating directly from this POM. Inspired by
71+
* {@code o.a.m.model.building.ModelProblemUtils.formatLocation(...)}.
72+
*
73+
* @param project the current project.
74+
* @param location The location which should be formatted, must not be {@code null}.
75+
* @return The formatted problem location or an empty string if unknown, never {@code null}.
76+
*/
77+
protected static String formatLocation(MavenProject project, InputLocation location) {
78+
StringBuilder buffer = new StringBuilder();
79+
80+
if (!location.getSource().getModelId().equals(getProjectId(project))) {
81+
buffer.append(location.getSource().getModelId());
82+
83+
if (location.getSource().getLocation().length() > 0) {
84+
if (buffer.length() > 0) {
85+
buffer.append(", ");
86+
}
87+
buffer.append(location.getSource().getLocation());
88+
}
89+
}
90+
if (location.getLineNumber() > 0) {
91+
if (buffer.length() > 0) {
92+
buffer.append(", ");
93+
}
94+
buffer.append("line ").append(location.getLineNumber());
95+
}
96+
if (location.getColumnNumber() > 0) {
97+
if (buffer.length() > 0) {
98+
buffer.append(", ");
99+
}
100+
buffer.append("column ").append(location.getColumnNumber());
101+
}
102+
return buffer.toString();
103+
}
40104
}

enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/AlwaysFail.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,6 @@ public void execute() throws EnforcerRuleException {
4343

4444
@Override
4545
public String toString() {
46-
return String.format("AlwaysFail[level=%s, message=%s]", getLevel(), getMessage());
46+
return String.format("AlwaysFail[message=%s]", getMessage());
4747
}
4848
}

enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/AlwaysPass.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,6 @@ public void execute() {
4040

4141
@Override
4242
public String toString() {
43-
return String.format("AlwaysPass[level=%s, message=%s]", getLevel(), getMessage());
43+
return String.format("AlwaysPass[message=%s]", getMessage());
4444
}
4545
}

enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/BanDependencyManagementScope.java renamed to enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/BanDependencyManagementScope.java

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,28 @@
1616
* specific language governing permissions and limitations
1717
* under the License.
1818
*/
19-
package org.apache.maven.plugins.enforcer;
19+
package org.apache.maven.enforcer.rules;
20+
21+
import javax.inject.Inject;
22+
import javax.inject.Named;
2023

2124
import java.util.ArrayList;
2225
import java.util.Collections;
2326
import java.util.List;
27+
import java.util.Objects;
2428

25-
import org.apache.maven.enforcer.rule.api.EnforcerRule2;
2629
import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
27-
import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
2830
import org.apache.maven.enforcer.rules.utils.ArtifactMatcher;
2931
import org.apache.maven.model.Dependency;
3032
import org.apache.maven.model.DependencyManagement;
31-
import org.apache.maven.plugin.logging.Log;
3233
import org.apache.maven.project.MavenProject;
33-
import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
3434

3535
/**
3636
* This rule bans all scope values except for {@code import} from dependencies within the dependency management.
3737
* There is a configuration option to ignore certain dependencies in this check.
3838
*/
39-
public class BanDependencyManagementScope extends AbstractNonCacheableEnforcerRule implements EnforcerRule2 {
39+
@Named("banDependencyManagementScope")
40+
public final class BanDependencyManagementScope extends AbstractStandardEnforcerRule {
4041

4142
/**
4243
* Specify the dependencies that will be ignored. This can be a list of artifacts in the format
@@ -54,40 +55,37 @@ public class BanDependencyManagementScope extends AbstractNonCacheableEnforcerRu
5455
*/
5556
private boolean checkEffectivePom = false;
5657

58+
private final MavenProject project;
59+
60+
@Inject
61+
public BanDependencyManagementScope(MavenProject project) {
62+
this.project = Objects.requireNonNull(project);
63+
}
64+
5765
@Override
58-
public void execute(EnforcerRuleHelper helper) throws EnforcerRuleException {
59-
Log logger = helper.getLog();
60-
MavenProject project;
61-
try {
62-
project = (MavenProject) helper.evaluate("${project}");
63-
if (project == null) {
64-
throw new EnforcerRuleException("${project} is null");
65-
}
66-
// only evaluate local depMgmt, without taking into account inheritance and interpolation
67-
DependencyManagement depMgmt = checkEffectivePom
68-
? project.getModel().getDependencyManagement()
69-
: project.getOriginalModel().getDependencyManagement();
70-
if (depMgmt != null && depMgmt.getDependencies() != null) {
71-
List<Dependency> violatingDependencies = getViolatingDependencies(logger, depMgmt);
72-
if (!violatingDependencies.isEmpty()) {
73-
String message = getMessage();
74-
StringBuilder buf = new StringBuilder();
75-
if (message == null) {
76-
message = "Scope other than 'import' is not allowed in 'dependencyManagement'";
77-
}
78-
buf.append(message + System.lineSeparator());
79-
for (Dependency violatingDependency : violatingDependencies) {
80-
buf.append(getErrorMessage(project, violatingDependency));
81-
}
82-
throw new EnforcerRuleException(buf.toString());
66+
public void execute() throws EnforcerRuleException {
67+
// only evaluate local depMgmt, without taking into account inheritance and interpolation
68+
DependencyManagement depMgmt = checkEffectivePom
69+
? project.getModel().getDependencyManagement()
70+
: project.getOriginalModel().getDependencyManagement();
71+
if (depMgmt != null && depMgmt.getDependencies() != null) {
72+
List<Dependency> violatingDependencies = getViolatingDependencies(depMgmt);
73+
if (!violatingDependencies.isEmpty()) {
74+
String message = getMessage();
75+
StringBuilder buf = new StringBuilder();
76+
if (message == null) {
77+
message = "Scope other than 'import' is not allowed in 'dependencyManagement'";
78+
}
79+
buf.append(message + System.lineSeparator());
80+
for (Dependency violatingDependency : violatingDependencies) {
81+
buf.append(getErrorMessage(project, violatingDependency));
8382
}
83+
throw new EnforcerRuleException(buf.toString());
8484
}
85-
} catch (ExpressionEvaluationException e) {
86-
throw new EnforcerRuleException("Cannot resolve expression: " + e.getCause(), e);
8785
}
8886
}
8987

90-
protected List<Dependency> getViolatingDependencies(Log logger, DependencyManagement depMgmt) {
88+
protected List<Dependency> getViolatingDependencies(DependencyManagement depMgmt) {
9189
final ArtifactMatcher excludesMatcher;
9290
if (excludes != null) {
9391
excludesMatcher = new ArtifactMatcher(excludes, Collections.emptyList());
@@ -98,7 +96,8 @@ protected List<Dependency> getViolatingDependencies(Log logger, DependencyManage
9896
for (Dependency dependency : depMgmt.getDependencies()) {
9997
if (dependency.getScope() != null && !"import".equals(dependency.getScope())) {
10098
if (excludesMatcher != null && excludesMatcher.match(dependency)) {
101-
logger.debug("Skipping excluded dependency " + dependency + " with scope " + dependency.getScope());
99+
getLog().debug("Skipping excluded dependency " + dependency + " with scope "
100+
+ dependency.getScope());
102101
continue;
103102
}
104103
violatingDependencies.add(dependency);
@@ -118,7 +117,10 @@ public void setExcludes(List<String> theExcludes) {
118117
this.excludes = theExcludes;
119118
}
120119

121-
public void setCheckEffectivePom(boolean checkEffectivePom) {
122-
this.checkEffectivePom = checkEffectivePom;
120+
@Override
121+
public String toString() {
122+
return String.format(
123+
"BanDependencyManagementScope[message=%s, excludes=%s, checkEffectivePom=%b]",
124+
getMessage(), excludes, checkEffectivePom);
123125
}
124126
}
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.maven.enforcer.rules;
20+
21+
import javax.inject.Inject;
22+
import javax.inject.Named;
23+
24+
import java.util.Objects;
25+
26+
import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
27+
import org.apache.maven.model.DistributionManagement;
28+
import org.apache.maven.project.MavenProject;
29+
30+
/**
31+
* This rule will check if a pom contains a <code>distributionManagement</code> part. This should be by best practice
32+
* only defined once. It could happen that you like to check the parent as well. This can be activated by using the
33+
* <code>ignoreParent</code> which is by default turned off (<code>true</code>) which means not to check the parent.
34+
*
35+
* @author Karl Heinz Marbaise
36+
* @since 1.4
37+
*/
38+
@Named("banDistributionManagement")
39+
public final class BanDistributionManagement extends AbstractStandardEnforcerRule {
40+
41+
/**
42+
* Allow using a repository entry in the distributionManagement area.
43+
*/
44+
private boolean allowRepository = false;
45+
46+
/**
47+
* Allow snapshotRepository entry in the distributionManagement area.
48+
*/
49+
private boolean allowSnapshotRepository = false;
50+
51+
/**
52+
* Allow site entry in the distributionManagement area.
53+
*/
54+
private boolean allowSite = false;
55+
56+
private final MavenProject project;
57+
58+
@Inject
59+
public BanDistributionManagement(MavenProject project) {
60+
this.project = Objects.requireNonNull(project);
61+
}
62+
63+
@Override
64+
public void execute() throws EnforcerRuleException {
65+
66+
if (project.isExecutionRoot()) {
67+
if (project.getParent() == null) {
68+
// Does it make sense to check something? If yes please make a JIRA ticket for it.
69+
getLog().debug("We have no parent and in the root of a build we don't check anything,");
70+
getLog().debug("because that is the location where we defined maven-enforcer-plugin.");
71+
} else {
72+
getLog().debug("We are in the root of the execution and we have a parent.");
73+
74+
DistributionManagementCheck check = new DistributionManagementCheck(project);
75+
check.execute(isAllowRepository(), isAllowSnapshotRepository(), isAllowSite());
76+
}
77+
} else {
78+
getLog().debug("We are in a deeper level.");
79+
DistributionManagementCheck check = new DistributionManagementCheck(project);
80+
check.execute(isAllowRepository(), isAllowSnapshotRepository(), isAllowSite());
81+
}
82+
}
83+
84+
public boolean isAllowRepository() {
85+
return allowRepository;
86+
}
87+
88+
public void setAllowRepository(boolean allowRepository) {
89+
this.allowRepository = allowRepository;
90+
}
91+
92+
public boolean isAllowSnapshotRepository() {
93+
return allowSnapshotRepository;
94+
}
95+
96+
public void setAllowSnapshotRepository(boolean allowSnapshotRepository) {
97+
this.allowSnapshotRepository = allowSnapshotRepository;
98+
}
99+
100+
public boolean isAllowSite() {
101+
return allowSite;
102+
}
103+
104+
public void setAllowSite(boolean allowSite) {
105+
this.allowSite = allowSite;
106+
}
107+
108+
private static class DistributionManagementCheck {
109+
private DistributionManagement distributionManagement;
110+
111+
DistributionManagementCheck(MavenProject project) {
112+
this.distributionManagement = project.getOriginalModel().getDistributionManagement();
113+
}
114+
115+
public void execute(boolean isAllowRepository, boolean isAllowSnapshotRepository, boolean isAllowSite)
116+
throws EnforcerRuleException {
117+
if (hasDistributionManagement()) {
118+
if (!isAllowRepository && hasRepository()) {
119+
throw new EnforcerRuleException("You have defined a repository in distributionManagement.");
120+
} else if (!isAllowSnapshotRepository && hasSnapshotRepository()) {
121+
throw new EnforcerRuleException("You have defined a snapshotRepository in distributionManagement.");
122+
} else if (!isAllowSite && hasSite()) {
123+
throw new EnforcerRuleException("You have defined a site in distributionManagement.");
124+
}
125+
}
126+
}
127+
128+
private boolean hasRepository() {
129+
return getDistributionManagement().getRepository() != null;
130+
}
131+
132+
public DistributionManagement getDistributionManagement() {
133+
return distributionManagement;
134+
}
135+
136+
public void setDistributionManagement(DistributionManagement distributionManagement) {
137+
this.distributionManagement = distributionManagement;
138+
}
139+
140+
private boolean hasSnapshotRepository() {
141+
return getDistributionManagement().getSnapshotRepository() != null;
142+
}
143+
144+
private boolean hasSite() {
145+
return getDistributionManagement().getSite() != null;
146+
}
147+
148+
private boolean hasDistributionManagement() {
149+
return getDistributionManagement() != null;
150+
}
151+
}
152+
153+
@Override
154+
public String toString() {
155+
return String.format(
156+
"BanDistributionManagement[allowRepository=%b, allowSnapshotRepository=%b, allowSite=%b]",
157+
allowRepository, allowSnapshotRepository, allowSite);
158+
}
159+
}

0 commit comments

Comments
 (0)