You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@maven.apache.org by "btpka3 (via GitHub)" <gi...@apache.org> on 2026/07/31 13:44:01 UTC

[PR] Fix: support custom output for 9 enforcer rules that ignored getMessage() [maven-enforcer]

btpka3 opened a new pull request, #1004:
URL: https://github.com/apache/maven-enforcer/pull/1004

   ## Problem
   
   Several enforcer rules did **not** output the user-configured `<message>` when
   the rule failed. This meant users could not provide custom context or
   explanations for rule violations.
   
   For example, with this configuration:
   
   ```xml
   <bannedPlugins>
     <message>Please remove the deprecated plugin before migrating.</message>
     <excludes>
       <exclude>com.example:old-plugin</exclude>
     </excludes>
   </bannedPlugins>
   ```
   
   The custom message was silently ignored and only the default error details were shown.
   
   ## Solution
   
   Follow the existing pattern (already used by `AlwaysFail`, `BannedDependenciesBase`, etc.)
   to prepend `getMessage()` to the error output when set:
   
   ```java
   StringBuilder buf = new StringBuilder();
   if (getMessage() != null) {
       buf.append(getMessage()).append(System.lineSeparator());
   }
   buf.append("default error details...");
   throw new EnforcerRuleException(buf.toString());
   ```
   
   ## Fixed Rules (9 total)
   
   | Rule | Problem |
   |------|---------|
   | `BannedPlugins` | Threw result directly, never checked `getMessage()` |
   | `BannedRepositories` | Threw errMsg directly |
   | `BanDuplicatePomDependencyVersions` | No message prefix |
   | `BanDistributionManagement` | Inner class threw directly, outer never wrapped |
   | `RequireMatchingCoordinates` | No message prefix |
   | `RequireExplicitDependencyScope` | No message prefix |
   | `RequireProfileIdsExist` | No message prefix |
   | `RequireSameVersions` | No message prefix |
   | `RequirePrerequisite` | Used replacement pattern instead of prepend |
   
   ## Tests
   
   All 9 modified rules now have test coverage verifying the custom message is correctly prepended:
   
   | Test File | Status |
   |-----------|--------|
   | `TestBannedPlugins` | **New** (4 tests) |
   | `TestBannedRepositories` | Added `shouldOutputCustomMessageWhenBanned` |
   | `TestRequireSameVersions` | Added `shouldOutputCustomMessageWhenVersionsDiffer` |
   | `BanDistributionManagementTest` | Added `shouldOutputCustomMessageWhenBanned` |
   | `RequirePrerequisiteTest` | Added `shouldOutputCustomMessageWhenPrerequisiteNotSet` |
   | `TestRequireExplicitDependencyScope` | **New** |
   | `TestRequireProfileIdsExist` | **New** |
   | `TestRequireMatchingCoordinates` | **New** (2 tests) |
   
   All **259 tests pass** with `mvn -pl enforcer-rules test`.
   
   ## Checklist
   
   - [x] Your pull request should address just one issue, without pulling in other changes.
   - [x] Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
   - [x] Each commit in the pull request should have a meaningful subject line and body.
   - [x] Write unit tests that match behavioral changes.
   - [x] Run `mvn verify` to make sure basic checks pass.
   - [x] You have run the integration tests successfully (`mvn -Prun-its verify`).
   
   ## ICLA
   
   ICLA has been submitted to secretary@apache.org and is pending approval.
   
   - [x] I hereby declare this contribution to be licenced under the [Apache License Version 2.0, January 2004](http://www.apache.org/licenses/LICENSE-2.0)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix: support custom output for 9 enforcer rules that ignored getMessage() [maven-enforcer]

Posted by "btpka3 (via GitHub)" <gi...@apache.org>.
btpka3 commented on code in PR #1004:
URL: https://github.com/apache/maven-enforcer/pull/1004#discussion_r3823686730


##########
enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/BanDistributionManagement.java:
##########
@@ -72,12 +72,23 @@ public void execute() throws EnforcerRuleException {
                 getLog().debug("We are in the root of the execution and we have a parent.");
 
                 DistributionManagementCheck check = new DistributionManagementCheck(project);
-                check.execute(isAllowRepository(), isAllowSnapshotRepository(), isAllowSite());
+                executeCheck(check);
             }
         } else {
             getLog().debug("We are in a deeper level.");
             DistributionManagementCheck check = new DistributionManagementCheck(project);
+            executeCheck(check);
+        }
+    }
+
+    private void executeCheck(DistributionManagementCheck check) throws EnforcerRuleException {
+        try {
             check.execute(isAllowRepository(), isAllowSnapshotRepository(), isAllowSite());
+        } catch (EnforcerRuleException e) {
+            if (getMessage() != null) {
+                throw new EnforcerRuleException(getMessage() + System.lineSeparator() + e.getMessage(), e);

Review Comment:
   Fixed. Replaced System.lineSeparator() with "\n" in all affected files (see commit d4f64b5).



##########
enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/BanDuplicatePomDependencyVersions.java:
##########
@@ -119,6 +119,9 @@ private void maven2Validation(Model model) throws EnforcerRuleException {
 
         if (summary.length() > 0) {
             StringBuilder message = new StringBuilder();
+            if (getMessage() != null) {
+                message.append(getMessage()).append(System.lineSeparator());

Review Comment:
   Fixed. Replaced System.lineSeparator() with "\n" in all affected files (see commit d4f64b5).



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix: support custom output for 9 enforcer rules that ignored getMessage() [maven-enforcer]

Posted by "elharo (via GitHub)" <gi...@apache.org>.
elharo merged PR #1004:
URL: https://github.com/apache/maven-enforcer/pull/1004


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix: support custom output for 9 enforcer rules that ignored getMessage() [maven-enforcer]

Posted by "elharo (via GitHub)" <gi...@apache.org>.
elharo commented on code in PR #1004:
URL: https://github.com/apache/maven-enforcer/pull/1004#discussion_r3825426464


##########
enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/BanDistributionManagement.java:
##########
@@ -72,12 +72,23 @@ public void execute() throws EnforcerRuleException {
                 getLog().debug("We are in the root of the execution and we have a parent.");
 
                 DistributionManagementCheck check = new DistributionManagementCheck(project);
-                check.execute(isAllowRepository(), isAllowSnapshotRepository(), isAllowSite());
+                executeCheck(check);
             }
         } else {
             getLog().debug("We are in a deeper level.");
             DistributionManagementCheck check = new DistributionManagementCheck(project);
+            executeCheck(check);
+        }
+    }
+
+    private void executeCheck(DistributionManagementCheck check) throws EnforcerRuleException {

Review Comment:
   Not for this PR but as I look at this now I realize we've designed this completely wrong. There shouldn't be exceptions here at all. Bleah.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix: support custom output for 9 enforcer rules that ignored getMessage() [maven-enforcer]

Posted by "elharo (via GitHub)" <gi...@apache.org>.
elharo commented on code in PR #1004:
URL: https://github.com/apache/maven-enforcer/pull/1004#discussion_r3813280134


##########
enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/BanDistributionManagement.java:
##########
@@ -72,12 +72,23 @@ public void execute() throws EnforcerRuleException {
                 getLog().debug("We are in the root of the execution and we have a parent.");
 
                 DistributionManagementCheck check = new DistributionManagementCheck(project);
-                check.execute(isAllowRepository(), isAllowSnapshotRepository(), isAllowSite());
+                executeCheck(check);
             }
         } else {
             getLog().debug("We are in a deeper level.");
             DistributionManagementCheck check = new DistributionManagementCheck(project);
+            executeCheck(check);
+        }
+    }
+
+    private void executeCheck(DistributionManagementCheck check) throws EnforcerRuleException {
+        try {
             check.execute(isAllowRepository(), isAllowSnapshotRepository(), isAllowSite());
+        } catch (EnforcerRuleException e) {
+            if (getMessage() != null) {
+                throw new EnforcerRuleException(getMessage() + System.lineSeparator() + e.getMessage(), e);

Review Comment:
   do not use System.lineSeparator() here as exception messages must be platform independent. Use \n



##########
enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/BannedPlugins.java:
##########
@@ -78,7 +78,12 @@ public void execute() throws EnforcerRuleException {
                         (m1, m2) -> m1.append(m2.toString()))
                 .toString();
         if (!result.isEmpty()) {
-            throw new EnforcerRuleException(result);
+            StringBuilder buf = new StringBuilder();
+            if (getMessage() != null) {
+                buf.append(getMessage()).append(System.lineSeparator());

Review Comment:
   do not use System.lineSeparator() here as exception messages must be platform independent. Use \n



##########
enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/BanDuplicatePomDependencyVersions.java:
##########
@@ -119,6 +119,9 @@ private void maven2Validation(Model model) throws EnforcerRuleException {
 
         if (summary.length() > 0) {
             StringBuilder message = new StringBuilder();
+            if (getMessage() != null) {
+                message.append(getMessage()).append(System.lineSeparator());

Review Comment:
   do not use System.lineSeparator() here as exception messages must be platform independent. Use \n



##########
enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/BannedRepositories.java:
##########
@@ -91,7 +91,12 @@ public void execute() throws EnforcerRuleException {
         String errMsg = repoErrMsg + pluginRepoErrMsg;
 
         if (!errMsg.isEmpty()) {
-            throw new EnforcerRuleException(errMsg);
+            StringBuilder buf = new StringBuilder();
+            if (getMessage() != null) {
+                buf.append(getMessage()).append(System.lineSeparator());

Review Comment:
   do not use System.lineSeparator() here as exception messages must be platform independent. Use \n



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix: support custom output for 9 enforcer rules that ignored getMessage() [maven-enforcer]

Posted by "btpka3 (via GitHub)" <gi...@apache.org>.
btpka3 commented on code in PR #1004:
URL: https://github.com/apache/maven-enforcer/pull/1004#discussion_r3823688444


##########
enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/BannedPlugins.java:
##########
@@ -78,7 +78,12 @@ public void execute() throws EnforcerRuleException {
                         (m1, m2) -> m1.append(m2.toString()))
                 .toString();
         if (!result.isEmpty()) {
-            throw new EnforcerRuleException(result);
+            StringBuilder buf = new StringBuilder();
+            if (getMessage() != null) {
+                buf.append(getMessage()).append(System.lineSeparator());

Review Comment:
   Fixed. Replaced System.lineSeparator() with "\n" in all affected files (see commit d4f64b5).



##########
enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/BannedRepositories.java:
##########
@@ -91,7 +91,12 @@ public void execute() throws EnforcerRuleException {
         String errMsg = repoErrMsg + pluginRepoErrMsg;
 
         if (!errMsg.isEmpty()) {
-            throw new EnforcerRuleException(errMsg);
+            StringBuilder buf = new StringBuilder();
+            if (getMessage() != null) {
+                buf.append(getMessage()).append(System.lineSeparator());

Review Comment:
   Fixed. Replaced System.lineSeparator() with "\n" in all affected files (see commit d4f64b5).



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org