Skip to content

Commit 1992401

Browse files
Introduce ruleName for rules configuration
We can define the same rule multiple times with different configuration Each execution has the same default name, with ruleName we can differentiate them
1 parent 5ec0a9f commit 1992401

File tree

7 files changed

+61
-5
lines changed

7 files changed

+61
-5
lines changed

enforcer-api/src/main/java/org/apache/maven/enforcer/rule/api/AbstractEnforcerRule.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ public abstract class AbstractEnforcerRule extends AbstractEnforcerRuleBase {
3434
*/
3535
private EnforcerLevel level = EnforcerLevel.ERROR;
3636

37+
/**
38+
* Rule name for current rule instance.
39+
*/
40+
private String ruleName;
41+
3742
/**
3843
* Current Enforcer execution level
3944
*
@@ -44,6 +49,17 @@ public EnforcerLevel getLevel() {
4449
return level;
4550
}
4651

52+
/**
53+
* Rule name for current rule instance.
54+
*
55+
* @return a rule name.
56+
* @since 3.6.0
57+
*/
58+
@Override
59+
public String getRuleName() {
60+
return ruleName;
61+
}
62+
4763
/**
4864
* If the rule is to be cached during session scope, whole executing of Maven build,
4965
* this id is used as part of the key.
@@ -64,7 +80,7 @@ public String getCacheId() {
6480
* message as a warning.
6581
*
6682
* @throws EnforcerRuleException the enforcer rule exception
67-
* @throws EnforcerRuleError in order to brake a build immediately
83+
* @throws EnforcerRuleError in order to brake a build immediately
6884
*/
6985
public abstract void execute() throws EnforcerRuleException;
7086
}

enforcer-api/src/main/java/org/apache/maven/enforcer/rule/api/EnforcerRuleBase.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,14 @@ default EnforcerLevel getLevel() {
4343
* @param log an {@link EnforcerLogger} instance
4444
*/
4545
default void setLog(EnforcerLogger log) {}
46+
47+
/**
48+
* Rule name for current rule instance.
49+
*
50+
* @return a rule name.
51+
* @since 3.6.0
52+
*/
53+
default String getRuleName() {
54+
return null;
55+
}
4656
}

enforcer-rules/src/site/apt/alwaysPass.apt.vm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ Always Pass
4747
</goals>
4848
<configuration>
4949
<rules>
50-
<AlwaysPass/>
50+
<alwaysPass/>
5151
</rules>
5252
<fail>true</fail>
5353
</configuration>

enforcer-rules/src/site/apt/index.apt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,16 @@ Built-In Rules
103103

104104
[]
105105

106+
Common parameters
107+
108+
The following parameters are supported by all rules:
109+
110+
* <<level>> - steering whether a rule should fail a build or just display a warning, allowed values: <<ERROR>>, <<WARN>>. Default is <<ERROR>>
111+
112+
* <<ruleName>> - optional name of rule configuration
113+
114+
[]
115+
116+
Custom rules
117+
106118
You may also create and inject your own custom rules by following the {{{https://maven.apache.org/enforcer/enforcer-api/writing-a-custom-rule.html}maven-enforcer-rule-api}} instructions.

maven-enforcer-plugin/src/it/projects/always-fail-warn/pom.xml

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,25 @@ under the License.
4242
<goal>enforce</goal>
4343
</goals>
4444
<configuration>
45+
<rulesToSkip>
46+
<rule>fail2</rule>
47+
</rulesToSkip>
4548
<rules>
46-
<AlwaysFail>
49+
<alwaysFail>
4750
<level>WARN</level>
48-
</AlwaysFail>
51+
</alwaysFail>
52+
<alwaysFail>
53+
<ruleName>fail1</ruleName>
54+
<level>WARN</level>
55+
</alwaysFail>
56+
<alwaysFail>
57+
<ruleName>fail2</ruleName>
58+
<level>WARN</level>
59+
</alwaysFail>
60+
<alwaysFail>
61+
<ruleName>fail3</ruleName>
62+
<level>WARN</level>
63+
</alwaysFail>
4964
</rules>
5065
</configuration>
5166
</execution>

maven-enforcer-plugin/src/it/projects/always-fail-warn/verify.groovy

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
*/
1919
File buildLog = new File( basedir, 'build.log' )
2020
assert buildLog.text.contains( '[WARNING] Rule 0: org.apache.maven.enforcer.rules.AlwaysFail warned with message:' )
21+
assert buildLog.text.contains( '[WARNING] Rule 1: org.apache.maven.enforcer.rules.AlwaysFail(fail1) warned with message:' )
22+
assert buildLog.text.contains( '[WARNING] Rule 2: org.apache.maven.enforcer.rules.AlwaysFail(fail3) warned with message:' )
2123

2224

2325

maven-enforcer-plugin/src/main/java/org/apache/maven/plugins/enforcer/internal/EnforcerRuleDesc.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ public EnforcerRuleDesc(String name, EnforcerRuleBase rule) {
4545
}
4646

4747
public String getName() {
48-
return name;
48+
String ruleName = rule.getRuleName();
49+
return ruleName == null || ruleName.isEmpty() ? name : ruleName;
4950
}
5051

5152
public EnforcerRuleBase getRule() {

0 commit comments

Comments
 (0)