Skip to content

Commit 1d85c5d

Browse files
committed
Add ignorePattern attribute to TrackedProperty for array filtering
Fixes #375 - Maven 4 auto-injects --module-version to compilerArgs during cache storage but not during validation, causing parameter mismatches. Changes: - Add ignorePattern field to TrackedProperty in MDO model - Implement regex-based filtering in BuildCacheMojosExecutionStrategy - Filter arrays before comparison (both runtime and cached values) The ignorePattern attribute allows filtering specific array elements before comparison, solving the Maven 4 module-version problem while still detecting legitimate compilerArgs changes. Users can configure this in maven-build-cache-config.xml: <reconcile propertyName="compilerArgs" ignorePattern="--module-version"/> Tested with multi-module JPMS project using Maven 4.0.0-rc-4.
1 parent 5990c9f commit 1d85c5d

File tree

2 files changed

+62
-2
lines changed

2 files changed

+62
-2
lines changed

src/main/java/org/apache/maven/buildcache/BuildCacheMojosExecutionStrategy.java

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ boolean isParamsMatched(
379379
Path baseDirPath = project.getBasedir().toPath();
380380
currentValue = normalizedPath(((Path) value), baseDirPath);
381381
} else if (value != null && value.getClass().isArray()) {
382-
currentValue = ArrayUtils.toString(value);
382+
currentValue = filterAndStringifyArray(value, trackedProperty.getIgnorePattern());
383383
} else {
384384
currentValue = String.valueOf(value);
385385
}
@@ -388,7 +388,13 @@ boolean isParamsMatched(
388388
return false;
389389
}
390390

391-
if (!Strings.CS.equals(currentValue, expectedValue)) {
391+
// Apply ignorePattern filtering to expected value if it's an array string representation
392+
String filteredExpectedValue = expectedValue;
393+
if (trackedProperty.getIgnorePattern() != null && expectedValue.startsWith("[") && expectedValue.endsWith("]")) {
394+
filteredExpectedValue = filterArrayString(expectedValue, trackedProperty.getIgnorePattern());
395+
}
396+
397+
if (!Strings.CS.equals(currentValue, filteredExpectedValue)) {
392398
if (!Strings.CS.equals(currentValue, trackedProperty.getSkipValue())) {
393399
LOGGER.info(
394400
"Plugin parameter mismatch found. Parameter: {}, expected: {}, actual: {}",
@@ -434,6 +440,55 @@ private static String normalizedPath(Path path, Path baseDirPath) {
434440
return normalizedPath;
435441
}
436442

443+
/**
444+
* Filters array values based on ignore pattern and converts to string representation.
445+
*/
446+
private static String filterAndStringifyArray(Object array, String ignorePattern) {
447+
if (ignorePattern == null) {
448+
return ArrayUtils.toString(array);
449+
}
450+
451+
java.util.regex.Pattern pattern = java.util.regex.Pattern.compile(ignorePattern);
452+
java.util.List<Object> filtered = new java.util.ArrayList<>();
453+
454+
int length = java.lang.reflect.Array.getLength(array);
455+
for (int i = 0; i < length; i++) {
456+
Object element = java.lang.reflect.Array.get(array, i);
457+
String elementStr = String.valueOf(element);
458+
if (!pattern.matcher(elementStr).find()) {
459+
filtered.add(element);
460+
}
461+
}
462+
463+
return filtered.toString();
464+
}
465+
466+
/**
467+
* Filters an array string representation (e.g., "[a, b, c]") based on ignore pattern.
468+
*/
469+
private static String filterArrayString(String arrayStr, String ignorePattern) {
470+
if (ignorePattern == null || !arrayStr.startsWith("[") || !arrayStr.endsWith("]")) {
471+
return arrayStr;
472+
}
473+
474+
java.util.regex.Pattern pattern = java.util.regex.Pattern.compile(ignorePattern);
475+
String content = arrayStr.substring(1, arrayStr.length() - 1);
476+
if (content.trim().isEmpty()) {
477+
return "[]";
478+
}
479+
480+
String[] elements = content.split(",\\s*");
481+
java.util.List<String> filtered = new java.util.ArrayList<>();
482+
483+
for (String element : elements) {
484+
if (!pattern.matcher(element.trim()).find()) {
485+
filtered.add(element.trim());
486+
}
487+
}
488+
489+
return filtered.toString();
490+
}
491+
437492
private enum CacheRestorationStatus {
438493
SUCCESS,
439494
FAILURE,

src/main/mdo/build-cache-config.mdo

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1459,6 +1459,11 @@ under the License.
14591459
<name>defaultValue</name>
14601460
<type>String</type>
14611461
</field>
1462+
<field xml.attribute="true">
1463+
<name>ignorePattern</name>
1464+
<type>String</type>
1465+
<description>Regular expression pattern to filter out matching values from array/list properties before comparison. Useful for filtering auto-injected values like Maven 4's --module-version</description>
1466+
</field>
14621467
</fields>
14631468
</class>
14641469
</classes>

0 commit comments

Comments
 (0)