Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,14 @@ boolean isParamsMatched(
MavenProject project, MojoExecution mojoExecution, Mojo mojo, CompletedExecution completedExecution) {
List<TrackedProperty> tracked = cacheConfig.getTrackedProperties(mojoExecution);

if (mojoExecution.getPlugin() != null) {
LOGGER.debug(
"Checking parameter match for {}:{} - tracking {} properties",
mojoExecution.getPlugin().getArtifactId(),
mojoExecution.getGoal(),
tracked.size());
}

for (TrackedProperty trackedProperty : tracked) {
final String propertyName = trackedProperty.getPropertyName();

Expand All @@ -367,7 +375,7 @@ boolean isParamsMatched(
expectedValue = trackedProperty.getDefaultValue() != null ? trackedProperty.getDefaultValue() : "null";
}

final String currentValue;
String currentValue;
try {
Object value = ReflectionUtils.getValueIncludingSuperclasses(propertyName, mojo);

Expand All @@ -386,8 +394,18 @@ boolean isParamsMatched(
} catch (IllegalAccessException e) {
LOGGER.error("Cannot extract plugin property {} from mojo {}", propertyName, mojo, e);
return false;
} catch (Exception e) {
// Catch all exceptions including NullPointerException when property doesn't exist in mojo
LOGGER.warn(
"Property '{}' not found in mojo {} - treating as null",
propertyName,
mojo.getClass().getSimpleName());
currentValue = "null";
}

LOGGER.debug(
"Checking property '{}': expected='{}', actual='{}'", propertyName, expectedValue, currentValue);

if (!Strings.CS.equals(currentValue, expectedValue)) {
if (!Strings.CS.equals(currentValue, trackedProperty.getSkipValue())) {
LOGGER.info(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -796,14 +796,18 @@ private boolean isCachedSegmentPropertiesPresent(
return false;
}

// Allow cache restore even if some tracked properties are missing from the cached build.
// The reconciliation check will detect mismatches and trigger rebuild if needed.
// This provides backward compatibility when new properties are added to tracking.
if (!DtoUtils.containsAllProperties(cachedExecution, trackedProperties)) {
LOGGER.warn(
"Cached build record doesn't contain all tracked properties. Plugin: {}, goal: {},"
+ " executionId: {}",
LOGGER.info(
"Cached build record doesn't contain all currently-tracked properties. "
+ "Plugin: {}, goal: {}, executionId: {}. "
+ "Proceeding with cache restore - reconciliation will verify parameters.",
mojoExecution.getPlugin(),
mojoExecution.getGoal(),
mojoExecution.getExecutionId());
return false;
// Don't reject the cache - let reconciliation check handle it
}
}
return true;
Expand Down
183 changes: 171 additions & 12 deletions src/main/java/org/apache/maven/buildcache/xml/CacheConfigImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
import org.apache.maven.buildcache.xml.config.Exclude;
import org.apache.maven.buildcache.xml.config.Executables;
import org.apache.maven.buildcache.xml.config.ExecutionConfigurationScan;
import org.apache.maven.buildcache.xml.config.ExecutionControl;
import org.apache.maven.buildcache.xml.config.ExecutionIdsList;
import org.apache.maven.buildcache.xml.config.GoalReconciliation;
import org.apache.maven.buildcache.xml.config.GoalsList;
Expand Down Expand Up @@ -118,6 +117,7 @@ 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 volatile CacheState state;
private CacheConfig cacheConfig;
Expand All @@ -129,6 +129,7 @@ public CacheConfigImpl(XmlService xmlService, Provider<MavenSession> providerSes
this.xmlService = xmlService;
this.providerSession = providerSession;
this.rtInfo = rtInfo;
this.parameterLoader = new PluginParameterLoader();
}

@Nonnull
Expand Down Expand Up @@ -248,27 +249,185 @@ 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();
// 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;
}
}
}

// Auto-generate from parameter definitions (track all functional parameters)
GoalReconciliation autoGenerated = generateReconciliationFromParameters(plugin, goal);
if (autoGenerated != null) {
LOGGER.debug(
"Auto-generated reconciliation config for {}:{} with {} functional parameters",
plugin.getArtifactId(),
goal,
autoGenerated.getReconciles() != null
? autoGenerated.getReconciles().size()
: 0);
}
return autoGenerated;
}

/**
* 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;
}

for (GoalReconciliation goalReconciliationConfig : reconciliation) {
final String goal = mojoExecution.getGoal();
// Validate each tracked property
List<TrackedProperty> properties = config.getReconciles();
if (properties == null) {
return;
}

if (isPluginMatch(mojoExecution.getPlugin(), goalReconciliationConfig)
&& Strings.CS.equals(goal, goalReconciliationConfig.getGoal())) {
return goalReconciliationConfig;
for (TrackedProperty property : properties) {
String propertyName = property.getPropertyName();

if (!goalDef.hasParameter(propertyName)) {
LOGGER.warn(
"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);
}
}
}
return null;
}

/**
* Auto-generates a reconciliation config by tracking all functional parameters
* from the plugin parameter definition.
* This provides automatic default tracking for any plugin with parameter definitions.
*
* @param plugin The plugin to generate config for
* @param goal The goal name
* @return Auto-generated config tracking all functional parameters, or null if no parameter definition exists
*/
private GoalReconciliation generateReconciliationFromParameters(Plugin plugin, String goal) {
String artifactId = plugin.getArtifactId();
String pluginVersion = plugin.getVersion();

LOGGER.debug(
"Attempting to auto-generate reconciliation config for {}:{} version {}",
artifactId,
goal,
pluginVersion);

// Load parameter definition for this plugin
PluginParameterDefinition pluginDef = parameterLoader.load(artifactId, pluginVersion);
if (pluginDef == null) {
LOGGER.debug("No parameter definition found for {}:{}", artifactId, pluginVersion);
return null;
}

// Get goal definition
PluginParameterDefinition.GoalParameterDefinition goalDef = pluginDef.getGoal(goal);
if (goalDef == null) {
LOGGER.debug("No goal definition found for goal '{}' in plugin {}", goal, artifactId);
return null;
}

LOGGER.debug(
"Found goal definition for {}:{} with {} total parameters",
artifactId,
goal,
goalDef.getParameters().size());

// Collect all functional parameters
List<TrackedProperty> functionalProperties = new ArrayList<>();
for (PluginParameterDefinition.ParameterDefinition param :
goalDef.getParameters().values()) {
if (param.isFunctional()) {
LOGGER.debug("Adding functional parameter '{}' to auto-generated config", param.getName());
TrackedProperty property = new TrackedProperty();
property.setPropertyName(param.getName());
functionalProperties.add(property);
}
}

// Only create config if there are functional parameters to track
if (functionalProperties.isEmpty()) {
LOGGER.debug("No functional parameters found for {}:{}", artifactId, goal);
return null;
}

LOGGER.debug("Created auto-generated config with {} functional parameters", functionalProperties.size());

// Create auto-generated reconciliation config
GoalReconciliation config = new GoalReconciliation();
config.setArtifactId(artifactId);
if (plugin.getGroupId() != null) {
config.setGroupId(plugin.getGroupId());
}
config.setGoal(goal);
for (TrackedProperty property : functionalProperties) {
config.addReconcile(property);
}

return config;
}

@Nonnull
Expand Down
Loading