Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .github/workflows/maven.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
<unusedPropertyRule.suppressions>junit.version,kept-for-compat.version</unusedPropertyRule.suppressions>
<pmd.version>7.22.0</pmd.version>
<junit.version>6.0.3</junit.version>
<project.junit.version>6.0.3</project.junit.version>

<sonar.organization>solubris</sonar.organization>
</properties>

<dependencies>
Expand Down Expand Up @@ -300,6 +303,7 @@
<requirePropertiesForDuplicates>true</requirePropertiesForDuplicates>
</versionPropertyRule>
<bomImportScopeRule/>
<unusedPropertyRule/>
</rules>
</configuration>
<executions>
Expand Down
29 changes: 26 additions & 3 deletions src/main/java/com/solubris/enforcer/UnusedPropertyRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -63,7 +69,8 @@ protected Stream<String> 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();
Expand All @@ -85,8 +92,24 @@ private static Set<String> extractSuppressions(Model model) {
.collect(toUnmodifiableSet());
}

private static boolean isVersionProperty(Map.Entry<String, String> 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<String, String> 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<String, String> e) {
if (excludesPattern == null || excludesPattern.isBlank()) return false;
Pattern excludes = Pattern.compile(excludesPattern);
Matcher excludesMatcher = excludes.matcher(e.getKey());
return excludesMatcher.matches();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading