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 8207c35..4af43f3 100644
--- a/pom.xml
+++ b/pom.xml
@@ -19,6 +19,9 @@
junit.version,kept-for-compat.version
7.22.0
6.0.3
+ 6.0.3
+
+ solubris
@@ -300,6 +303,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