Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
189 changes: 178 additions & 11 deletions src/main/java/org/apache/maven/buildcache/xml/CacheConfigImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ public class CacheConfigImpl implements org.apache.maven.buildcache.xml.CacheCon
private final XmlService xmlService;
private final Provider<MavenSession> providerSession;
private final RuntimeInformation rtInfo;
private final PluginParameterLoader parameterLoader;
private final DefaultReconciliationLoader defaultReconciliationLoader;

private volatile CacheState state;
private CacheConfig cacheConfig;
Expand All @@ -129,6 +131,8 @@ public CacheConfigImpl(XmlService xmlService, Provider<MavenSession> providerSes
this.xmlService = xmlService;
this.providerSession = providerSession;
this.rtInfo = rtInfo;
this.parameterLoader = new PluginParameterLoader();
this.defaultReconciliationLoader = new DefaultReconciliationLoader();
}

@Nonnull
Expand Down Expand Up @@ -248,29 +252,192 @@ public boolean isLogAllProperties(MojoExecution mojoExecution) {
}

private GoalReconciliation findReconciliationConfig(MojoExecution mojoExecution) {
if (cacheConfig.getExecutionControl() == null) {
if (mojoExecution == null) {
return null;
}

final ExecutionControl executionControl = cacheConfig.getExecutionControl();
if (executionControl.getReconcile() == null) {
final String goal = mojoExecution.getGoal();
final Plugin plugin = mojoExecution.getPlugin();

if (plugin == null) {
return null;
}

final List<GoalReconciliation> reconciliation =
executionControl.getReconcile().getPlugins();

for (GoalReconciliation goalReconciliationConfig : reconciliation) {
final String goal = mojoExecution.getGoal();
// First check explicit configuration
if (cacheConfig.getExecutionControl() != null && cacheConfig.getExecutionControl().getReconcile() != null) {
List<GoalReconciliation> explicitConfigs =
cacheConfig.getExecutionControl().getReconcile().getPlugins();
for (GoalReconciliation config : explicitConfigs) {
if (isPluginMatch(plugin, config) && Strings.CS.equals(goal, config.getGoal())) {
// Validate explicit config against parameter definitions (with version)
validateReconciliationConfig(config, plugin);
return config;
}
}
}

if (isPluginMatch(mojoExecution.getPlugin(), goalReconciliationConfig)
&& Strings.CS.equals(goal, goalReconciliationConfig.getGoal())) {
return goalReconciliationConfig;
// Fall back to defaults if no explicit configuration found
List<GoalReconciliation> defaults = getDefaultReconciliationConfigs();
for (GoalReconciliation config : defaults) {
if (isPluginMatch(plugin, config) && Strings.CS.equals(goal, config.getGoal())) {
// Validate default config against parameter definitions (with version)
validateReconciliationConfig(config, plugin);
return config;
}
}

return null;
}

/**
* Validates a single reconciliation config against plugin parameter definitions.
* Uses plugin version to load the appropriate parameter definition.
*/
private void validateReconciliationConfig(GoalReconciliation config, Plugin plugin) {
String artifactId = config.getArtifactId();
String goal = config.getGoal();
String pluginVersion = plugin.getVersion();

// Load parameter definition for this plugin with version
PluginParameterDefinition pluginDef = parameterLoader.load(artifactId, pluginVersion);

if (pluginDef == null) {
LOGGER.warn(
"No parameter definition found for plugin {}:{} version {}. "
+ "Cannot validate reconciliation configuration. "
+ "Consider adding a parameter definition file to plugin-parameters/{}.xml",
artifactId,
goal,
pluginVersion != null ? pluginVersion : "unknown",
artifactId);
return;
}

// Get goal definition
PluginParameterDefinition.GoalParameterDefinition goalDef = pluginDef.getGoal(goal);
if (goalDef == null) {
LOGGER.warn(
"Goal '{}' not found in parameter definition for plugin {} version {}. "
+ "Cannot validate reconciliation configuration.",
goal,
artifactId,
pluginVersion != null ? pluginVersion : "unknown");
return;
}

// Validate each tracked property
List<TrackedProperty> properties = config.getReconciles();
if (properties == null) {
return;
}

for (TrackedProperty property : properties) {
String propertyName = property.getPropertyName();

if (!goalDef.hasParameter(propertyName)) {
LOGGER.error(
"Unknown parameter '{}' in reconciliation config for {}:{} version {}. "
+ "This may indicate a plugin version mismatch or renamed parameter. "
+ "Consider updating parameter definition or removing from reconciliation.",
propertyName,
artifactId,
goal,
pluginVersion != null ? pluginVersion : "unknown");
} else {
PluginParameterDefinition.ParameterDefinition paramDef = goalDef.getParameter(propertyName);
if (paramDef.isBehavioral()) {
LOGGER.warn(
"Parameter '{}' in reconciliation config for {}:{} is categorized as BEHAVIORAL. "
+ "Behavioral parameters affect how the build runs but not the output. "
+ "Consider removing if it doesn't actually affect build artifacts.",
propertyName,
artifactId,
goal);
}
}
}
}

/**
* Load default reconciliation configurations from XML.
* Defaults are loaded from classpath: default-reconciliation/defaults.xml
*/
private List<GoalReconciliation> getDefaultReconciliationConfigs() {
List<GoalReconciliation> defaults = defaultReconciliationLoader.loadDefaults();

// Validate all default configurations against parameter definitions
validateReconciliationConfigs(defaults);

return defaults;
}

/**
* Validates reconciliation configs against plugin parameter definitions.
* Warns about unknown parameters that may indicate plugin changes or configuration errors.
*/
private void validateReconciliationConfigs(List<GoalReconciliation> configs) {
for (GoalReconciliation config : configs) {
String artifactId = config.getArtifactId();
String goal = config.getGoal();

// Load parameter definition for this plugin
PluginParameterDefinition pluginDef = parameterLoader.load(artifactId);

if (pluginDef == null) {
LOGGER.warn(
"No parameter definition found for plugin {}:{}. "
+ "Cannot validate reconciliation configuration. "
+ "Consider adding a parameter definition file to plugin-parameters/{}.xml",
artifactId,
goal,
artifactId);
continue;
}

Copy link
Contributor

@AlexanderAshitkin AlexanderAshitkin Oct 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a debug statement here please? Something like: Default properties to reconcile for a plugin goal {}: {}
If it is not logged upstack, it could be helpful for troubleshooting.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

YAGNI

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me know how you guys want to handle this. I'm fine either way.

// Get goal definition
PluginParameterDefinition.GoalParameterDefinition goalDef = pluginDef.getGoal(goal);
if (goalDef == null) {
LOGGER.warn(
"Goal '{}' not found in parameter definition for plugin {}. "
+ "Cannot validate reconciliation configuration.",
goal,
artifactId);
continue;
}

// Validate each tracked property
List<TrackedProperty> properties = config.getReconciles();
if (properties != null) {
for (TrackedProperty property : properties) {
String propertyName = property.getPropertyName();

if (!goalDef.hasParameter(propertyName)) {
LOGGER.error(
"Unknown parameter '{}' in default reconciliation config for {}:{}. "
+ "This parameter is not defined in the plugin parameter definition. "
+ "This may indicate a plugin version mismatch or renamed parameter. "
+ "Please update the parameter definition or remove this property from reconciliation.",
propertyName,
artifactId,
goal);
} else {
PluginParameterDefinition.ParameterDefinition paramDef =
goalDef.getParameter(propertyName);
if (paramDef.isBehavioral()) {
LOGGER.warn(
"Parameter '{}' in reconciliation config for {}:{} is categorized as BEHAVIORAL. "
+ "Behavioral parameters typically should not affect cache invalidation. "
+ "Consider removing this parameter from reconciliation if it doesn't affect build output.",
propertyName,
artifactId,
goal);
}
}
}
}
}
}

@Nonnull
@Override
public List<PropertyName> getLoggedProperties(MojoExecution mojoExecution) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.maven.buildcache.xml;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.maven.buildcache.xml.config.GoalReconciliation;
import org.apache.maven.buildcache.xml.config.TrackedProperty;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

/**
* Loads default reconciliation configurations from classpath resources.
* Default configs are stored in src/main/resources/default-reconciliation/defaults.xml
*/
public class DefaultReconciliationLoader {

private static final Logger LOGGER = LoggerFactory.getLogger(DefaultReconciliationLoader.class);
private static final String DEFAULTS_PATH = "default-reconciliation/defaults.xml";

private List<GoalReconciliation> cachedDefaults;

/**
* Load default reconciliation configurations from XML
*/
public List<GoalReconciliation> loadDefaults() {
if (cachedDefaults != null) {
return cachedDefaults;
}

InputStream is = getClass().getClassLoader().getResourceAsStream(DEFAULTS_PATH);

if (is == null) {
LOGGER.warn("No default reconciliation configuration found at {}", DEFAULTS_PATH);
cachedDefaults = new ArrayList<>();
return cachedDefaults;
}

try {
cachedDefaults = parseDefaults(is);
LOGGER.info("Loaded {} default reconciliation configurations", cachedDefaults.size());
return cachedDefaults;
} catch (Exception e) {
LOGGER.warn("Failed to load default reconciliation configurations: {}", e.getMessage(), e);
cachedDefaults = new ArrayList<>();
return cachedDefaults;
}
}

private List<GoalReconciliation> parseDefaults(InputStream is) throws Exception {
List<GoalReconciliation> defaults = new ArrayList<>();

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(is);

Element root = doc.getDocumentElement();
NodeList pluginNodes = root.getElementsByTagName("plugin");

for (int i = 0; i < pluginNodes.getLength(); i++) {
Element pluginElement = (Element) pluginNodes.item(i);
GoalReconciliation config = parsePlugin(pluginElement);
defaults.add(config);
}

return defaults;
}

private GoalReconciliation parsePlugin(Element pluginElement) {
String artifactId = getTextContent(pluginElement, "artifactId");
String goal = getTextContent(pluginElement, "goal");

GoalReconciliation config = new GoalReconciliation();
config.setArtifactId(artifactId);
config.setGoal(goal);

// Parse properties if present
NodeList propertiesNodes = pluginElement.getElementsByTagName("properties");
if (propertiesNodes.getLength() > 0) {
Element propertiesElement = (Element) propertiesNodes.item(0);
NodeList propertyNodes = propertiesElement.getElementsByTagName("property");

for (int i = 0; i < propertyNodes.getLength(); i++) {
String propertyName = propertyNodes.item(i).getTextContent().trim();
TrackedProperty property = new TrackedProperty();
property.setPropertyName(propertyName);
config.addReconcile(property);
}
}

return config;
}

private String getTextContent(Element parent, String tagName) {
NodeList nodes = parent.getElementsByTagName(tagName);
if (nodes.getLength() > 0) {
return nodes.item(0).getTextContent().trim();
}
return null;
}
}
Loading