From 0b788830b78737b5879c147cc0d5aa63f35736bb Mon Sep 17 00:00:00 2001 From: twalters Date: Sun, 8 Mar 2026 00:29:20 +0000 Subject: [PATCH 1/2] include/exclude filters --- pom.xml | 2 ++ .../solubris/enforcer/UnusedPropertyRule.java | 29 +++++++++++++++++-- .../enforcer/UnusedPropertyRuleTest.java | 3 +- 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/pom.xml b/pom.xml index ade6040..7a2511e 100644 --- a/pom.xml +++ b/pom.xml @@ -19,6 +19,7 @@ junit.version,kept-for-compat.version 7.22.0 6.0.3 + 6.0.3 @@ -300,6 +301,7 @@ true + diff --git a/src/main/java/com/solubris/enforcer/UnusedPropertyRule.java b/src/main/java/com/solubris/enforcer/UnusedPropertyRule.java index 2adf8ec..91e22da 100644 --- a/src/main/java/com/solubris/enforcer/UnusedPropertyRule.java +++ b/src/main/java/com/solubris/enforcer/UnusedPropertyRule.java @@ -13,11 +13,14 @@ import java.util.Map; import java.util.Objects; import java.util.Set; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import java.util.stream.Stream; import static com.solubris.enforcer.ModelScanner.scanModel; import static com.solubris.enforcer.Violations.throwViolations; import static java.util.Collections.emptyList; +import static java.util.function.Predicate.not; import static java.util.stream.Collectors.groupingBy; import static java.util.stream.Collectors.toList; import static java.util.stream.Collectors.toUnmodifiableSet; @@ -36,6 +39,9 @@ public class UnusedPropertyRule extends AbstractEnforcerRule { private final Model originalModel; private final Model effectiveModel; + protected String includesPattern = ".*\\.version"; + protected String excludesPattern = "project\\..*"; + @SuppressWarnings("unused") @Inject public UnusedPropertyRule(MavenSession session) { @@ -63,7 +69,8 @@ protected Stream scan() { return originalModel.getProperties().entrySet().stream() .map(UnusedPropertyRule::asStringEntry) - .filter(UnusedPropertyRule::isVersionProperty) + .filter(this::isIncluded) + .filter(not(this::isExcluded)) .filter(e -> !suppressed.contains(e.getKey())) .map(e -> { String propName = e.getKey(); @@ -85,8 +92,24 @@ private static Set extractSuppressions(Model model) { .collect(toUnmodifiableSet()); } - private static boolean isVersionProperty(Map.Entry e) { - return e.getKey().endsWith(".version"); + /** + * TODO should maven properties like ${project.version} be considered here? + * project.version is typically defined in the version element, so why would it be defined in the properties? + * Using a regex could cover both cases. + */ + private boolean isIncluded(Map.Entry e) { + // TODO these patterns could be precompiled and stored as fields instead of being recompiled for every property + if (includesPattern == null || includesPattern.isBlank()) return false; + Pattern includes = Pattern.compile(includesPattern); + Matcher includesMatcher = includes.matcher(e.getKey()); + return includesMatcher.matches(); + } + + private boolean isExcluded(Map.Entry e) { + if (excludesPattern == null || excludesPattern.isBlank()) return false; + Pattern excludes = Pattern.compile(excludesPattern); + Matcher excludesMatcher = excludes.matcher(e.getKey()); + return excludesMatcher.matches(); } /** diff --git a/src/test/java/com/solubris/enforcer/UnusedPropertyRuleTest.java b/src/test/java/com/solubris/enforcer/UnusedPropertyRuleTest.java index 89c9243..0a4774b 100644 --- a/src/test/java/com/solubris/enforcer/UnusedPropertyRuleTest.java +++ b/src/test/java/com/solubris/enforcer/UnusedPropertyRuleTest.java @@ -28,9 +28,8 @@ class UnusedPropertyRuleTest { private final UnusedPropertyRule rule; UnusedPropertyRuleTest() { - UnusedPropertyRule rule = new UnusedPropertyRule(originalModel, effectiveModel); + this.rule = new UnusedPropertyRule(originalModel, effectiveModel); rule.setLog(mock(EnforcerLogger.class)); - this.rule = rule; } @Test From 0b8a538ff4eb4ad855b0f45907d1c69bb0154eed Mon Sep 17 00:00:00 2001 From: twalters Date: Sun, 8 Mar 2026 00:54:13 +0000 Subject: [PATCH 2/2] auto analysis not working --- .github/workflows/maven.yml | 4 +++- pom.xml | 2 ++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/.github/workflows/maven.yml b/.github/workflows/maven.yml index 90c8859..a0efb23 100644 --- a/.github/workflows/maven.yml +++ b/.github/workflows/maven.yml @@ -28,7 +28,9 @@ jobs: distribution: 'temurin' cache: maven - name: Build with Maven - run: mvn -B install + env: + SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} + run: mvn -B install org.sonarsource.scanner.maven:sonar-maven-plugin:sonar -Dsonar.projectKey=solubris_coding-rules # Optional: Uploads the full dependency graph to GitHub to improve the quality of Dependabot alerts this repository can receive # - name: Update dependency graph diff --git a/pom.xml b/pom.xml index 971409d..4af43f3 100644 --- a/pom.xml +++ b/pom.xml @@ -20,6 +20,8 @@ 7.22.0 6.0.3 6.0.3 + + solubris